一、薪資文件
內容如下,標題爲:姓名,性別,年紀,薪資
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)