1.此為GitHub項目的學習記錄,記錄著我的思考,代碼基本都有註釋。 2.可以作為Python初學者鞏固基礎的絕佳練習,原題有些不妥的地方我也做了一些修正。 3.建議大家進行Python編程時使用英語,工作時基本用英語。 4.6~17題為level1難度,18-22題為level3難度,其餘都為l ...
1.此為GitHub項目的學習記錄,記錄著我的思考,代碼基本都有註釋。
2.可以作為Python初學者鞏固基礎的絕佳練習,原題有些不妥的地方我也做了一些修正。
3.建議大家進行Python編程時使用英語,工作時基本用英語。
4.6~17題為level1難度,18-22題為level3難度,其餘都為level1難度。
項目名稱:
100+ Python challenging programming exercises for Python 3
#!usr/bin/env Python3.9 # linux環境運行必須寫上
# -*- coding:UTF-8 -*- # 寫一個特殊的註釋來表明Python源代碼文件是unicode格式的
"""Question:
Write a program which will find all such numbers
which are divisible by 7 but are not a multiple of 5,
between 2000 and 3200 (both included).
The numbers obtained should be printed
in a comma-separated sequence on a single line."""
'''Hints: Consider use range(#begin, #end) method'''
l1 = [] # 建立空列表
for i in range(2000, 3201): # for迴圈尋找能被7整除但不能被5整除的數字
if (i % 7 == 0) and (i % 5 != 0):
l1.append(str(i)) # 列表裡的數據需要是字元串
print(','.join(l1)) # 在一行顯示,以逗號隔開
#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-
"""
Question:
Write a program which can compute the factorial of a given numbers.
The results should be printed in a comma-separated sequence on a single line.
Suppose the following input is supplied to the program: 8 Then, the output should be: 40320
"""
'''Hints: In case of input data being supplied to the question, it should be assumed to be a console input.'''
i = 1
result = 1
t = int(input('請輸入一個數字: '))
while i <= t: # while迴圈
result = result * i # 實現階乘
i += 1
print(result)
# 源代碼
'''
def fact(x):
if x == 0:
return 1
return x * fact(x - 1)
x=int(input('請輸入一個數字: '))
print(fact(x))
'''
#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-
"""
Question:
With a given integral number n,
write a program to generate a dictionary
that contains (i, i*i) such that is an integral number between 1 and n (both included).
and then the program should print the dictionary.
Suppose the following input is supplied to the program: 8
Then, the output should be: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}
"""
'''
Hints: In case of input data being supplied to the question, it should be assumed to be a console input.
Consider use dict()
'''
m = int(input('請輸入一個數字1~n:'))
n = {} # 創建一個空字典
for i in range(1, m+1):
n[i] = (i * i) # 將鍵值對存入空字典
i += 1
print(n)
# 源代碼
'''
n=int(input())
d=dict()
for i in range(1,n+1):
d[i]=i*i
print(d)
'''
#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-
"""
Question:
Write a program which accepts a sequence of comma-separated numbers from console and generate a list and a tuple
which contains every number.
Suppose the following input is supplied to the program: 34,67,55,33,12,98
Then, the output should be: ['34', '67', '55', '33', '12', '98'] ('34', '67', '55', '33', '12', '98')
"""
'''
Hints: In case of input data being supplied to the question,
it should be assumed to be a console input.
tuple() method can convert list to tuple
'''
import re
t = input('請輸入數據:')
d = t.split(',') # split()函數按照所給參數分割序列
k = re.findall(r'\d+', t) # findall()函數在字元串中找到正則表達式所匹配的所有子串,並組成一個列表返回
# \d 相當於[0-9],匹配0-9數字
r = tuple(k) # 轉換為元組類型
print(k)
print(r)
#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-
"""
Question:
Define a class which has at least two methods:
getString: to get a string from console
input printString: to print the string in upper case.
Also please include simple test function to test the class methods.
"""
'''
Hints: Use init method to construct some parameters
'''
class InputOutputString(): # 創建類函數
def __init__(self):
self.s = ''
def __getString__(self):
self.s = input('Please input what you want:')
def __printString__(self):
print(self.s.upper()) # upper()函數使字元串字母變為大寫
stringRan = InputOutputString()
stringRan.__getString__()
stringRan.__printString__()
#!usr\bin\env Python3
# encoding:UTF-8
"""
Question:
Write a program that calculates and prints the value according to the given formula:
Q = Square root of [(2 * C * D)/H]
Following are the fixed values of C and H: C is 50. H is 30.
D is the variable whose values should be input to your program in a comma-separated sequence.
Example Let us assume the following comma separated input sequence is given to the program: 100,150,180
The output of the program should be: 18,22,24
"""
'''
If the output received is in decimal form,
it should be rounded off to its nearest value
(for example, if the output received is 26.0, it should be printed as 26)
In case of input data being supplied to the question, it should be assumed to be a console input.
'''
import math
C = 50
H = 30
value = [] # 創建空列表
t = input('Please input your values, example:34, 23, 180, ... :')
x = [i for i in t.split(',')] # 列表推導式
for d in x:
value.append(str(round(math.sqrt((2 * C * float(d)) / H)))) # round()函數實現四捨五入,sqrt表示平方
print(','.join(value))
#!usr\bin\env Python3
# encoding:UTF-8
"""
Question:
Write a program which takes 2 digits, X,Y as input and generates a 2-dimensional array.
The element value in the i-th row and j-th column of the array should be i*j.
Note: i=0,1.., X-1; j=0,1,¡..., Y-1.
Example Suppose the following inputs are given to the program: 3,5
Then, the output of the program should be: [[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]
"""
'''
Note: In case of input data being supplied to the question,
it should be assumed to be a console input in a comma-separated form.
'''
input_String = input('Please input 2 digits, example:2,3.:')
dimensions = [int(x) for x in input_String.split(',')] # 表示數組行數和列數
rowNum = dimensions[0] # 賦予行數
colNum = dimensions[1] # 賦予列數
multi_list = [[0 for col in range(colNum)] for row in range(rowNum)] # 創建一個空數組列表
for col in range(colNum):
for row in range(rowNum):
multi_list[row][col] = col * row # 向空數組填入數值
print(multi_list)
#!usr\bin\env Python3
# encoding:UTF-8
"""
Question:
Write a program that accepts a comma separated sequence of words as input
and prints the words in a comma-separated sequence after sorting them alphabetically.
Suppose the following input is supplied to the program: without,hello,bag,world
Then, the output should be: bag,hello,without,world
"""
'''
Hints: In case of input data being supplied to the question, it should be assumed to be a console input.
'''
temp = input('Please input your words:')
inputStr = temp.split(',') # 按“, ”分隔輸入的單詞
inputStr.sort() # 按字母順序進行排列
print(','.join(inputStr)) # 輸出按字母順序排列的單詞,單詞之間用逗號分隔
# 源代碼
"""
items=[x for x in input().split(',')]
items.sort()
print(','.join(items))
"""
#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-
"""
Question:
Write a program that accepts sequence of lines as input
and prints the lines after making all characters in the sentence capitalized.
Suppose the following input is supplied to the program: Hello world Practice makes perfect
Then, the output should be: HELLO WORLD PRACTICE MAKES PERFECT
"""
'''
Hints: In case of input data being supplied to the question, it should be assumed to be a console input.
'''
lines = []
while True:
s = input('Please input your sentences:')
if s:
lines.append(s.upper()) # 將大寫過後的句子放進列表裡,可以放進去多個句子
else:
break
for sentence in lines: # 列印每一句句子
print(sentence)
#!usr/bin/env Python3.9
# -*- coding:UTF-8 -*-
"""
Question:
Write a program that accepts a sequence of whitespace separated words as input
and prints the words after removing all duplicate words and sorting them alphanumerically.
Suppose the following input is supplied to the program: hello world and practice makes perfect and hello world again
Then, the output should be: again and hello makes perfect practice world
"""
'''
Hints: In case of input data being supplied to the question, it should be assumed to be a console input.
We use set container to remove duplicated data automatically and then use sorted() to sort the data.
'''
temp = input('Please input your words: ') # 輸入想說的話
words = [x for x in temp.split(' ')] # 按照空格分割,把單詞放進列表
print(' '.join(sorted(list(set(words))))) # 用set()函數創建一個無序的、不重覆(可利用這一點刪除重覆元素)
# 元素集,並可進行與或等運算。經過排序後再用空格分隔裝進列表列印出來
再附上Python常用標準庫供大家學習使用:
Python一些常用標準庫解釋文章集合索引(方便翻看)
“學海無涯”