一、薪资文件
内容如下,标题为:姓名,性别,年纪,薪资
albert male 18 3000
james male 38 30000
林玲 female 28 20000
结衣 female 28 10000
要求从文件中取出每一条记录放入列表中,列表中的每个元素都是如下格式:
{'name':'albert','sex':'male','age:18,'salary':3000}
f = open('info.txt', 'r')
list_info = []
for line in f:
data_info = line.split(' ')
name = data_info[0]
sex = data_info[1]
age = data_info[2]
salary = data_info[3]
people = {'name': name, 'sex': sex, 'age': int(age), 'salary': int(salary)}
list_info.append(people)
f.close()
print(list_info)
二、根据题一,求薪资最高
# 方式一 :手写算法
def get_the_richest(list1):
richest = list1[0]
for i in range(len(list1)):
if list1[i]['salary'] > richest['salary']:
richest = list1[i]
return richest
# 方式二:使用内置函数max与lambda组合
print('内置函数', max(list_info, key=lambda x: x['salary']))
三、将名字首字母改为大写
def change_capitalize(list1):
for i in range(len(list1)):
if 'a' <= list1[i]['name'][0] <= 'z':
list1[i]['name'] = list1[i]['name'].capitalize()
return list1
res1 = change_capitalize(list_info)
print(res1)
# need to create redundant joint
res2 = map(lambda x: x['name'].capitalize(), list_info)
print(list(res2))
四、过滤掉以a开头的名字信息
def filter_a(list1):
list2 = []
for i in range(len(list1)):
if list1[i]['name'][0] != 'a':
list2.append(list1[i])
return list2
print(list(filter(lambda x: not x['name'].startswith('a'), list_info)))
五、用递归打印斐波那契数列
def Fibonacci(n):
if n < 1:
return
elif n == 1:
return 1
elif n == 2:
return 1
else:
return Fibonacci(n - 1) + Fibonacci(n - 2)
for i in range(1, 20):
print(Fibonacci(i))
六、使用random模块,随机生成8位验证码,随机大小写字母和数字
import random
def get_verification():
verification_code = ''
for i in range(8):
alpha1 = chr(random.randint(65, 90))
alpha2 = chr(random.randint(97, 122))
number = random.randint(0, 9)
verification_code += random.choice([alpha1, alpha2, str(number)])
return verification_code
print((get_verification()))
七、写一个模拟撞库的程序,假如密码都是用md5加密的,撞库就是用多个猜测的密码尝试比对正确的密码,比对过程一定是用md5来进行的。
import random
import hashlib
def get_password_violently():
while True:
password = ''
# imagine password is formed by 4 numbers or alphas
for i in range(4):
random_alpha = chr(random.randint(65, 90))
random_number = random.randint(0, 9)
password += random.choice([random_alpha, str(random_number)])
# md5 processing...
m = hashlib.md5()
m.update(password.encode('utf-8'))
md5_password = m.hexdigest()
all_alpha_number = list(map(lambda x: chr(x), range(48, 122)))
# simulate user input password
for a in all_alpha_number:
for b in all_alpha_number:
for c in all_alpha_number:
for d in all_alpha_number:
input_password = a + b + c + d
m = hashlib.md5()
m.update(input_password.encode('utf-8'))
input_md5_password = m.hexdigest()
if input_md5_password == md5_password:
return password
res = get_password_violently()
print("user's password", res)
八、使用re模块写一个验证手机号码是否有效的程序
首先准备一个通讯录文件,内容为:
姓名 地区 身高 体重 电话
况咏蜜 北京 171 48 11151054608
王欣妍 上海 169 46 137234523
…
import re
def get_model_phone(file):
invalid_data = {}
with open(file, 'r', encoding='utf-8') as f:
for line in f:
model_data_list = line.split()
name = model_data_list[0]
phone = model_data_list[-1]
valid_phone = re.findall('13\d\d\d\d\d\d\d\d\d', phone)
if valid_phone:
invalid_data[name] = valid_phone[0]
return invalid_data
res = get_model_phone('info2.txt')
print(res)