添加了示例代码

pull/19/head
jackfrued 2018-08-01 23:58:51 +08:00
parent c2c2786a46
commit 601efd95ba
3 changed files with 51 additions and 0 deletions

View File

@ -0,0 +1,51 @@
# 经典递归求解问题:
# 1. 迷宫寻路
# 2. 汉诺塔(梵塔)
# 3. 骑士周游
# 4. 八皇后
def f(n: int, m=1) -> int:
if n == 0 or n == 1:
return m
return f(n - 1, n * m)
def sum(n: int) -> int:
if n == 1:
return 1
return n + sum(n - 1)
def steps(n: int, m={}) -> int:
if n < 0:
return 0
elif n == 0:
return 1
else:
try:
return m[n]
except:
m[n] = steps(n - 1) + steps(n - 2) + steps(n - 3)
return m[n]
def list_depth(items: list) -> int:
max_depth = 1 if isinstance(items, list) else 0
if max_depth:
for item in items:
if isinstance(item, list):
max_depth = max(max_depth, list_depth(item) + 1)
return max_depth
def main():
mylist = [1, ['a', ['b', ['c']]],[100, [200, 300, [400, [500, [600, [700]]]]]]]
thylist = [[], [[[]]], [[], []]]
print(list_depth(mylist))
print(list_depth(thylist))
if __name__ == '__main__':
main()

Binary file not shown.

After

Width:  |  Height:  |  Size: 540 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 142 KiB