python moodle

Python is an animal

fuck it

題解只是提供想法及做法,在觀看前請愛惜自己的期末分數

I

A

1
2
3
4
5
6
a=0
for i in range(0,5):
a=a+int(input())
print("sum:",a)
print("avg:",'%.2f'%(a/5))

B

1
2
3
4
5
a=0.0
r=float(input())
h=float(input())
a=r*r*h*3.1416
print('%.4f'%a)

II

A

1
2
3
4
5
6
7
s=input()
a=s.split()
avg=0.0
for i in range(1,6):
avg=avg+float(a[i])
avg=avg/5
print(a[0]+": "+'%.4f'%avg)

B

1
2
3
4
5
6
7
8
s=input()
s=s.split()
if(any(c.isalpha() for c in s[1])):
print("Score 1 error!!")
elif(any(c.isalpha() for c in s[2])):
print("Score 2 error!!")
else:
print(s[0]+": "+str('%.4f'%((float(s[1])+float(s[2]))/2)))

C

1
2
3
4
5
6
import math as m
a=float(input())
b=float(input())
c=float(input())
s=(a+b+c)/2
print('%.2f'%(m.sqrt(s*(s-a)*(s-b)*(s-c))))

D

1
2
3
4
5
6
7
8
import math as m
a=float(input())
b=float(input())
c=float(input())
ans1=float('%.2f'%((-b+m.sqrt(b*b-4*a*c))/2*a))
ans2=float('%.2f'%((-b-m.sqrt(b*b-4*a*c))/2*a))
print(ans1 if ans1>ans2 else ans2)
print(ans2 if ans1>ans2 else ans1)

III

A

1
2
s=input()
print(s.lower().capitalize())

B

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
s=input()
s=s.replace('.','!').lower()
a=s.split()
ans=""
for i in a:
if(i=="he" or i=="she"):
ans=ans+"I"
elif(i=="do" or i=="does"):
ans=ans+"did"
elif(i=="have" or i=="has"):
ans=ans+"had"
else:
ans=ans+i
ans=ans+' '
ans=ans.rstrip().capitalize()
print(ans)

C

1
2
3
4
import math as m
g=9.8
h=float(input()) #sqrt(2h/g)=t
print('%.1f'%(m.sqrt(2*h/g)))

D

1
2
3
4
5
6
7
8
9
10
11
12
str=input()
str=str.replace('(','{').replace(')','}').lower()
str=str.split()
meow=""
for i in str:
if i.startswith("sqrt"):
meow=meow+"$\\"+i+"$"
else:
meow=meow+i
meow=meow+' '
meow=meow.capitalize().strip()
print(meow)

E

1
2
3
4
5
6
k=[128,64,32,16,8,4,2,1]
s=input()
ans=0
for i in range(0,8):
ans=ans+k[i] if s[i]=='1' else ans
print("The value of binary",s,"in decimal is is:",str(ans)+'.')

IV

A

1
a=int(input());print("M" if a<0 else "O" if a%2==1 else "Z" if a==0 else "E")

B

1
2
3
4
5
6
7
8
9
10
11
a=float(input())
a=a+float(input())
a=a+float(input())
ans=""
if((a/3)<0 or (a/3)>100):
ans="BS"
elif((a/3)<60):
ans="FAIL"
else:
ans="PASS"
print(ans)

C

1
2
3
4
5
6
l=[]
for i in range(0,3):
tmp=float(input())
tmp=tmp*1.25 if tmp<60 else tmp
l.append(tmp)
print("PleaseGoToDieOneDie" if (sum(l)/3)<60 else "Hmm")

D

1
2
3
4
5
6
7
8
9
10
11
12
import math as m
r=int(input())
if r>10000:
print("Sleeping in school")
else:
mn=70
r=r-1500
if r<=0:
print(mn)
else:
mn=mn+(m.ceil(r/500)*5)
print(mn)

E

1
2
3
4
5
6
d=[["0101","A"],["0111","B"],["0010","C"],["1101","D"],["1000","E"],["1100","F"]]
s=input()
s=s.replace(" ","")
for x in d:
if s==x[0]:
print(x[1])

F

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import sys
str=input()
if any(c.isalpha() for c in str):
print("-_-")
sys.exit()
str=int(str)
if str>100 or str<0:
print("Error!")
sys.exit()
if str>=90:
print("A\nWork hard play hard")
elif str>=80 and str<90:
print("B\nWork hard play hard")
elif str>=70 and str<80:
print("C\nWork hard play hard")
elif str>=60 and str<70:
print("D\nWork hard play hard")
else:
print("E\nPlay hard die hard")

G

1
2
3
4
5
6
7
8
9
10
11
s=input()
if(s.endswith("tnfsh.tn.edu.tw")):
if(s.startswith("https://") or s.startswith("http://")):
print("OK!!")
else:
print("Invalid web site URL!")
else:
if(s.startswith("https://") and s.endswith("tn.edu.tw")):
print("OK!!")
else:
print("Invalid web site URL!")

V

A

1
2
3
4
5
ans=0.0
n=int(input())
for i in range(1,n+1):
ans=ans+(1/i)
print(f'%.4f'%ans)

B

1
2
3
4
5
6
7
8
9
10
11
12
13
import sys
def meow(x):
tmp=0
for i in range(1,x):
if(x%i==0):
tmp=tmp+i
return 1 if tmp==x else 0
a,b=int(input()),int(input())
for i in range(a,b+1):
if(meow(i)):
print(i)
sys.exit()
print("No number is perfect")

C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
s=input()
s=s.split()
sup=10000
sdn=-1
s=[int(x) for x in s]
s.sort(reverse=True)
srted=""
for i in s:
srted=srted+str(i)+' '
srted=srted.strip()
for i in s:
if int(i)<60 and int(i)>=sdn:
sdn=int(i)
if int(i)>=60 and int(i)<=sup:
sup=int(i)
print(srted)
print("best case") if sdn>=60 or sdn==-1 else print(sdn)
print("worst case") if sup<60 or sup==10000 else print(sup)

D

1
2
3
4
5
6
7
8
9
10
11
import math as m
s=input()
s=s.split()
s=map(int,s)
s=list(s)
n=len(s)
mu=sum(s)/n
sig=0.0
for i in s:
sig=sig+(i-mu)*(i-mu)
print(f'%.3f'%m.sqrt((sig/n)))

E

1
2
3
4
5
6
7
8
9
10
a=input()
a=int(a)
insu=[]
for i in range(2,a):
if a%i==0:
insu.append(i)
if len(insu)==0:
print("XD")
else:
print(sum(insu))

VI

A

1
2
3
4
5
s=input().strip()
tmp=0
for i in s:
tmp=tmp+int(i)
print(tmp)

B

1
2
3
4
5
6
7
k=int(input())
s=0
meow=1
while s<=k:
s=s+(1/meow)
meow=meow+1
print(meow-1)

C

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
import math
a=""
try:
while 1:
s=input()
a=a+s+" "
except:
a=list(map(int,a.split()))

sum=0
for i in a:
sum=sum+i
p=0
f=0
for j in a:
if j>=60:
p=p+1
else:
f=f+1
print(p)
print(f)
print('{0:.2f}'.format(sum/len(a)))
avg=sum/len(a)
ss=0
for k in a:
ss=ss+k**2
sigma=math.sqrt(ss/len(a)-avg*avg)
print('{0:.2f}'.format(sigma))

D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
a=list(map(int, input().split()))
i=0
while a[0]!=a[2] or a[1]!=a[2]:
i=i+1
b=(a[0]+a[1])//2
if b>a[2]:
a[1]=b-1
if b<a[2]:
a[0]=b+1
if a[2]==(a[0]+a[1])//2:
print(i,': ',b,sep='')
print(i+1,': ',a[2],sep='')
break
print(i,': ',b,sep='')

E

1
2
3
4
5
6
7
8
import sys
a=int(input())
tmp=a
for i in range(1,a+1):
tmp=tmp-i
if tmp<=0:
print(i)
sys.exit()

F

1
2
3
4
5
6
7
8
9
10
11
12
13
a=input()
ans=0
d={}
while 1:
ans=0
for i in a:
ans=ans+(int(i)*int(i))
if d.get(ans)==None:
d[ans]=1
else:
break
a=str(ans)
print("Happy" if ans==1 else "Unhappy")

G

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import sys
try:
while 1:
tans=0
s=input().split()
a=int(s[0]);b=int(s[1])
for i in range(min(a,b),max(a,b)+1):
n=i
ans=0
while 1:
ans=ans+1
if(n==1):
break
elif(n%2==1):
n=n*3+1
elif(n%2==0):
n=n/2
tans=max(ans,tans)
print(f'{a} {b} {tans}')
except:
sys.exit()

VII

A

hint : 既然已經學了字典的概念,那就拋棄條件判斷試試看吧

1
2
3
4
5
6
7
8
9
10
d=[["0101","A"],["0111","B"],["0010","C"],["1101","D"],["1000","E"],["1100","F"]]
t=int(input())
ans=""
for _ in range(0,t):
s=input()
s=s.replace(" ","")
for x in d:
if s==x[0]:
ans=ans+x[1]
print(ans)

B

水題

1
2
3
4
5
6
s=input()
a=0;b=0
for i in range(0,len(s)):
a=a+int(s[i]) if i%2==0 else a
b=b+int(s[i]) if i%2==1 else b
print(abs(a-b))

C

hint : 簡單的觀念,既然不能判前一項,那就判後一項吧

1
2
3
4
5
6
7
8
9
n=int(input())
a=[int(x) for x in input().split()]
hj=0;lj=0
for i in range(0,n-1):
if a[i]<a[i+1]:
hj=hj+1
if a[i]>a[i+1]:
lj=lj+1
print(hj);print(lj)

D

hint : 加個輸出不難吧

1
2
3
4
5
6
7
8
9
10
11
meow=int(input())
for k in range(1,meow+1):
n=int(input())
a=[int(x) for x in input().split()]
hj=0;lj=0
for i in range(0,n-1):
if a[i]<a[i+1]:
hj=hj+1
if a[i]>a[i+1]:
lj=lj+1
print(f"Case {k}:",hj,lj)

E

hint : 先建出一個矩陣,這樣等下輸入時直接塞入就好了,省時間

1
2
3
4
5
6
7
8
9
10
11
12
13
14
x=int(input())
y=int(input())
arr=[[0 for i in range(0,x)] for i in range(0,y)]
for i in range(0,x):
s=input().split()
s=s[::-1]
ns=0
for j in range(0,y):
arr[j][i]=s[ns]
ns=ns+1
for i in arr:
for j in i:
print(" "*(3-len(str(j)))+str(j),end="")
print()

F

hint : 簡單的觀念,輸出時紀錄幾個零,然後再輸出就好啦 !!

1
2
3
4
5
6
7
8
9
s=input().split()
zs=0
for i in s:
if(i=="0"):
zs=zs+1
else:
print(i,end=' ')
for i in range(0,zs):
print("0",end=' ')

G

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
import sys
s=input().split();fx=int(s[0]);fy=int(s[1])
s=input().split();x=int(s[0]);y=int(s[1])
d={}
d[f"{x}-{y}"]=1
s=input().split()
for i in s:
if(len(d)>=fx*fy):
ans=0
for k in d:
ans=ans+int(d[k])
print(ans)
sys.exit()
if(i=="1"):
if(x-1>=0):
x=x-1
elif(i=="2"):
if(y+1<fy):
y=y+1
elif(i=="3"):
if(x+1<fx):
x=x+1
elif(i=="4"):
if(y-1>=0):
y=y-1
if(d.get(f"{x}-{y}")==None):
d[f"{x}-{y}"]=1
else:
d[f"{x}-{y}"]=d[f"{x}-{y}"]+1

VIII

A

hint : 字典

1
2
3
4
5
6
7
8
9
10
import sys
str=input()
k=str.split()
d={}
for i in k:
d[i]=1 if d.get(i)==None else 2
for i in k:
if d[i]==1:
print(i)
sys.exit()

B

hint : easy math

1
2
3
4
5
6
7
lst=input()
lst=lst.split()
pa=int(input())
owo=1
for i in lst:
owo=owo*int(i)
print(owo%pa)

explain : python 可以執行大數運算所以此題沒什麼意義,正規寫法如下

1
2
3
4
5
6
lst=input().split()
pa=int(input())
owo=1
for i in lst:
owo=owo*(int(i)%pa)
print(owo)

B-2

hint : 簡單的split跟字串基本原理就可以解決

1
2
3
4
5
6
7
8
9
10
11
12
13
s=input().split(',')
h=-100000
hn=""
sm=0
for i in s:
k=i.split(':')
if(int(k[1])>h):
h=int(k[1])
hn=k[0]
sm=sm+int(k[1])
print(i.replace(":"," : "))
print("Sum : "+str(sm))
print("Top : "+hn)

C

hint : 條件判斷,字串處理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
s=input().split()
nd=input()
w=int(input())
tmp=0
ans=""
for i in s:
if i==nd:
tmp=tmp+1
if(tmp!=w):
ans=ans+i+' '
else:
ans=ans+i
ans=ans+' '
print(ans.strip())

D

hint : 字典處理,這邊觀念是將行數與字母重複數做比對,簡單實作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
s=set()
d={}
h=0
l=0
try:
while 1:
st=input()
st=st.strip()
st=st.split(',')
for i in st:
if (i in s)==False:
s.add(i)
if d.get(i)==None:
d[i]=1
else:
d[i]=d[i]+1
l=l+1
except:
for i in d.keys():
if(d[i]==l):
h=h+1
print(h);print(sorted(s))

E

hint : sorted() 可以依照keys來做排序喔 >\\\<

1
2
3
4
5
6
7
8
9
10
11
s=input().lower()
s=s.replace(':','').replace('.','').replace(',','').replace('?','').replace(';','')
s=s.split()
d={}
for i in s:
if d.get(i)==None:
d[i]=1
else:
d[i]=d[i]+1
for i in sorted(d.keys()):
print(i+":",d[i])

IX

need to know : funtion => def(){}

A

1
2
def sum(a,b):
return (int(a)+int(b))

B

1
2
3
4
5
6
def isPrime(a):
a=int(a)
for i in range(2,a):
if a%i==0:
return False
return True

C

1
2
3
4
5
6
7
8
def magic(a):
if a=="0":
return 0
a=int(a)
sum=0.0
for i in range(1,a+1):
sum=sum+1/i
return sum

D

1
2
3
4
5
6
def findMax(s):
tmp=-10000
for i in s:
if int(i)>tmp:
tmp=int(i)
return tmp

E

1
2
3
4
5
6
7
8
9
10
11
def resort(s):
zs=0
ans=[]
for i in s:
if(i=="0"):
zs=zs+1
else:
ans.append(i)
for i in range(0,zs):
ans.append(0)
return ans

A

A

1
2
3
4
5
6
7
8
9
def magic(a):
if a=="0":
return 0
a=int(a)
sum=0.0
for i in range(1,a+1):
sum=sum+1/i
return sum
print('%.4f'%magic(int(input())))

正規解法:

1
2
3
4
5
6
def meow(n):
if n==1:
return 1
else:
return (1/n)+meow(n-1)
print('%.4f'%meow(int(input())))

B

1
2
3
4
5
6
def meow(n,t):
if(2*t>=n):
return 0
else:
return n-(2*t)+meow(n,t+1)
print(meow(int(input()),0))

C

假解

1
2
3
4
5
6
7
s=input()
s=s.replace('[',' ').replace(']',' ').replace(',',' ')
s=s.split()
sm=0
for i in s:
sm=sm+int(i)
print(sm)

正解

1
2
3
4
5
6
7
8
9
def meow(s,l):
for i in l:
if type(i) == type([]):
s=meow(s,i)
else:
s=s+i
return s
s=eval(input())
print(meow(0,s))

D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
s=list(map(int,input().split()))
l=[]
l.append(s)
while(len(s)>1):
a=[]
for i in range(1,len(s)):
a.append(s[i]+s[i-1])
s=a
l.append(a)
for i in range(len(l)-1,-1,-1):
ans=""
for j in l[i]:
ans=ans+str(j)+' '
print(ans.strip())

遞迴解法
但是非常沒有必要而且時間複雜度很高,p.s.扣還比較長

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def meow(s,t):
k=[]
if(len(s)==t):
return s
else:
for i in range(1,len(s)):
k.append(s[i]+s[i-1])
return meow(k,t)

l=list(map(int,input().split()))

for i in range(1,len(l)+1):
d=meow(l,i)
for k in range(0,len(d)):
if k>0 : print(' ',end='')
print(d[k],end='')
print()

end of cheatsheat