玩转算法面试-- Leetcode真题分门别类讲解

130 阅读2分钟

download:玩转算法面试-- Leetcode真题分门别类讲解

相信这是一门非常及时的课程,送给面试在即的你,学完这门课程,对于面试中遇到的大多数算法问题,你都会迎刃而解,但课程绝不止于面试,同样适合即将参加各类算法竞赛的同学,重要的是提升你的算法思维,这将是贯穿你编程生涯的核心内功!

适合人群及技术储备要求
适合具备基本编程思想,了解C++基本语法,最好你已经学习过另一门实战课程《算法与数据结构》,或者对基础数据结构已经有了解,在此基础上,想提升算法设计能力的同学,那这门课程非常适合你

import random
2 if name =="main": #四位數字字母考證码的生成
3 checkcode="" #保管考證码的變量
4 for i in range(4):
5 index=random.randrange(0,4) #生成一個03中的數
6 if index!=i and index +1 !=i:
7 checkcode +=chr(random.randint(97,122)) # 生成a
z中的一個小寫字母
8 elif index +1==i:
9 checkcode +=chr(random.randint(65,90) ) # 生成A~Z中的一個大寫字母
10 else:
11 checkcode +=str(random.randint(1,9)) # 數字1-9
12 print(checkcode)
復製代码
輸出爲:m47A、8wQ9、vugS

2。格式化時間函數
1 def formatTime(longtime):
2 '''格式化時間的函數'''
3 import time
4 return time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(longtime))

3。記载顯現登錄日誌實例

復製代码
import time
def show_info():
print('''輸入提示數字,執行相應操作
0:退出
1:查看登錄日誌
''')
def write_loginfo(username):
"""
將用戶名和登錄時間寫入日誌
:param username: 用戶名
"""
with open('log.txt','a') as f:
string = "用戶名:{} 登錄時間:{}\n".format(username ,time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
f.write(string)
def read_loginfo():
"""
讀取日誌
"""
with open('log.txt','r') as f:
while True:
line = f.readline()
if line == '':
break # 跳出循環
print(line) # 輸出一行内容
if name == "main":
輸入用戶名

username = input('請輸入用戶名:')

檢測用戶名

while len(username) < 2 :
print('用戶名長度應不少於2位')
username = input('請輸入用戶名:')

輸入密码

password = input('請輸入密码:')

檢測密码

while len(passw ord) < 6 :
print('密码長度應不少於6位')
password = input('請輸入密码:')
print('登錄勝利')
write_loginfo(username) # 寫入日誌
show_info() # 提示信息
num = int(input('輸入操作數字:')) # 輸入數字
while True:
if num == 0:
print('退出勝利')
break
elif num == 1:
print('查看登錄日誌')
read_loginfo()
show_info()
num = int(input('輸入操作數字:'))
else:
print('您輸入的數字有誤')
show_info()
num = int(input('輸入操作數字:'))

3。模仿淘寶客服自動回復
復製代码
1 # 任務2:模仿淘寶客服自動回復
2
3 def find_answer(question):
4 with open('reply.txt','r') as f :
5 while True:
6 line=f.readline()
7 if not line: #也能夠爲if line==''
8 break
9 keyword=line.split('|')[0]
10 reply=line.split('|')[1]
11 if keyword in question:
12 return reply