• 函数递归


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

    用递归的方法找出列表中的值
    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(" ").split(",")[0],'price':line.strip(" ").split(",")[1],'count':line.strip(" ").split(",")[2]} for line in f]
    print(res)

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



  • 相关阅读:
    Spring Boot 配置加载顺序详解
    JVM总结篇
    nginx负载均衡的策略
    布隆过滤器的方式解决缓存穿透问题
    Spring Cloud Eureka自我保护机制(服务无法剔除)
    缓存穿透,缓存击穿,缓存雪崩解决方案分析
    高并发秒杀系统总结
    Linux环境进程间通信(一)
    HDU 1695 GCD(容斥定理)
    数据结构精要------冒泡与直接排序算法
  • 原文地址:https://www.cnblogs.com/Roc-Atlantis/p/9182813.html
Copyright © 2020-2023  润新知