目前,這個項目已經得到了7.1k Stars,4.1k Forks。python
初級水平是指剛剛經過Python入門課程的人。他能夠用1或2個Python類或函數來解決一些問題。一般,答案能夠直接在教科書中找到。git
中級水平是指剛剛學習了Python,可是已經有了較強的編程背景的人。他應該可以解決可能涉及3個或3個Python類或函數的問題。答案不能直接在教科書中找到。github
先進水平是他應該使用Python來解決更復雜的問題,使用更豐富的庫、函數、數據結構和算法。他應該使用幾個Python標準包和高級技術來解決這個問題。算法
=========一、Question:問題 二、Hints:提示 三、Solution:解決方案===========編程
一、 編寫一個程序,找出全部能被7整除但不是5的倍數的數,2000年到3200年(都包括在內)。 獲得的數字應該以逗號分隔的順序打印在一行上。數組
考慮使用range(#begin, #end)方法session
1 values = [] 2 3 for i in range(1000, 3001): 4 s = str(i) 5 if (int(s[0]) % 2 == 0) and (int(s[1]) % 2 == 0) and (int(s[2])%2==0) and (int(s[3])%2==0): 6 values.append(s) 7 8 print(','.join(values)) 9 10 11 12 13 #2000,2002,2004,.........,2884,2886,2888
二、 編寫一個能夠計算給定數字的階乘的程序。 結果應該以逗號分隔的順序打印在一行上。 假設向程序提供如下輸入: 8 則輸出爲: 40320數據結構
若是向問題提供輸入數據,則應該將其視爲控制檯輸入。app
1 def fact(x): 2 if x == 0: 3 return 1 4 return x*fact(x-1) 5 6 x = int(input()) 7 print(fact(x))
三、 對於給定的整數n,編寫一個程序來生成一個字典,其中包含(i, i*i)一個在1和n之間的整數(都包含在內)。而後程序應該打印字典。 假設向程序提供如下輸入:8 則輸出爲: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}數據結構和算法
考慮使用dict類型()
1 n = int(input()) 2 d = dict() 3 for i in range(1, n+1): 4 d[i] = i*i 5 print(d)
四、編寫一個程序,接受來自控制檯的逗號分隔的數字序列,並生成一個包含每一個數字的列表和元組。 假設向程序提供如下輸入: 34,67,55,33,12,98 則輸出爲: ['34', '67', '55', '33', '12', '98'] ('34', '67', '55', '33', '12', '98')
方法能夠將列表轉換爲元組
1 values = input() 2 l = values.split(",") 3 t = tuple(l) 4 print(l) 5 print(t)
五、 定義一個至少有兩個方法的類: getString:從控制檯輸入獲取字符串 打印字符串:打印字符串的大寫字母。還請包含簡單的測試函數來測試類方法。
使用_init__方法構造一些參數 (直接一個uppper不能夠嗎??)
1 class InputOutString(object): 2 def __init__(self): 3 self.s = "" 4 5 def getString(self): 6 self.s = input() 7 8 def printString(self): 9 print(self.s.upper()) 10 11 12 strObj = InputOutString() 13 strObj.getString() 14 strObj.printString()
六、 編寫一個程序,計算和打印的價值根據給定的公式: Q =√[(2 * C * D)/H] 如下是C和H的固定值: C是50。H是30。 D是一個變量,它的值應該以逗號分隔的序列輸入到程序中。
例子-假設程序的輸入序列是逗號分隔的: 100,150,180 則輸出爲:18,22,24
若是接收到的輸出是十進制的,則應四捨五入到其最接近的值(例如,若是接收到的輸出是26.0,則應打印爲26)
1 # !/usr/bin/env python 2 import math 3 c=50 4 h=30 5 value = [] 6 items=[x for x in input().split(',')] 7 for d in items: 8 value.append(str(int(round(math.sqrt(2*c*float(d)/h))))) 9 10 print(','.join(value))
七、編寫一個程序,以2位數字X,Y爲輸入,生成一個二維數組。數組的第i行和第j列的元素值應該是i*j。 注意: i= 0,1 . .,x - 1;j = 0, 1,¡Y-1。
例子--假設程序有如下輸入:3,5 輸出 [[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]
1 input_str = input() 2 dimensions=[int(x) for x in input_str.split(',')] 3 rowNum=dimensions[0] 4 colNum=dimensions[1] 5 multilist = [[0 for col in range(colNum)] for row in range(rowNum)] 6 7 for row in range(rowNum): 8 for col in range(colNum): 9 multilist[row][col]= row*col 10 11 print(multilist)
八、 編寫一個程序,接受逗號分隔的單詞序列做爲輸入,並在按字母順序排序後以逗號分隔的序列打印單詞。
假設向程序提供如下輸入: without,hello,bag,world 輸出爲: bag,hello,without,world
1 items=[x for x in input().split(',')] 2 items.sort() 3 print(','.join(items))
九、編寫一個接受行序列做爲輸入的程序,並在將句子中的全部字符都大寫後打印這些行。
Suppose the following input is supplied to the program: Hello world Practice makes perfect Then, the output should be: HELLO WORLD PRACTICE MAKES PERFECT
1 lines = [] 2 while True: 3 s = input() 4 if s: 5 lines.append(s.upper()) 6 else: 7 break; 8 9 for sentence in lines: 10 print(sentence)
十、編寫一個程序,接受一系列空格分隔的單詞做爲輸入,並在刪除全部重複的單詞並按字母數字排序後打印這些單詞。
假設向程序提供如下輸入: hello world and practice makes perfect and hello world again 輸出爲: again and hello makes perfect practice world
咱們使用set容器自動刪除重複的數據,而後使用sort()對數據進行排序。
1 s = input() 2 words = [word for word in s.split(" ")] 3 print(' '.join(sorted(list(set(words)))))
十一、編寫一個程序,接受一個逗號分隔的4位二進制數序列做爲輸入,而後檢查它們是否能被5整除。能被5整除的數字要按逗號分隔的順序打印。
例如:0100,0011,1010,1001 輸出爲:1010
1 value = [] 2 items=[x for x in input().split(',')] 3 for p in items: 4 intp = int(p, 2) 5 if not intp%5: 6 value.append(p) 7 8 print(','.join(value))
十二、編寫一個程序,它將找到全部這些數字在1000和3000之間(都包括在內),使數字的每一位都是偶數。 獲得的數字應該以逗號分隔的順序打印在一行上。
輸出結果爲:2000,2002,2004,2006,2008,2020,2022,2024,......,2866,2868,2880,2882,2884,2886,2888
1 values = [] 2 3 for i in range(1000, 3001): 4 s = str(i) 5 if (int(s[0]) % 2 == 0) and (int(s[1]) % 2 == 0) and (int(s[2])%2==0) and (int(s[3])%2==0): 6 values.append(s) 7 8 print(','.join(values))
1三、編寫一個程序,接受一個句子,並計算字母和數字的數量。
例如 hello world! 123 輸出爲: LETTERS 10 DIGITS 3
1 s = input() 2 d={"DIGITS":0, "LETTERS":0} 3 for c in s: 4 if c.isdigit(): 5 d["DIGITS"]+=1 6 elif c.isalpha(): 7 d["LETTERS"]+=1 8 else: 9 pass 10 print("LETTERS", d["LETTERS"]) 11 print("DIGITS", d["DIGITS"])
1四、編寫一個程序,接受一個句子,並計算字母和數字的數量。編寫一個程序,接受一個句子,並計算大寫字母和小寫字母的數量。
例如:I love You till the end of the World UPPER CASE 3 LOWER CASE 25
1 s = input() 2 d={"UPPER CASE":0, "LOWER CASE":0} 3 for c in s: 4 if c.isupper(): 5 d["UPPER CASE"]+=1 6 elif c.islower(): 7 d["LOWER CASE"]+=1 8 else: 9 pass 10 print("UPPER CASE", d["UPPER CASE"]) 11 print("LOWER CASE", d["LOWER CASE"])
1五、編寫一個程序,計算a+aa+aaa+aaaa的值與一個給定的數字做爲a的值。
例如:2 2468
1 a = input() 2 n1 = int( "%s" % a ) 3 n2 = int( "%s%s" % (a,a) ) 4 n3 = int( "%s%s%s" % (a,a,a) ) 5 n4 = int( "%s%s%s%s" % (a,a,a,a) ) 6 print (n1+n2+n3+n4)
1六、使用列表理解來對列表中的每一個奇數求平方。該列表是由逗號分隔的數字序列輸入的。
例如:1,2,3,4,5,6,7,8,9 輸出爲:1,3,5,7,9
1 # values = "1,2,3,4,5,6,7,8,9" 2 values = input() 3 numbers = [x for x in values.split(",") if int(x)%2!=0] 4 print(",".join(numbers))
1七、
編寫一個程序,根據控制檯輸入的交易日誌計算銀行賬戶的淨金額。事務日誌格式以下: D 100 W 200 D表示存款,W表示取款。
假設向程序提供如下輸入: D 300 D 300 W 200 D 100 輸出爲:500 (越寫越以爲無聊啊,繼續堅持)
1 netAmount = 0 2 while True: 3 s = input() 4 if not s: 5 break 6 values = s.split(" ") 7 operation = values[0] 8 amount = int(values[1]) 9 if operation=="D": 10 netAmount+=amount 11 elif operation=="W": 12 netAmount-=amount 13 else: 14 pass 15 print(netAmount)
1八、網站須要用戶輸入用戶名和密碼才能註冊。編寫程序檢查用戶輸入密碼的有效性。
如下是檢查密碼的準則:
1. [a-z]之間至少有一個字母
2. [0-9]之間至少1個數字
1. [A-Z]之間至少有一個字母
3.至少有一個字符來自[$#@]
4. 最小交易密碼長度:6
5. 最大交易密碼長度:12
您的程序應該接受一個逗號分隔的密碼序列,並將根據上述標準檢查它們。匹配條件的密碼將被打印出來,每一個密碼之間用逗號分隔。
例子
若是下列密碼做爲程序的輸入:
ABd1234@1 F1 # 2 w3e * 2 we3345
則程序輸出爲:
ABd1234@1
1 import re 2 value = [] 3 items=[x for x in input().split(',')] 4 for p in items: 5 if len(p)<6 or len(p)>12: 6 continue 7 else: 8 pass 9 if not re.search("[a-z]",p): 10 continue 11 elif not re.search("[0-9]",p): 12 continue 13 elif not re.search("[A-Z]",p): 14 continue 15 elif not re.search("[$#@]",p): 16 continue 17 elif re.search("\s",p): 18 continue 19 else: 20 pass 21 value.append(p) 22 print(",".join(value))
1九、
您須要編寫一個程序來對(姓名、年齡、身高)元組按升序排序,其中姓名是字符串,年齡和身高是數字。元組由控制檯輸入。排序標準爲:
1:根據名字排序;
2:而後根據年齡排序;
3:而後按分數排序。
優先級是>年齡>得分。
若是下列元組做爲程序的輸入: Tom,19,80 John,20,90 Jony,17,91 Jony,17,93 Json,21,85
輸出爲: [('John', '20', '90'), ('Jony', '17', '91'), ('Jony', '17', '93'), ('Json', '21', '85'), ('Tom', '19', '80')]
----咱們使用itemgetter來啓用多個排序鍵。
1 import operator 2 l = [] 3 while True: 4 s = input() 5 if not s: 6 break 7 l.append(tuple(s.split(","))) 8 9 print(sorted(l, key=operator.itemgetter(0,1,2)))
20、使用生成器定義一個類,它能夠迭代給定範圍0到n之間的數字,這些數字能夠被7整除。
例如100內的: 0 7 14 21 28 35 42 49 56 63 70 77 84 91 98
1 def putNumbers(n): 2 i = 0 3 while i<n: 4 j=i 5 i=i+1 6 if j%7==0: 7 yield j 8 9 for i in putNumbers(100): 10 print(i)
==============暫停分隔符==================下班回去繼續寫=================下面拓展連接==================
Python入門、提升學習網站連接:https://github.com/jackfrued/Python-100-Days?utm_source=wechat_session&utm_medium=social&utm_oi=931473721877651456
刷Leetcode網站力扣:https://leetcode-cn.com/problemset/all/
Python進階:https://docs.pythontab.com/interpy/
不抱怨,不埋怨,沒有所謂的寒冬, 只有不努力的人。加油,爲了更牛的技術更高的薪資,多學習多積累。2019.11.30