Code n Hack in Python3
Week 19
Bebras
PR70可以報早鳥
http://compthinking.csie.ntnu.edu.tw/index.php/activity/41-2017
APCS
實作題4題400分
一級分=0-49分
二級分=50-149分
三級分=150-249分
四級分=250-349分
五級分=350-400分
http://pr.ntnu.edu.tw/newspaper/index.php?mode=data&id=32635
https://zerojudge.tw/ShowProblem?problemid=b964
- while True:
- try:
- input()
- grades = input().split()
- grades = [ int(grade) for grade in grades ]
- grades = sorted(grades)
- answer1 = ''
- answer2 = 'best case'
- answer3 = 'worst case'
- for grade in grades:
- answer1 += str(grade) + ' '
- if grade < 60:
- answer2 = grade
- if answer3=='worst case' and grade >= 60:
- answer3 = grade
- print(answer1[:-1])
- print(answer2)
- print(answer3)
- except:
- break
https://zerojudge.tw/ShowProblem?problemid=b965
- while True:
- try:
- R,C,M=input().split()
- matrix=[]
- for _ in range(int(R)):
- matrix.append(input().split())
- operations=input().split()
- for operation in operations[::-1]:
- if operation=='1':
- matrix=matrix[::-1]
- else:
- matrix=[ row[::-1] for row in matrix ]
- transpose=zip(*matrix)
- matrix=[]
- for row in transpose:
- matrix.append(row)
- print(len(matrix),len(matrix[0]))
- for row in matrix:
- string=''
- for element in row:
- string+=element+' '
- print(string[:-1])
- except:
- break
Week 18
Review
N階乘
費式數列第N項
Final exam
https://repl.it/classroom/invite/CLHrFuo
Week 17
2017高中資訊學術聯展
http://www.infas-js.club/2017/
遞迴函數
在函數的定義裡面呼叫自己
用遞迴函數求N階乘
f(n) = n! = n*(n-1)*(n-2)*...*1
- def f(n):
- if n == 1:
- return 1
- return f(n-1)*n
用遞迴函數求費式數列第N項
f(n) = f(n-1) + f(n-2), f(1) = f(2) = 1
- def f(n):
- if n < 3:
- return 1
- return f(n-1) + f(n-2)
用遞迴函數求等比數列第N項
f(n) = f(n-1)*5, f(1) = 6
https://zerojudge.tw/ShowProblem?problemid=b558
- def f(n):
- if n==1:
- return 1
- return f(n-1)+n-1
- while True:
- try:
- print(f(int(input())))
- except:
- break
https://zerojudge.tw/ShowProblem?problemid=a044
公式:f(n) = f(n-1) + n*(n-1)/2 + 1
- def f(n):
- if n==1:
- return 2
- '''因為judge只給3秒,加入以下判斷,就能AC
- if n==2:
- return 4
- '''
- return f(n-1) + n*(n-1)//2 + 1
- while True:
- try:
- print(f(int(input())))
- except:
- break
Week 16
2017台北科學日
6/4在師大公館校區有空來玩
https://zerojudge.tw/ShowProblem?problemid=a149
- T=input()
- for _ in range(int(T)):
- n=input()
- product=1
- for i in n:
- product *= int(i)
- print(product)
https://zerojudge.tw/ShowProblem?problemid=a263
- def is_leap(year):
- return True if year%400==0 or (year%4==0 and year%100!=0) else False
- # https://zh.wikipedia.org/wiki/%E9%97%B0%E5%B9%B4
- def days(Y,M,D):
- # count Y into days
- result = Y*365 + Y//4 - Y//100 + Y//400
- if is_leap(Y) and M<3:
- result -= 1
- # count M into days
- days_before = [0,31,28,31,30,31,30,31,31,30,31,30]
- for month in range(M):
- result += days_before[month]
- # count D into days
- result += D
- return result
- while True:
- try:
- Y1, M1, D1 = input().split()
- Y2, M2, D2 = input().split()
- Y1, M1, D1 = int(Y1), int(M1), int(D1)
- Y2, M2, D2 = int(Y2), int(M2), int(D2)
- print( abs( days(Y1, M1, D1) - days(Y2, M2, D2) ) )
- except:
- break
Week 15
APCS
實作題如同ZeroJudge題目
http://apcs.csie.ntnu.edu.tw/index.php/samplequestions/implementationquestions
高一選修課程意見調查
https://goo.gl/forms/8k0lZ4GQZhkUm95s1
https://zerojudge.tw/ShowProblem?problemid=a065
- while True:
- try:
- S=input()
- N=''
- for i in range(6):
- N+=str( abs( ord(S[i])-ord(S[i+1]) ) )
- print(N)
- except:
- break
https://zerojudge.tw/ShowProblem?problemid=a147
- while True:
- try:
- n=int(input())
- for i in range(1,n):
- if i%7!=0:
- print(i,end=' ')
- print()
- except:
- break
https://zerojudge.tw/ShowProblem?problemid=a148
- while True:
- try:
- numbers=input().split()
- total=0
- for i in range(1,len(numbers)):
- total+=int(numbers[i])
- if total/int(numbers[0])>59:
- print('no')
- else:
- print('yes')
- except:
- break
Week 14
https://zerojudge.tw/ShowProblem?problemid=a053
- while True:
- try:
- x=int(input())
- if x<=10:
- grade=x*6
- elif 10<x<=20:
- grade=60+(x-10)*2
- elif 20<x<=40:
- grade=80+(x-20)*1
- else:
- grade=100
- print(grade)
- except:
- break
https://zerojudge.tw/ShowProblem?problemid=b532
- O=['+','-','*','/','%'] # 5個運算符號
- for _ in range(int(input())):
- S=input()
- x=''
- for s in S:
- if s in O: # 找到運算符號
- o=s
- left=int(x) # 字串x轉整數,存入left
- x=''
- continue # 不到11行,直接跳到for迴圈的下一個s
- try:
- x+=str(int(s)) # 字串S中的字元s,若能轉整數,就再轉回字元,接入字串x,若轉失敗,就continue
- except:
- continue
- right=int(x) # 字串x轉整數,存入right
- if o=='+':
- answer=left+right
- if o=='-': # 因為不影響輸出結果,且較為整齊美觀,才用四個if來寫
- answer=left-right
- if o=='*':
- answer=left*right
- if o=='/':
- answer=left//right
- if o=='%':
- answer=left%right
- print(answer)
Week 13
https://zerojudge.tw/ShowProblem?problemid=a020
- alphabet=[10,11,12,13,14,15,16,17,34,18,19,20,21,22,35,23,24,25,26,27,28,29,32,30,31,33]
- while True:
- try:
- ID=input()
- n=alphabet[ord(ID[0])-ord('A')]
- n=(n%10)*9+n//10 # (2) 英文轉成的數字, 個位數乘9再加上十位數的數字
- m=1
- for char in ID[-2:0:-1]: # (3) 各數字從右到左依次乘1、2、3、4....8
- n+=int(char)*m
- m+=1
- n+=int(ID[-1]) # (4) 求出(2),(3) 及最後一碼的和
- #print(n)
- if n%10==0:
- print('real')
- else:
- print('fake')
- except:
- break
https://zerojudge.tw/ShowProblem?problemid=a022
- while True:
- try:
- s=input()
- if s==s[::-1]:
- print('yes')
- else:
- print('no')
- except:
- break
https://zerojudge.tw/ShowProblem?problemid=a038
- while True:
- try:
- s=input()
- print(int(s[::-1]))
- except:
- break
Week 12
https://en.wikipedia.org/wiki/ASCII
https://docs.python.org/3/library/functions.html
ord(字元) 和 chr(整數) 互為反函數
https://zerojudge.tw/ShowProblem?problemid=a009
- key=ord('*')-ord('1')
- while True:
- try:
- INPUT=input()
- OUTPUT=''
- for char in INPUT:
- OUTPUT+=chr(ord(char)+key)
- print(OUTPUT)
- except:
- break
Week 11
https://zerojudge.tw/ShowProblem?problemid=a003
- while True:
- try:
- M,D=input().split()
- M,D=int(M),int(D)
- S=(M*2+D)%3
- if S==0:
- print('普通')
- elif S==1:
- print('吉')
- else:
- print('大吉')
- except:
- break
https://zerojudge.tw/ShowProblem?problemid=a004
https://zh.wikipedia.org/wiki/%E9%97%B0%E5%B9%B4
- if ((西元年分是400的倍數)或(西元年分是4的倍數但不是100的倍數))
- { 閏年 }
- else
- { 平年 }
- while True:
- try:
- y=int( input() )
- if y%400==0 or (y%4==0 and y%100!=0):
- print('閏年')
- else:
- print('平年')
- except:
- break
https://zerojudge.tw/ShowProblem?problemid=a005
- for _ in range(int(input())):
- a,b,c,d=input().split()
- a,b,c,d=int(a),int(b),int(c),int(d)
- if b-a==c-b:
- print(a,b,c,d,d+b-a)
- else:
- print(a,b,c,d,d*int(b/a))
by 蔣森 :+1:
https://zerojudge.tw/ShowProblem?problemid=a006
- while True:
- try:
- a,b,c=input().split()
- a,b,c=int(a),int(b),int(c)
- D=b**2-4*a*c
- if D>0:
- x1=str( int( (-b+D**0.5)/2/a ) )
- x2=str( int( (-b-D**0.5)/2/a ) )
- print('Two different roots x1='+x1+' , x2='+x2) # str+str+str+str
- elif D==0:
- x=str( int( -b/2/a ) )
- print('Two same roots x='+x)
- else:
- print('No real root')
- except:
- break
Week 10
https://zerojudge.tw/ShowProblem?problemid=a002
- while True:
- try:
- x,y=input().split()
- x,y=int(x),int(y)
- print(x+y)
- except:
- break
Week 9
解一元二次方程式
- print('請輸入一元二次方程式係數abc')
- a=float(input('a:'))
- b=float(input('b:'))
- c=float(input('c:'))
- D=b**2-4*a*c
- if D > 0:
- x1=(-b+D**0.5)/(2*a)
- x2=(-b-D**0.5)/(2*a)
- print('相異實數解為',x1,'和',x2)
QUIZ: finish it
QUIZ: fewer than 10 lines
QUIZ: what if someone doesn't input numbers?
定義(宣告)函數、呼叫函數
- def f(x):
- if x > 0:
- return x
- else:
- return -x
- print(f(55))
- print(f(-66))
QUIZ: 定義一個階乘函數
Week 8
使用input函數取得整數
- a=int(input('輸入A:'))
- b=int(input('輸入B:'))
- print('A加B等於',a+b)
- print('A減B等於',a-b)
- print('A乘以B等於',a*b)
- print('A除以B等於',a/b)
QUIZ: get float
使用try/except做例外處理
- while True:
- try:
- a=float(input('輸入A:'))
- break
- except:
- print('只接受數字,請重新輸入!')
QUIZ: what's wrong when someone doesn't input numbers into B?
使用字串的split方法把字串切成一個數個字串的list
- p = 'Life is short you need Python'
- q = p.split()
- print(p, type(p), len(p))
- print(q, type(q), len(q))
- for letter in p:
- print(letter)
QUIZ: print a word in a line
Week 7
- for i in range(1,17):
- print('被乘數是',i)
- for j in range(1,17):
- print(i, 'x', j, '=', i*j, end='\ ')
- if j%4==0:
- print()
QUIZ:
QUIZ:
Week 6
九九乘法表
- for i in range(1,10):
- for j in range(1,10):
- print(i, '乘以', j, '等於', i*j)
九九乘法表加分隔線
- for i in range(1,10):
- for j in range(1,10):
- print(i, '乘以', j, '等於', i*j)
- print('這是分隔線')
九九乘法表加分隔線分成三欄
- for i in range(1,10):
- for j in range(1,10):
- print(i, 'x', j, '=', i*j, end='\ ')
- if j%3==0:
- print()
- print()
Week 5
十進位→二進位
0 → 000
1 → 001
2 → 010
3 → 011
4 → 100
5 → 101
6 → 110
7 → 111
且、或、互斥或
- # 二進位 十進位
- # 011 3
- # and) 110 6
- #---------------
- # 010 2
- ans = 0b011 & 0b110
- print(ans) #印出十進位
- print(bin(ans)) #轉成二進位
- print()
- # 二進位 十進位
- # 011 3
- # or) 110 6
- #---------------
- # 111 7
- ans = 0b011 | 0b110
- print(ans) #印出十進位
- print(bin(ans)) #轉成二進位
- print()
- # 二進位 十進位
- # 011 3
- # xor) 110 6
- #---------------
- # 101 5
- ans = 0b011 ^ 0b110
- print(ans) #印出十進位
- print(bin(ans)) #轉成二進位
- print()
range函數產生range物件
- range(start, stop, step) # 當傳入3個參數時
- range(start, stop, step=1) # 當傳入2個參數時
- range(start=0, stop, step=1) # 當傳入1個參數時
QUIZ: 印出100到1000之間所有10的倍數
Week 4
for loop
- L = [3.14, '7 7 7', [1, 2, 3], 'hello python', 0]
- for element in L:
- print(element)
QUIZ: https://repl.it
Week 3
list and its methods
a_list = [ 'a', 'b', 'c', 55, 66, '77' ]
a_list[ i ] 取得位置 i 的值
a_list[ i : j ] 取得位置 i 到位置 j 但不包括位置 j 的串,長度為 j - i
a_list[ : j ] 取得最前(左)到位置 j 但不包括位置 j 的串
a_list[ i : ] 取得位置 i 到最後(右)的串
a_list.append( x ) 把 x 接到串的最後(右)
a_list.remove( x ) 把 x 從串裡移除
a_list.insert( i, x ) 把 x 插入串的位置 i
more methods
https://docs.python.org/3.6/tutorial/datastructures.html
字串跟串很像,設 s = "hello python, i'm a newbie",只用到三個加號,接成 'happy'
- s[0]+s[18]+s[6]+s[6:8]
Week 2
built-in functions
int( )
float( )
str( )
type( )
len( )
more built-in functions
https://docs.python.org/3/library/functions.html
QUIZ: 用學過的內建函數和任何運算子,只用到2.5產生777777777
- str( int(2.5+2.5+2.5) ) * len( '2.5' ) * len( '2.5' )
- str( int(2.5+2.5+2.5) ) * ( int(2.5+2.5+2.5) + int(2.5) )
- str( int(2.5+2.5+2.5) ) * ( int(2.5) + int(2.5/2.5) ) ** int(2.5)
Week 1
Python語言
https://zh.wikipedia.org/zh-tw/Python
Python 3 Tutorial
https://www.sololearn.com/Course/Python/
Python 3 IDE
https://repl.it/languages/python3
課程
https://repl.it/classroom/invite/CLHrFuo
專頁
https://www.facebook.com/code.n.hack