经典算法题总结

第一题:递归

  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)

 按照这种姿势求:

  

猜你喜欢

转载自www.cnblogs.com/liujiliang/p/9176961.html