摘要:刷題繼續(xù)昨天和大家分享了題,今天繼續(xù)來刷題解法一解法二解法一解法一解法二解法一解法一解法二解法一解法一解法二解法一解法二解法一解法二解法三解法一解法二源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點擊以下鏈接下
刷題繼續(xù)
昨天和大家分享了61-70題,今天繼續(xù)來刷71~80題
Question 71:Please write a program to output a random number, which is divisible by 5 and 7, between 10 and 150 inclusive using random module and list comprehension.
import random print (random.choice([i for i in range(10,151) if i%5==0 and i%7==0]))解法二
import random resp = [i for i in range(10,151) if i % 35 == 0 ] print(random.choice(resp))Question 72:
Please write a program to generate a list with 5 random numbers between 100 and 200 inclusive.
import random resp = random.sample(range(100,201),5) print(resp)Question 73:
Please write a program to randomly generate a list with 5 even numbers between 100 and 200 inclusive.
import random numbers = random.sample(range(100,201,2),5) print(numbers)解法二
import random print (random.sample([i for i in range(100,201) if i%2==0], 5))Question 74:
Please write a program to randomly generate a list with 5 numbers, which are divisible by 5 and 7 , between 1 and 1000 inclusive.
import random lst = [i for i in range(1,1001) if i%35 == 0] resp = random.sample(lst,5) print(resp)Question 75:
Please write a program to randomly print a integer number between 7 and 15 inclusive.
import random number= random.choice([x for x in range(7,16)]) print(number)解法二
import random print(random.randrange(7,16))Question 76:
Please write a program to compress and decompress the string "hello world!hello world!hello world!hello world!".
import zlib s = "hello world!hello world!hello world!hello world!".encode() t = zlib.compress(s) print(t) print(zlib.decompress(t))Question 77:
Please write a program to print the running time of execution of "1+1" for 100 times.
from timeit import Timer t = Timer("for i in range(100):1+1") t.timeit()解法二
import time before = time.time() for i in range(100): x = 1 + 1 after = time.time() execution_time = after - before print(execution_time)Question 78:
Please write a program to shuffle and print the list [3,6,7,8].
import random lst = [3,6,7,8] random.shuffle(lst) print(lst)解法二
import random lst = [3,6,7,8] seed = 7 random.Random(seed).shuffle(lst) print(lst)Question 79:
Please write a program to generate all sentences where subject is in ["I", "You"] and verb is in ["Play", "Love"] and the object is in ["Hockey","Football"].
subjects=["I", "You"] verbs=["Play", "Love"] objects=["Hockey","Football"] res = [[i, j, k] for i in subjects for j in verbs for k in objects] for x in res: print(" ".join(x)) Out: I Play Hockey I Play Football I Love Hockey I Love Football You Play Hockey You Play Football You Love Hockey You Love Football解法二
subjects=["I", "You"] verbs=["Play", "Love"] objects=["Hockey","Football"] for sub in subjects: for verb in verbs: for obj in objects: print("{} {} {}".format(sub,verb,obj)) Out: I Play Hockey I Play Football I Love Hockey I Love Football You Play Hockey You Play Football You Love Hockey You Love Football解法三
import itertools subjects=["I", "You"] verbs=["Play", "Love"] objects=["Hockey","Football"] all_list = [subjects,verbs,objects] res = list(itertools.product(*all_list)) for x in res: print(" ".join(x)) Out: I Play Hockey I Play Football I Love Hockey I Love Football You Play Hockey You Play Football You Love Hockey You Love FootballQuestion 80:
Please write a program to print the list after removing even numbers in [5,6,77,45,22,12,24].
def isEven(n): return n%2!=0 li = [5,6,77,45,22,12,24] lst = list(filter(isEven,li)) print(lst)解法二
li = [5,6,77,45,22,12,24] lst = list(filter(lambda n:n%2!=0,li)) print(lst)源代碼下載
這十道題的代碼在我的github上,如果大家想看一下每道題的輸出結(jié)果,可以點擊以下鏈接下載:
Python 71 - 80題
我的運行環(huán)境Python 3.6+,如果你用的是Python 2.7版本,絕大多數(shù)不同就體現(xiàn)在以下3點:
raw_input()在Python3中是input()
print需要加括號
fstring可以換成.format(),或者%s,%d
謝謝大家,我們下期見!希望各位朋友不要吝嗇,把每道題的更高效的解法寫在評論里,我們一起進步!!!
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/43959.html
摘要:刷題繼續(xù)昨天和大家分享了題,今天繼續(xù)來刷題解法一解法一解法二解法一解法一解法一解法一解法二解法一解法二解法一解法二解法三解法一解法一解法二源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點擊以下鏈接下載題我的運 刷題繼續(xù) 昨天和大家分享了71-80題,今天繼續(xù)來刷81~90題 Question 81: By using list comprehension, p...
摘要:笨辦法學(xué)第版結(jié)構(gòu)非常簡單,共包括個習(xí)題,其中個覆蓋了輸入輸出變量和函數(shù)三個主題,另外個覆蓋了一些比較高級的話題,如條件判斷循環(huán)類和對象代碼測試及項目的實現(xiàn)等。最后只想說,學(xué)習(xí)不會辜負任何人,笨辦法學(xué) 內(nèi)容簡介 《笨辦法學(xué)Python(第3版)》是一本Python入門書籍,適合對計...
摘要:刷題繼續(xù)昨天和大家分享了題,今天繼續(xù)來刷題解法一解法二解法一解法二解法一解法一解法一解法一解法一解法一解法二解法一解法二解法一源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點擊以下鏈接下載題我的運行環(huán)境如果你 刷題繼續(xù) 昨天和大家分享了21-30題,今天繼續(xù)來刷31~40題 Question 31: Define a function which can pr...
摘要:刷題繼續(xù)昨天和大家分享了題,今天繼續(xù)來刷題解法一解法一解法一解法二解法一解法二解法一解法二解法三解法一解法一解法一解法一解法一源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點擊以下鏈接下載 刷題繼續(xù) 昨天和大家分享了41-50題,今天繼續(xù)來刷51~60題 Question 51: Write a function to compute 5/0 and use ...
摘要:刷題繼續(xù)大家好,我又回來了,昨天和大家分享了題,今天繼續(xù)來看題解法一解法二解法一解法二解法一解法二解法一解法二解法一解法一解法一解法一解法一解法一源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點擊以下鏈接下載題 刷題繼續(xù) 大家好,我又回來了,昨天和大家分享了31-40題,今天繼續(xù)來看41~50題 Question 41: Write a program whi...
閱讀 623·2023-04-26 02:08
閱讀 2654·2021-11-18 10:02
閱讀 3460·2021-11-11 16:55
閱讀 2341·2021-08-17 10:13
閱讀 2901·2019-08-30 15:53
閱讀 685·2019-08-30 15:44
閱讀 2545·2019-08-30 11:10
閱讀 1755·2019-08-29 16:57