github展示python100題
鏈接如下:
https://github.com/zhiwehu/Python-programming-exercises/blob/master/100%2B%20Python%20challenging%20programming%20exercises.txt
以下爲博主翻譯後題目及解答,答案代碼分爲兩個,第一條爲博主個人解答(Python3),第二條爲題目所提供答案(Python2)
………………………………………………………………………………
題目1-20鏈接:https://blog.csdn.net/weixin_41744624/article/details/103426225
題目21-40鏈接:https://blog.csdn.net/weixin_41744624/article/details/103511139
題目41-60鏈接:https://blog.csdn.net/weixin_41744624/article/details/103575741
題目61-80鏈接:
https://blog.csdn.net/weixin_41744624/article/details/103607992
本部分爲題目81-98,難度1-3不定序~
經檢測題庫去除重複只有98題啦(歡迎評論添加好題目)~
………………………………………………………………………………
81、問題:
請編寫一個程序來壓縮和解壓縮字符串
“hello world!hello world!hello world!hello world!”.
(使用zlib.compress()和zlib.decompress()壓縮和解壓縮字符串,Python3記得轉換字符存儲方式)
import zlib
a='hello world!hello world!hello world!hello world!'
s=a.encode("utf-8")
zlib_s=zlib.compress(s)
print(zlib_s)
zlib_ds=zlib.decompress(zlib_s)
print(zlib_ds)
import zlib
s = 'hello world!hello world!hello world!hello world!'
t = zlib.compress(s)
print t
print zlib.decompress(t)
82、問題:
請編寫程序打印“1+1”執行100次的運行時間
import time
start = time.clock()
for i in range(0,100):
print("1+1")
end = time.clock()
print (end-start)
from timeit import Timer
t = Timer("for i in range(100):1+1")
print t.timeit()
83、問題:
請編寫一個程序來重新排列並打印列表[3,6,7,8]
import random
ls = [3,6,7,8]
ls2=random.shuffle(ls)
print (ls)
from random import shuffle
li = [3,6,7,8]
shuffle(li)
print li
84、問題:
請編寫一個程序,生成主語在“I”、“You”中,動詞在“Play”、“Love”中,賓語在“Hockey”、“Football”中的所有句子。
for i in ("I","You"):
for j in ("Play","Love"):
for k in ("Hockey","Football"):
print(i+j+k)
subjects=["I", "You"]
verbs=["Play", "Love"]
objects=["Hockey","Football"]
for i in range(len(subjects)):
for j in range(len(verbs)):
for k in range(len(objects)):
sentence = "%s %s %s." % (subjects[i], verbs[j], objects[k])
print sentence
85、問題:
打印[5,6,77,45,22,12,24]中的刪除偶數後的列表,請編寫程序打印列表。
ls = [5,6,77,45,22,12,24]
ls2 = []
for i in ls:
if i%2 !=0:
ls2.append(i)
print (ls2)
li = [5,6,77,45,22,12,24]
li = [x for x in li if x%2!=0]
print li
86、問題:
使用列表的理解,刪除[12,24,35,70,88,120,155]中可被5和7整除的,刪除數後,請編寫程序打印列表。
ls = [12,24,35,70,88,120,155]
ls2 = []
for i in ls:
if (i%7 !=0 and i%5 !=0):
ls2.append(i)
print (ls2)
li = [12,24,35,70,88,120,155]
li = [x for x in li if x%5!=0 and x%7!=0]
print li
87、問題:
使用列表理解功能,刪除[12,24,35,70,88,120,155].中的第0、2、4、6個數字後,請編寫程序打印列表。
ls = [12,24,35,70,88,120,155]
ls2 = []
for i in range(0,len(ls)):
if i in (0,2,4,6):
a=ls[i]
ls2.append(a)
print (ls2)
li = [12,24,35,70,88,120,155]
li = [x for (i,x) in enumerate(li) if i%2!=0]
print li
88、問題:
通過使用列表理解,請編寫一個程序生成一個358的三維數組,其每個元素爲0。
num_list=[[[0]*3 for i in range(5)] for i in range (8)]
print (num_list)
array = [[ [0 for col in range(8)] for col in range(5)] for row in range(3)]
print array
89、問題:
使用列表理解,請在刪除[12,24,35,70,88,120,155]中的第0、4、5個數字後編寫程序打印列表。
ls = [12,24,35,70,88,120,155]
ls2 = []
for i in range(0,len(ls)):
if i not in (0,4,5):
a=ls[i]
ls2.append(a)
print (ls2)
li = [12,24,35,70,88,120,155]
li = [x for (i,x) in enumerate(li) if i not in (0,4,5)]
print li
90、問題:
使用列表理解,請在刪除[12,24,35,24,88,120,155]中的值24後,編寫程序打印列表。
ls = [12,24,35,24,88,120,155]
ls2 = []
for i in ls:
if int(i) == 24:
pass
else:
ls2.append(i)
print (ls2)
li = [12,24,35,24,88,120,155]
li = [x for x in li if x!=24]
print li
91、問題:
使用兩個給定列表[1,3,6,78,35,55]和[12,24,35,24,88,120,155],編寫一個程序來生成一個元素與上述列表相交的列表。
ls = [1,3,6,78,35,55]
ls2 = [12,24,35,24,88,120,155]
ls3 = []
for i in ls:
for j in ls2:
if i==j:
ls3.append(i)
print (ls3)
set1=set([1,3,6,78,35,55])
set2=set([12,24,35,24,88,120,155])
set1 &= set2
li=list(set1)
print li
92、問題:
對於給定的列表[12,24,35,24,88,120,155,88,120,155],在刪除保留原始順序的所有重複值後,編寫程序打印此列表。
ls = [12,24,35,24,88,120,155,88,120,155]
ls2 = set(ls)
ls3 = sorted(ls2,key=ls.index)
print (ls3)
def removeDuplicate( li ):
newli=[]
seen = set()
for item in li:
if item not in seen:
seen.add( item )
newli.append(item)
return newli
li=[12,24,35,24,88,120,155,88,120,155]
print removeDuplicate(li)
93、問題:
定義一個類人及其兩個子類:男性和女性。所有班級都有一個“getGender”方法,可以爲男性類打印“男性”,爲女性類打印“女性”。
class person():
def getGender(self):
pass
class male(person):
def getGender(self):
print ("male")
class female(person):
def getGender(self):
print ("female")
x=person()
x.getGender
a=male()
b=female()
a.getGender()
b.getGender()
class Person(object):
def getGender( self ):
return "Unknown"
class Male( Person ):
def getGender( self ):
return "Male"
class Female( Person ):
def getGender( self ):
return "Female"
aMale = Male()
aFemale= Female()
print aMale.getGender()
print aFemale.getGender()
94、問題:
請編寫一個程序,計算並打印控制檯輸入的字符串中每個字符的個數。
例子:
如果將以下字符串作爲程序的輸入:
abcdefgabc
那麼,程序的輸出應該是:
a,2
c,2
b,2
e,1
d,1
g,1
f,1
(大致同22題)
ls = input ("please input:")
digit = []
digitchar = []
alpha = []
for i in ls:
if i.isalpha():
alpha.append(i)
elif i.isdigit():
digit.append(i)
else:
digitchar.append(i)
x = sorted(set (alpha))
for z in x:
print (str(z)+":"+str(alpha.count(z)))
dic = {}
s=raw_input()
for s in s:
dic[s] = dic.get(s,0)+1
print '\n'.join(['%s,%s' % (k, v) for k, v in dic.items()])
95、問題:
請編寫一個從控制檯接收字符串並按相反順序打印的程序
ls = input ("please input:")
ls2 = []
n = 0
for i in range(0,len(ls)):
ls2.append(ls[len(ls)-i-1])
print (ls2)
s=raw_input()
s = s[::-1]
print s
96、問題:
請編寫一個從控制檯接受字符串的程序,並打印具有偶數索引的字符。
例子:
如果將以下字符串作爲程序的輸入:
H1e2l3l4o5w6o7r8l9d
那麼,程序的輸出應該是:
Helloworld
ls = input ("please input:")
ls2 = []
for i in ls:
if ls.index(i)%2 == 0:
ls2.append(i)
print (ls2)
s=raw_input()
s = s[::2]
print s
97、問題:
請編寫一個程序,打印[1,2,3]的所有排列組合
(使用itertools.permutations()獲取列表的排列)
import itertools
ls =[1,2,3]
ls2=itertools.permutations(ls)
for item in ls2:
print (item)
import itertools
print list(itertools.permutations([1,2,3]))
98、問題:
編寫一個程序來解決一個經典的中國古代難題:
我們在一個農場的雞和兔子中間數了35頭94腿。我們有多少隻兔子和多少隻雞?
x=35
y=94
for i in range(x):
if (i*4+(x-i)*2 == y):
print ("雞:"+str(x-i)+"兔:"+str(i))
def solve(numheads,numlegs):
ns='No solutions!'
for i in range(numheads+1):
j=numheads-i
if 2*i+4*j==numlegs:
return i,j
return ns,ns
numheads=35
numlegs=94
solutions=solve(numheads,numlegs)
print (solutions)