-
Notifications
You must be signed in to change notification settings - Fork 0
/
03_01_递归练习.py
57 lines (41 loc) · 954 Bytes
/
03_01_递归练习.py
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
# coding=UTF-8
# 尾递归
def sum_number(arr, total=0):
""" 递归求和
:param arr: 数组
:param total: 总和
:return: total 总和
"""
if len(arr) == 0:
return total
else:
total += arr.pop(0)
return sum_number(arr, total)
aa = sum_number([1, 2, 3])
print(aa)
def count_list(arr, count=0):
"""递归计算包含元素数
:param arr: 数组
:param count: 个数
:return: count 个数
"""
if len(arr):
del arr[0]
return count_list(arr, count + 1)
else:
return count
print(count_list([1, 2]))
def find_max(arr, max=0):
""" 递归查找最大值
:param arr: 数组
:param max: 最大值
:return: max 最大值
"""
if len(arr):
if arr[0] > max:
max = arr[0]
del arr[0]
return find_max(arr, max)
else:
return max
print(find_max([1, 2, 123, 999, 123, 555, 666]))