码迷,mamicode.com
首页 > 编程语言 > 详细

经典算法题总结

时间:2018-06-13 14:58:59      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:dex   info   不能   json   height   width   int   http   ems   

第一题:递归

  1.给一个dict或者json 求 value大于53 并且为int 将该value 转换为str

  

mydict1 = {"a":{"a":[1,2,3]},
          "b":{"b":1}}


def Foo(mydict):

    for _,v in mydict.items():
        if isinstance(v,dict):
            Foo(v)

        elif isinstance(v,int) and ord(v) > 53:
            v = str(v)

        elif isinstance(v,list):
            for i in v :
                Foo(i)
        elif isinstance(v,tuple):
            for i in v:
                Foo(i)

  

第二题:逻辑

  2. 给一个数组 [7,3,5,6,4]  最大值不能在比他小的值前面,求最大值和最小值的差?

 

a = [7,3,5,6,4]

big = 0
small = 0
index = 0
for i in a:
    if small == 0:
        small = i
    if i > big:
        if index+1 == len(a):
            if i > big:
                big = i
        elif i > a[index+1]:
            pass
        else:
            big = i

    if i < small:
        small = i
    index += 1 #0 1 2

over = big - small

print(over)

 按照这种姿势求:

 技术分享图片 

 

经典算法题总结

标签:dex   info   不能   json   height   width   int   http   ems   

原文地址:https://www.cnblogs.com/liujiliang/p/9176961.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!