日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

Python程序每日一练习

發(fā)布時(shí)間:2023/11/29 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python程序每日一练习 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

問題一:做為Apple Store App獨(dú)立開發(fā)者,你要搞限時(shí)促銷,為你的應(yīng)用生成激活碼(或者優(yōu)惠券),使用Python如何生成200個(gè)激活碼(或者優(yōu)惠券)?

簡介:通用唯一識(shí)別碼(英語:Universally Unique Identifier,簡稱UUID)是一種軟件建構(gòu)的標(biāo)準(zhǔn),亦為開放軟件基金會(huì)組織在分散式計(jì)算環(huán)境領(lǐng)域的一部份。

UUID的目的,是讓分散式系統(tǒng)中的所有元素,都能有唯一的辨識(shí)資訊,而不需要透過中央控制端來做辨識(shí)資訊的指定。如此一來,每個(gè)人都可以建立不與其它人沖突的UUID。在這樣的情況下,就不需考慮資料庫建立時(shí)的名稱重復(fù)問題。目前最廣泛應(yīng)用的UUID,是微軟公司的全局唯一標(biāo)識(shí)符(GUID),而其他重要的應(yīng)用,則有Linux ext2/ext3檔案系統(tǒng)、LUKS加密分區(qū)、GNOME、KDE、Mac OS X等等。另外我們也可以在e2fsprogs套件中的UUID函式庫找到實(shí)現(xiàn)。

分析這里參考(http://www.blogjava.net/BearRui/archive/2010/10/19/unique_random_code.html)

主鍵+隨機(jī)碼的方式.

這種方法優(yōu)點(diǎn):使用也比較簡單,不用直接去查詢數(shù)據(jù)庫,而最大的優(yōu)點(diǎn)是查詢的時(shí)候,可以根據(jù)邀請(qǐng)碼直接得到主鍵id, 然后根據(jù)id去數(shù)據(jù)庫查詢(速度很快),再比較查詢出來的邀請(qǐng)碼和用戶提交的邀請(qǐng)碼是否一致。

  • 生成:id(數(shù)據(jù)庫primary key )->16進(jìn)制 + "L(標(biāo)識(shí)符)" +隨機(jī)碼
  • 獲取id:獲取16進(jìn)制的id再轉(zhuǎn)回10進(jìn)制
  • import random import stringdef activation_code(id,length = 10):'''id+L+隨機(jī)碼string模塊中的三個(gè)函數(shù)為:string.letters,string.printable.string.printable'''prefix = hex(int(id))[2:]+'L' #prefix為前綴length =length -len(prefix)chars = string.ascii_letters+string.digitsreturn prefix + ''.join([random.choice(chars) for i in range(length)])def get_id(code):'''hex to dec'''return str(int(code.upper(),16))if __name__ =="__mian__":for i in range(10,500,35):code = activation_code(i)id_hex = code.split('L')[0]id = get_id(id_hex)print (code,id)if __name__=="__main__":for i in range(10,200,35):code = activation_code(i)id_hex = code.split('L')[0]id = get_id(id_hex)print (code,id)#print(code)

    問題二:任一個(gè)英文的純文本文件,統(tǒng)計(jì)其中的單詞出現(xiàn)的個(gè)數(shù)

    1.strip()沒有參數(shù)時(shí),刪除空白符,包括、n\r\t空格,strip()函數(shù)只能用于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 寫一個(gè)爬圖片的程序

    ? ?這個(gè)就是一個(gè)簡單的爬蟲,只要模擬瀏覽器即可

    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) + "次爬去成功")

    問題四:一個(gè)HTML文件,找出里面的正文

    問題五:有個(gè)目錄,里面是你自己寫過的程序,統(tǒng)計(jì)一下你寫過多少行代碼。包括空行和注釋,但是要分別列出來。

    ?

    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: #計(jì)算注釋數(shù)目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個(gè)數(shù)字,能組成多少個(gè)互不相同且無重復(fù)數(shù)字的三位數(shù)?都是多少?

    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)

    問題七:

    企業(yè)發(fā)放的獎(jiǎng)金根據(jù)利潤提成。
    利潤(I)低于或等于10萬元時(shí),獎(jiǎng)金可提10%;
    利潤高于10萬元,低于20萬元時(shí),低于10萬元的部分按10%提成,高于10萬元的部分,可可提成7.5%;
    20萬到40萬之間時(shí),高于20萬元的部分,可提成5%;
    40萬到60萬之間時(shí)高于40萬元的部分,可提成3%;
    60萬到100萬之間時(shí),高于60萬元的部分,可提成1.5%,
    高于100萬元時(shí),超過100萬元的部分按1%提成,
    從鍵盤輸入當(dāng)月利潤I,求應(yīng)發(fā)放獎(jiǎng)金總數(shù)?

    ?

    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("請(qǐng)輸入當(dāng)月利潤:"))print(reward(profit))

    ?問題八:一個(gè)整數(shù),
    它加上100后是一個(gè)完全平方數(shù),再加上168又是一個(gè)完全平方數(shù),
    請(qǐng)問該數(shù)是多少?

    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)

    ?

    ?

    未完待續(xù),有時(shí)間會(huì)繼續(xù)上傳,http://www.cnblogs.com/bakoom/p/5251293.html

    轉(zhuǎn)載于:https://www.cnblogs.com/wj-1314/p/7487040.html

    總結(jié)

    以上是生活随笔為你收集整理的Python程序每日一练习的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。