Python程序每日一练习
生活随笔
收集整理的這篇文章主要介紹了
Python程序每日一练习
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
問題一:做為Apple Store App獨立開發者,你要搞限時促銷,為你的應用生成激活碼(或者優惠券),使用Python如何生成200個激活碼(或者優惠券)?
簡介:通用唯一識別碼(英語:Universally Unique Identifier,簡稱UUID)是一種軟件建構的標準,亦為開放軟件基金會組織在分散式計算環境領域的一部份。
UUID的目的,是讓分散式系統中的所有元素,都能有唯一的辨識資訊,而不需要透過中央控制端來做辨識資訊的指定。如此一來,每個人都可以建立不與其它人沖突的UUID。在這樣的情況下,就不需考慮資料庫建立時的名稱重復問題。目前最廣泛應用的UUID,是微軟公司的全局唯一標識符(GUID),而其他重要的應用,則有Linux ext2/ext3檔案系統、LUKS加密分區、GNOME、KDE、Mac OS X等等。另外我們也可以在e2fsprogs套件中的UUID函式庫找到實現。分析:這里參考(http://www.blogjava.net/BearRui/archive/2010/10/19/unique_random_code.html)
主鍵+隨機碼的方式.
這種方法優點:使用也比較簡單,不用直接去查詢數據庫,而最大的優點是查詢的時候,可以根據邀請碼直接得到主鍵id, 然后根據id去數據庫查詢(速度很快),再比較查詢出來的邀請碼和用戶提交的邀請碼是否一致。
問題二:任一個英文的純文本文件,統計其中的單詞出現的個數
1.strip()沒有參數時,刪除空白符,包括、n\r\t空格,strip()函數只能用于str類型,list類型等不可用。
2.split()用于分割,分隔符可以自己制定
def world_count(inputfile):if os.path.isfile(inputfile) !=True:print("inputfile not exits")sys.exit()word_count = 0words = open(inputfile , "r").readlines()for word in words:print("word: %s" %word)temp = word.strip().split('')word_count += len(temp)print("word count:%s" %word_count)return word_count?問題三:用 Python 寫一個爬圖片的程序
? ?這個就是一個簡單的爬蟲,只要模擬瀏覽器即可
import urllib.request import reurl = 'http://tieba.baidu.com/p/2166231880' headers = ("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36") opener = urllib.request.build_opener() opener.assheaders = [headers] urllib.request.install_opener(opener) data = urllib.request.urlopen(url).read() data2 = data.decode("utf-8","ignore") pattern = '<img pic_type="0" class="BDE_Image" src="(.*?)" bdwater="杉本有美吧,.*?" width=".*?" height=".*?" changedsize="true">' allurl = re.compile(pattern).findall(data2) #print(allurl)for i in range(0,len(allurl)):#print(allurl[i])thisimg = allurl[i]file = "D:/pycode/"+str(i)+".jpg"urllib.request.urlretrieve(thisimg,filename = file)print("第" + str(i) + "次爬去成功")問題四:一個HTML文件,找出里面的正文
問題五:有個目錄,里面是你自己寫過的程序,統計一下你寫過多少行代碼。包括空行和注釋,但是要分別列出來。
?
import os import string import reos.chdir('C:/workspace')fh=open('test_test.py') read_fh=fh.readlines() fh.close() number_code=0 number_empty=0 number_note=0 pattern='.*#' #正則匹配模式for x in read_fh:if '#' in x: #計算注釋數目if re.findall(pattern,x)[0][:-1].isspace() or re.findall(pattern,x)[0][:-1]=='':number_note+=1else:number_code+=1elif x.isspace():number_empty+=1else:number_code+=1 print('code number is %d'%(number_code+number_empty+number_note)) print('empty number is %d'%number_empty) print('note number is %d'%number_note)?問題六:有1、2、3、4個數字,能組成多少個互不相同且無重復數字的三位數?都是多少?
d = [1,2,3,4] def threenums():print(None)count = 0nums = []for index1 in range(1,5):for index2 in range(1,5):for index3 in range(1,5):if index1 != index2 and index2 != index3 and index3 !=index1:num = 100*index1 +10*index2 +index3if num not in nums:nums.append(num)count +=1print(count)print(nums)問題七:
企業發放的獎金根據利潤提成。
利潤(I)低于或等于10萬元時,獎金可提10%;
利潤高于10萬元,低于20萬元時,低于10萬元的部分按10%提成,高于10萬元的部分,可可提成7.5%;
20萬到40萬之間時,高于20萬元的部分,可提成5%;
40萬到60萬之間時高于40萬元的部分,可提成3%;
60萬到100萬之間時,高于60萬元的部分,可提成1.5%,
高于100萬元時,超過100萬元的部分按1%提成,
從鍵盤輸入當月利潤I,求應發放獎金總數?
?
def reward(profit):reward = 0.0if profit <=100000:return profit*0.1elif profit <=20 and profit >10:return (profit-10000)*0.075+100000*0.1elif profit <=40 and profit >20:return (profit-10000)*0.05+100000*0.1+10000*0.075elif profit <=60 and profit >40:return (profit-10000)*0.03+100000*0.1+10000*0.075+100000*0.05elif profit <=100 and profit >60:return (profit-10000)*0.015+100000*0.1+10000*0.075+100000*0.05+100000*0.03else:return (profit-10000)*0.01+100000*0.1+10000*0.075+100000*0.05+100000*0.03+100000*0.015if __name__ == "__mian__":profit = int(input("請輸入當月利潤:"))print(reward(profit))?問題八:一個整數,
它加上100后是一個完全平方數,再加上168又是一個完全平方數,
請問該數是多少?
import mathfor i in range(10000):x = int(math.sqrt(i+100))y = int(math.sqrt(i+168))if (x*x == i+100) and (y*y == i+168):print(i) ?
?
(未完待續,有時間會繼續上傳,http://www.cnblogs.com/bakoom/p/5251293.html)
轉載于:https://www.cnblogs.com/wj-1314/p/7487040.html
總結
以上是生活随笔為你收集整理的Python程序每日一练习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到鱼竿丢了是什么意思
- 下一篇: 梦到亲人生病怎么回事