1.递归实现斐波那契数列
一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第30位数是多少, 用递归算法实现。
# coding=utf-8 __author__ = 'Wang' def foo(n): if n == 1 or n == 2: return 1 else: return foo(n-1) + foo(n-2) a = foo(30) print a
2.递归实现整数逆序
将一整数逆序后放入一数组中(要求递归实现) Ex : 1234 变为 {4,3,2,1}
# coding=utf-8 __author__ = 'Wang' list_b = [] def convert(a): if a >= 10: list_item = a % 10 list_b.append(list_item) convert(a/10) elif a < 10 and a >= 0: list_b.append(a) else: print "input error" convert(234568) print list_b
3.递归实现遍历全部目录找出所有文件
all_file_list = [] all_dir_list = [] def find_all_dir_file(path): # path = os.path.abspath(path) path_list = os.listdir(path) for item in path_list: if os.path.isdir(item): dir_path = os.path.join(path, item) find_all_dir_file(dir_path) else: all_file_list.append(item) return all_dir_list, all_file_list a, b = find_all_dir_file('E:\\Project') print a print b