python 期末考
期末考
第一題
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41# 第一題
def Price(ID, p):
prices = [21000, 20000, 24000, 18000, 35000, 33000, 36500, 38900]
discount = 1.0
total_price = 0
if ID >= 1 and ID <= 8:
price = prices[ID - 1]
if p == 2:
discount = 0.96
elif p >= 3 and p <= 5:
discount = 0.92
elif p >= 6 and p <= 10:
discount = 0.88
elif p >= 11:
discount = 0.85
total_price = price * p * discount
if total_price > 200000:
total_price *= 0.95
return int(total_price)
if __name__ == '__main__':
N = int(input())
ID = []
p = []
for i in range(N):
input_ID = int(input())
ID.append(input_ID)
input_p = int(input())
p.append(input_p)
Sum = 0
for i in range(N):
Sum += Price(ID[i], p[i])
print(Sum)第二題
1
2
3
4
5
6
7# 第二題
def TransStr(S):
ans=0
for i in range(len(S)):
if(ord(S[i])-64>26):ans+=ord(s[i])-96
else:ans+=ord(s[i])-64
return ans第三題
1
2
3
4
5
6
7
8
9
10
11# 第三題
def mushroom(plant):
S=[0,0,0,0,0,0]
for i in range(len(plant)):
if(plant[i]==0):S[0]+=1
if(plant[i]==1):S[1]+=1
if(plant[i]==10):S[2]+=1
if(plant[i]==11):S[3]+=1
if(plant[i]==12):S[4]+=1
if(plant[i]==13):S[5]+=1
return S第四題
1
2
3
4
5
6
7
8# 第四題
def TransYearA(year):
year=(year-1994)%10
return year
def TransYearB(year):
year=(year-1996)%12
return year第五題
- 第一種方法
1
2
3
4
5
6
7
8
9#第五題
def NumOfWords(S):
word_count=0
for i in range(len(S)-1):
if(S[i+1]==" " or S[i+1]=="," or S[i+1]=="." or S[i+1]=="-"):
if(S[i]!=" " and S[i]!="," and S[i]!="." and S[i]!="-"):word_count+=1
if(S[-1]!=" " and S[-1]!="," and S[-1]!="." and S[-1]!="-"):
if(S[-2]!=" " or S[-2]!="," or S[-2]!="." or S[-2]!="-"):word_count+=1
return word_count - 第二種方法
1
2
3
4
5#第五題-2
def NumOfWords(S):
import re
words=re.findall(r'\b\w+\b', S)
return len(words)
- 第一種方法
本部落格所有文章除特別聲明外,均採用 CC BY-NC-SA 4.0 許可協議。轉載請註明來自 Yun 的個人部落格!