函数递归 - 递推与回溯 练习题

递归与二分法习题
二分法就是在按照从大到小或者从小到大规律排布的列表中,
寻找的值通过与中间的值比较大小,从而对列表进行操作,然后再比较的循环过程。

用递归的方法找出列表中的值
num = [1,3,4,5,6,8,22,33,55,778,990]
def search(search_number,num):
if len(num) == 0:return
mid = len(num) // 2
mid_nums = num[len(num)//2]
if search_number > mid_nums:
num = num[mid + 1:]
search(search_number,num)
elif search_number < mid_nums:
num = num[:mid]
search(search_number, num)
else:
print("find it")

search(363,num)

2.根据最后一个人的年龄,猜测第一个人的年龄

# def age(n): # 猜第五个人的年龄,传进参数5
# if n == 1: # 已知最后一个人的年龄 为26 (条件成立,递推结束后,开始回溯)
# return 26
# return age(n-1) + 2 # age(4) +2 ---》 age(3) + 2 ---> age(2) +2 --->age(1) +2
#
# print(age(5))

3.列表嵌套列表,用递归取得列表中的所有值。
l=[1,[2,[3,[4,[5,[6,[7,[8,[9,]]]]]]]]]

def tell(l):
for item in l:
if type(item) is list:
#继续进入下一层递归
tell(item)
else:
print(item)

tell(l)

# 使用递归打印斐波那契数列
# 0、1、1、2、3、5、8、13、21 # 0,1 1,1 1,2 2,3 3,5 5,8
# def fib(max):
# n,a,b = 0,0,1
# while n < max:
# print(b) #直接输出数列的值
# a,b = b,a+b #b赋值给a后,a = 1,a+b 赋值给b b还是一,因为赋值后的a并没有立即参与运算。
# n += 1
# print(fib(15))
# 0、1、1、2、3、5、8、13、21
# def func(num1,num2):
#
# res = num1+num2 # res = 1
# print(res)
# num1=num2
# num2 = res
# if res < 10000:
# func(num1,num2)
# else:
# return
#
# func(0,1)


# 2.一个嵌套很多层的列表,如l =[1, 2, [3, [4, 5, 6, [7, 8, [9, 10, [11, 12, 13, [14, 15]]]]]]],用递归取出所有的值
# l =[1,2,[3,[4,5,6,[7,8,[9,10,[11,12,13,[14,15]]]]]]]
# def tell(l):
# for item in l:
# if type(item) is list:
# tell(item)
# else:
# print(item)
# tell(l)


# 3. 编写用户登录装饰器,在登录成功后无需重新登录,同一账号重复输错三次密码则锁定5分钟
# import time
# user_status = {"user": None}
#
#
# def logging(func):
# def wrapper(*args,**kwargs):
# count = 0
# while True:
#
# if count == 3:
# print("用户名或密码错误,账户已被锁定")
# time.sleep(100)
# if user_status["user"]: # 这儿注意点
# res = func(*args, **kwargs)
# return res
# name = input("用户名>>: ").strip()
# pwd = input("密码>>: ").strip()
#
# if name == "egon" and pwd == "123":
# print("logging success")
# user_status["user"] = name
# res = func(*args,**kwargs)
# return res
# else:
# print("用户名或密码错误")
# count += 1
# return wrapper
#
#
# @logging
# def index(name):
# print("welcome {name} to my country".format(name = name))
# return 123
#
#
# @logging
# def auth(name):
# print("welcome {name} to my country".format(name = name))
# return 234
#
#
# index("egon")
# auth("luffei")


# 4、求文件a.txt中总共包含的字符个数?思考为何在第一次之后的n次sum求和得到的结果为0?(需要使用sum函数)
# with open("a.txt",encoding="utf-8") as f:
# res = [len(line) for line in f]
# print(sum(res))


# 5、文件shopping.txt内容如下
#
# mac,20000,3
# lenovo,3000,10
# tesla,1000000,10
# chicken,200,1
# 求总共花了多少钱?
#
# 打印出所有商品的信息,格式为[{'name':'xxx','price':333,'count':3},...]
#
# 求单价大于10000的商品信息,格式同上

db_file = "a.txt"

with open(db_file,encoding="utf-8") as f:
res = [{'name':line.strip("\n").split(",")[0],'price':line.strip("\n").split(",")[1],'count':line.strip("\n").split(",")[2]} for line in f]
print(res)

res2 = [item for item in res if int(item["price"])>10000]
print(res2)



转载于:https://www.cnblogs.com/Roc-Atlantis/p/9182813.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值