1. 程式人生 > 其它 >輕鬆實現Rust系統入門,實戰編譯器開發完整無密網盤分享

輕鬆實現Rust系統入門,實戰編譯器開發完整無密網盤分享

###輕鬆實現Rust系統入門,實戰編譯器開發完整無密網盤分享

連結:https://pan.baidu.com/s/10gIsm6b97uUkN4JWDCtsTg
提取碼:r6w9
--來自百度網盤超級會員V4的分享

1。四位數字字母考證碼的生成例項

 1 import random    
 2 if __name__ =="__main__":    #四位數字字母考證碼的生成
 3     checkcode="" #保管考證碼的變數
 4     for i in range(4):
 5         index=random.randrange(0,4)  #生成一個0~3中的數
 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
13         return '對不起,沒有你想要找的問題'
14          
15 if __name__ =='__main__':
16     question=input('請輸入想要發問的內容:')
17     while True:
18         if question=='bye':
19             break
20         reply=find_answer(question)
21         if not reply:
22             question=input("小蜜不懂您在說什麼,您能夠問一些與訂單、賬戶和支付相關的內容(退出請輸入bye):")
23         else:
24             print(reply)
25             question=input("您能夠問一些與訂單、賬戶和支付相關的內容(退出請輸入bye):")
26     print('謝謝,再見!')
27         
4。求最大條約數和最小公倍數 (輾轉相除法)

最大條約數:指兩個或多個整數共有約數中最大的一個

最小公倍數:兩個或多個整數公有的倍數叫做它們的公倍數,其中除0以外最小的一個公倍數就叫做這幾個整數的最小公倍數

二者關係:兩個數之積=最小公倍數*最大條約數

 1 a=int(input('輸入數字1:'))
 2 b=int(input('輸入數字2:'))
 3 s=a*b
 4 while a%b!=0:
 5     a,b=b,(a%b)
 6     print(a)
 7     print(b)
 8 else:
 9     print(b,'is the maximum common divisor最大條約數')
10     print(s//b,'is the least common multiple,最小公倍數')

更相減損法

 1 a=int(input('please enter 1st num:'))
 2 b=int(input('please enter 2nd num:'))
 3 s=a*b
 4      
 5 while a!=b:
 6     if a>b:
 7         a-=b
 8     elif a<b:
 9         b-=a
10 else:
11     print(a,'is the maximum common divisor')
12     print(s//a,'is the least common multiple')
13  
14 #運轉結果
15 please enter 1st num:40
16 please enter 2nd num:60
17 20 is the maximum common divisor
18 120 is the least common multiple
5。判別能否為閏年 (輾轉相除法)
 1 # 判別能否為閏年
 2 while True:
 3     try:
 4         num=eval(input("請輸入一個年份:"))
 5     except:
 6         print('輸入錯誤年份')
 7         continue
 8     if (num %4==0 and num%100 !=0) or num %400==0:
 9         print(num,"是閏年")
10     else:
11         print(num,"不是閏年")
import calendar
year = int(input("請輸入年份:"))
check_year=calendar.isleap(year)
if check_year == True:
    print ("閏年")
else:
    print ("閏年")
6。Python統計字串中數字,字母,漢字的個數
 1 import re
 2 str_test='abcdefgHABC123456中華民族'
 3  
 4 #把正則表示式編譯成物件,假如經常運用該物件,此種方式可進步一定效率
 5 num_regex = re.compile(r'[0-9]')
 6 zimu_regex = re.compile(r'[a-zA-z]')
 7 hanzi_regex = re.compile(r'[\u4E00-\u9FA5]')
 8 
 9 print('輸入字串:',str_test)
10 #findall獲取字串中一切匹配的字元
11 num_list = num_regex.findall(str_test)
12 print('包含的數字:',num_list)
13 zimu_list = zimu_regex.findall(str_test)
14 print('包含的字母:',zimu_list)
15 hanzi_list = hanzi_regex.findall(str_test)
16 print('包含的漢字:',hanzi_list)

#羊車門問題

 1 import random as r
 2 
 3 #總次數
 4 total=1000000 #1000,1W,10W,100W
 5 #換與不換的獲勝次數
 6 win1=0
 7 win2=0
 8 
 9 for i in range(total):
10     #模仿選擇過程
11     man=r.randint(1,3)
12     car=r.randint(1,3)
13     #結果:一開端為車門,不換+1.
14     #       否則則一開端為羊門,換+1.
15     if man==car:
16         win1+=1
17     else:
18         win2+=1
19 
20 print("在{}次實驗中:".format(total))
21 print("若不更改門,獲勝概率為{:.3}%.".format((win1/total)*100))
22 print("若更改門,獲勝概率為{:.3}%.".format((win2/total)*100))
 1 import random
 2 x=random.randint(5000,10000)
 3 print(x)
 4 change=0
 5 nochange=0
 6 for i in range(1,x+1):
 7   a=random.randrange(1,4)
 8   b=random.randrange(1,4)
 9   if a==b:
10     nochange=nochange+1
11   else:
12     change=change+1
13 print("不更改組擇得到汽車的概率為{:.2f}".format(nochange/x))
14 
15 print("更改組擇得到汽車的概率為{:.2f}".format(change/x))