forked from zhanghe06/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursion.py
More file actions
61 lines (47 loc) · 1.13 KB
/
recursion.py
File metadata and controls
61 lines (47 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python
# encoding: utf-8
"""
@author: zhanghe
@software: PyCharm
@file: recursion.py
@time: 2017/6/17 下午4:26
@title: 递归实现阶乘
"""
def factorial(n):
print n
if n == 1:
return 1 # 递归结束
return n * factorial(n - 1) # 问题规模减1,递归调用
nodes = [
{'id': 1, 'parent': None},
{'id': 2, 'parent': 1},
{'id': 3, 'parent': 1},
{'id': 4, 'parent': 2},
{'id': 5, 'parent': 2},
{'id': 6, 'parent': 5},
{'id': 7, 'parent': 6},
{'id': 8, 'parent': 3}
]
node_list = []
def pop_list(nodes, parent=None, node_list=None):
"""
递归父子关系
:param nodes:
:param parent:
:param node_list:
:return:
"""
if parent is None:
return node_list
next_parent = None
node_list.append([])
for node in nodes:
if node['parent'] == parent:
node_list[-1].append(node)
if node['id'] == parent:
next_parent = node['parent']
pop_list(nodes, next_parent, node_list)
return node_list
print pop_list(nodes, 5, node_list)
if __name__ == '__main__':
print factorial(10)