作业:
1 使用递归打印斐波那契数列(前两个数的和得到第三个数,如:0 1 1 2 3 4 7...)
#递归
def fib(a,b,stop):
if a > stop:
return
print(a,end=' ')
fib(b,a+b,stop)
fib(0,1,100)
#非递归
def fib(n):
a,b=0,1
while a < n:
print(a,end=' ')
a,b=b,a+b
print()
fib(10)
View Code
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)
View Code
3 编写用户登录装饰器,在登录成功后无需重新登录,同一账号重复输错三次密码则锁定5分钟
import time
import sys
current_user = {'user':None}
def auth(func):
def wrapper(*args,**kwargs):
if current_user['user']:
res = func(*args,**kwargs)
return res
tag = True
count = 0
while tag:
if count > 2:
lock_time = time.time()
with open('db_lock','at',encoding='utf-8') as f:
f.write('%s:%s
' %(name_inp,lock_time)) #db_lock egon:时间戳
print('超过3次,锁定5分钟')
sys.exit()
# while True:
name_inp = input('username>>: ').strip() #用户名是否存在不验证,输入存在的用户名
current_time = time.time()
#判断是否在锁定中 1 在 判断是否超过5分钟 超过重新输入 不超过退出 2 不在 锁定文件
with open('db_lock',encoding='utf-8') as f2:
for line in f2: #是否锁定 遍历完都没有匹配到 则没锁定
line = line.strip('
').split(':')
if name_inp == line[0]: #是否超过5分钟
if current_time - float(line[1]) > 300:
# print('锁定解除')
break
else:
print('锁定中剩余时间:%s秒' %(300 - (current_time - float(line[1]))))
sys.exit()
else:
pass
pwd_inp = input('password>>: ')
with open('db',encoding='utf-8') as f1: #db egon:123
for line in f1:
line = line.strip('
').split(':')
if name_inp == line[0] and pwd_inp == line[1]:
current_user['user'] = name_inp
print('验证成功')
res = func(*args,**kwargs)
return res
else:
print('用户名或密码错误')
count += 1
return wrapper
@auth
def index():
print('hello world')
@auth
def home(name):
print('welcome %s to home page' %name)
index()
home('alex')
View Code
4、求文件a.txt中总共包含的字符个数?思考为何在第一次之后的n次sum求和得到的结果为0?(需要使用sum函数)
with open('a.txt',encoding='utf-8') as f:
g=(len(line) for line in f) #生成器
print(sum(g))
print(sum(g))
print(sum(g))
生成器的next方法是从头到尾一次性的
View Code
5、文件shopping.txt内容如下
mac,20000,3
lenovo,3000,10
tesla,1000000,10
chicken,200,1
求总共花了多少钱?
打印出所有商品的信息,格式为[{'name':'xxx','price':333,'count':3},...]
求单价大于10000的商品信息,格式同上
常规法
total = 0
with open('shopping.txt',encoding='utf-8') as f:
for line in f:
line = line.strip('
').split(',')
line_monny = int(line[1]) * int(line[2])
total += line_monny
print('total:%s' %total)
生成器方法
with open('shopping.txt',encoding='utf-8') as f:
g=(int(line.strip('
').split(',')[1]) * int(line.strip('
').split(',')[2]) for line in f)
print(sum(g))
shopping_info = []
with open('shopping.txt',encoding='utf-8') as f:
for line in f:
line = line.strip('
').split(',')
keys = ['name','price','count']
d = {k:v for k,v in zip(keys,line)} #字典生成式
shopping_info.append(d)
print(shopping_info)
shopping_info_10000 = []
with open('shopping.txt',encoding='utf-8') as f:
for line in f:
line = line.strip('
').split(',')
if int(line[1]) > 10000:
keys = ['name','name','count']
d = {k:v for k,v in zip(keys,line)}
shopping_info_10000.append(d)
print(shopping_info_10000)
View Code
明日默写:
二分查找
nums=[1,3,7,11,22,34,55,78,111,115,137,149,246,371]
def search(search_num,nums):
print(nums)
if len(nums) == 0:
print('not exists')
return
mid_index = len(nums) // 2
if search_num > nums[mid_index]:
nums = nums[mid_index+1:]
search(search_num,nums)
elif search_num < nums[mid_index]:
nums = nums[:mid_index-1]
search(search_num,nums)
else:
print('exist')
search(149,nums)
View Code
预习:模块与包的使用http://www.cnblogs.com/linhaifeng/articles/6379069.html