1. 程式人生 > >經典算法題總結

經典算法題總結

() brush 最小 items 一個數 dex wid pre 不能

第一題:遞歸

  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)

 按照這種姿勢求:

 技術分享圖片

經典算法題總結