python 100題練習記錄(三)
Question 13
Level 2
Question:
Write a program that accepts a sentence and calculate the number of letters and digits.
Suppose the following input is supplied to the program:
hello world! 123
Then, the output should be:
LETTERS 10
DIGITS 3
Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
方法一
str = input("請輸入字串內容:")
dig,let = 0,0
for i in str:
if i.isdigit():
dig += 1
if i.isalpha():
let += 1
print("LETTERS %d"%let)
print("DIGITS %d"%dig)
方法二-字典
dic = {'LETTERS':0,'DIGITS':0}
str = input("請輸入字串:")
for i in str:
if i.isalpha():
dic['LETTERS'] += 1
if i.isdigit():
dic['DIGITS'] += 1
print("LETTERS %d"%dic['LETTERS'])
print("LETTERS %d"%dic['DIGITS'])
Question 14
Level 2
Question:
Write a program that accepts a sentence and calculate the number of upper case letters and lower case letters.
Suppose the following input is supplied to the program:
Hello world!
Then, the output should be:
UPPER CASE 1
LOWER CASE 9
Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
dic = {"UPPER CASE":0,"LOWER CASE":0}
str = input("請輸入字串:")
for s in str:
if s.isupper():
dic['UPPER CASE'] += 1
if s.islower():
dic['LOWER CASE'] += 1
print("UPPER CASE %d"%dic['UPPER CASE'])
print("LOWER CASE %d"%dic['LOWER CASE'])
Question 15
Level 2
Question:
Write a program that computes the value of a+aa+aaa+aaaa with a given digit as the value of a.
Suppose the following input is supplied to the program:
9
Then, the output should be:
11106
Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
方法一
i = int(input("請輸入數值‘0-9’:"))
r = i+(i*10+i)+(i*100+i*10+i)+(i*1000+i*100+i*10+i)
print(r)
方法二
i = int(input("請輸入數值‘0-9’:"))
n1 = int("%d"%i)
n2 = int("%d%d"%(i,i))
n3 = int("%d%d%d"%(i,i,i))
n4 = int("%d%d%d%d"%(i,i,i,i))
print(n1+n2+n3+n4)
Question 16
Level 2
Question:
Use a list comprehension to square each odd number in a list. The list is input by a sequence of comma-separated numbers.
Suppose the following input is supplied to the program:
1,2,3,4,5,6,7,8,9
Then, the output should be:
1,3,5,7,9
Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
list = input("請輸入數值,以','分割:").split(',')
for i in list:
if int(i)%2 == 0:
list.remove(i)
print(",".join(list))
Question 17
Level 2
Question:
Write a program that computes the net amount of a bank account based a transaction log from console input. The transaction log format is shown as following:
D 100
W 200
D means deposit while W means withdrawal.
Suppose the following input is supplied to the program:
D 300
D 300
W 200
D 100
Then, the output should be:
500
Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
total = 0
while True:
s = input("請輸入數值,以D/W進行前置標識,以空格做分割:").split(" ")
count = s[1].strip()
if s[0].upper() == "D":
if int(count) > 0:
total += int(count)
print(total)
else:
print("輸入的值必須大於0")
if s[0].upper() == "W":
if int(count) > 0 :
total -= int(count)
print(total)
else:
print("輸入有誤")
Question 18
Level 3
Question:
A website requires the users to input username and password to register. Write a program to check the validity of password input by users.
Following are the criteria for checking the password:
- At least 1 letter between [a-z]
- At least 1 number between [0-9]
- At least 1 letter between [A-Z]
- At least 1 character from [$#@]
- Minimum length of transaction password: 6
- Maximum length of transaction password: 12
Your program should accept a sequence of comma separated passwords and will check them according to the above criteria. Passwords that match the criteria are to be printed, each separated by a comma.
Example
If the following passwords are given as input to the program:
ABd1234@1,a F1#,2w3E*,2We3345
Then, the output of the program should be:
ABd1234@1
Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
name = input("請輸入賬號資訊:")
p = input("請輸入密碼資訊,以逗號分割:").split(',')
def check_pwd(p):
for pwd in p:
up_count, low_count, num_count, ts_count = 0, 0, 0, 0
if len(pwd) in range(6, 13):
for i in pwd:
if str(i).isupper():
up_count += 1
elif str(i).islower():
low_count += 1
elif str(i).isdigit():
num_count += 1
elif str(i) in ("$#@"):
ts_count += 1
else:
pass
if (up_count > 0) and (low_count > 0) and (num_count > 0) and (ts_count > 0):
print(pwd)
else:
print("密碼必須取6-12位,包含字母大小寫、特殊符號、數字")
check_pwd(p)
Question 19
Level 3
Question:
You are required to write a program to sort the (name, age, score) tuples by ascending order where name is string, age and score are numbers. The tuples are input by console. The sort criteria is:
1: Sort based on name;
2: Then sort based on age;
3: Then sort by score.
The priority is that name > age > score.
If the following tuples are given as input to the program:
Tom,19,80
John,20,90
Jony,17,91
Jony,17,93
Json,21,85
Then, the output of the program should be:
[(‘John’, ‘20’, ‘90’), (‘Jony’, ‘17’, ‘91’), (‘Jony’, ‘17’, ‘93’), (‘Json’, ‘21’, ‘85’), (‘Tom’, ‘19’, ‘80’)]
Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
We use itemgetter to enable multiple sort keys.
import operator
l = []
while True:
s = input()
if not s:
break
l.append(tuple(s.split(",")))
print(sorted(l, key=operator.itemgetter(0,1,2)) )
Question 20
Level 3
Question:
Define a class with a generator which can iterate the numbers, which are divisible by 7, between a given range 0 and n.
Hints:
Consider use yield
class Num_7(object):
def __init__(self,n):
self.min = 0
self.max = n
def check_7(self):
for i in range(0,self.max):
if i%7 == 0 :
print(i,end=" ")
if __name__ == '__main__':
num = Num_7(89)
num.check_7()
相關文章
- Python-100 練習題 02Python
- vsc練習問題記錄
- Python-100 練習題 01 & 列表推導式Python
- 【錯題記錄】JavaScript專項練習(篇五)JavaScript
- 【錯題記錄】JavaScript專項練習(篇六)JavaScript
- 新手練習:Python練習題目Python
- Python練習題(三)--視訊展示網站Python網站
- Python 練習題Python
- 生物套卷練習記錄
- python練習題解析Python
- 語文套卷練習記錄
- Python3.x 基礎練習題100例(51-60)Python
- Python基礎練習題Python
- 五、python的練習題Python
- Python函式練習題Python函式
- python相關練習題Python
- Vue 學習記錄三Vue
- 【show me the code】Python練習題&語法筆記 2Python筆記
- 演算法練習記錄(24.10.5)演算法
- python學習記錄Python
- 「學習記錄」《數值分析》第三章計算實習題(Python語言)Python
- python指令碼練習筆記Python指令碼筆記
- 【21】Python100例基礎練習(5)Python
- python練習冊-第0000題Python
- ISCC 2024 練武題 misc趣題記錄
- Caffe學習記錄:Cifar-10 自定義網路訓練記錄
- python學習記錄7Python
- python學習記錄5Python
- python物件導向練習題01Python物件
- python練習冊-第0002題Python
- python006 列表練習題Python
- Python練習題篇(列表、字典、元祖)Python
- 9道python基礎練習題Python
- 學車手記之三 練習移庫(zt)
- java學習記錄第三週Java
- 大一暑假學習記錄(三)
- 一個關於狗記錄的Java練習Java
- Opencv第三章練習題答案OpenCV