# 1. 编写函数 myfac(x) 计算x的阶乘x!
# 5! = 5 * 4 * 3 * 2 * 1
# print(myfac(5)) # 120
# 方法1用循环来实现
# def myfac(x):
# s = 1
# for i in range(2, x + 1):
# s *= i
# return s
# 方法2用递归来实现
def myfac(x):
if x == 1:
return 1
return x * myfac(x - 1)
print(myfac(5)) # 120
# 2. 写程序算出1~20的阶乘的和
# 1! + 2! + 3! + .... + 19! + 20!
# 思考:
# 能否用函数式编程中的高阶函数实现?
def myfac(x):
if x == 1:
return 1
return x * myfac(x - 1)
# def mysum(n):
# s = 0
# for i in range(1, n + 1):
# s += myfac(i)
# return s
def mysum(n):
return sum(map(myfac, range(1, n + 1)))
print(mysum(20))
# 3. 已知有列表:
# L = [[3,5,8], 10, [[13, 14], 15], 18]
# 1) 写一个函数 print_list(lst) 打印出列表中所有数字
# print_list(L)
# 2) 写一个函数sum_list(lst) 返回列表中所有数字的和
# print(sum_list(L)) # 86
# 注:
# type(x) 可以返回一个变量的类型
# 如:
# >>> type(20) is int # True
# >>> type([1,2,3]) is list # True
L = [[3, 5, 8], 10, [[13, 14], 15], 18]
# 1) 写一个函数 print_list(lst) 打印出列表中所有数字
def print_list(lst):
for x in lst:
if type(x) is list:
print_list(x)
else:
print(x)
# 2) 写一个函数sum_list(lst) 返回列表中所有数字的和
def sum_list(lst):
s = 0
for x in lst:
if type(x) is list:
s += sum_list(x)
else:
s += x
return s
print_list(L)
print('列表L的和是:', sum_list(L)) # 86