# python solution for zero judge ###### Day 1: 2021/12/17 ### [github 連結](https://github.com/tzuhao84/0j) ### 目前進度: [a053](https://zerojudge.tw/ShowProblem?problemid=a053) since 2022/3/6 可以先讓字典值轉list再乘上對應數值,再加上sum,去檢查第九碼,符合的話,輸出對應的英文字母。 ### 解題日期: a015: 2022/2/1 a017: 2022/2/3 a020: 2022/2/4 a021: 2022/2/13 a022: 2022/2/13 a024: 2022/2/14 a034: 2022/2/21 a038: 2022/2/22 a040: 2022/2/27 a042: 2022/2/27 a044: not yet a053: 2022/3/6 ## 觀念 * python 輸出換行:預設輸出後換行 * if else 用法: ``` if 條件A: 結果A elif 條件B: 結果B else: 其它結果 ``` * print 輸出變數格式 ``` a=7, b=3 1.print('a+b=',a+b) => a+b= 10 2.print(f'a+b={a+b}') => a+b=10 差異在於第二種方法輸出時,少空一格 ``` * 平方根 ``` a**0.5 ``` * [跳出迴圈] ``` break:強制跳出 ❮整個❯ 迴圈 continue:強制跳出 ❮本次❯ 迴圈,繼續進入下一圈 pass:不做任何事情,所有的程式都將繼續 ``` * 餘數定理的運算子: 商數//, 餘數% ``` print(87//2) >> 43 print(87%2) >> 1 ``` * [字典](https://medium.com/ccclub/ccclub-python-for-beginners-tutorial-533b8d8d96f3) ``` 新增資料 dictName[key] = value 計算key的數目 len(dict) ``` * 接續下一行:\ ``` data11=data21=data31=data41=data51=data61=data71=data81\ =data91=data101=data111=data121=data131=data141\ =data151=data161=data171=data181=data191=data201=data211\ = pd.DataFrame(columns=['w','ka','period']) 若是以逗號連接,可不加\ datas1 = [data11, data21, data31, data41, data51, data61, data71, data81, data91, data101, data111,data121, data131,data141,data151,data161,data171, data181,data191,data201,data211] ``` --- ## 技巧 * [try except statement](https://pydoing.blogspot.com/2011/01/python-try.html) ``` a = 22 b = 33 try: if a < b: print(n) except: print("except") ``` * [while True:](https://selflearningsuccess.com/python-while-loop/) ``` True 要用大寫 這是一個會一直持續下去的循環,通常會搭配break陳述句使用,來控制迴圈。 陳述的條件很多元,不僅止於這邊提到的,端看你的需求來訂出條件。 ex: while [True for i in list if i in criteria] ``` * [讀取一行以空格分開的多個數據](https://blog.csdn.net/qq_36478460/article/details/81905679) ``` l = list(map(int,input().split())) print(l) ``` * [解包list,不印出括號](https://www.delftstack.com/zh-tw/howto/python/list-without-brackets-python/) ``` lst = ['x','y','z'] print(*lst, sep = ',') ``` * [合併list成為一個字串](https://blog.csdn.net/DeniuHe/article/details/86905359) ``` a = ['周一','周二','周三','周四','周五','周六','周日'] s = "".join(a) >>周一周二周三周四周五周六周日 a = ['周一','周二','周三','周四','周五','周六','周日'] s = "*".join(a) print(s) >>周一*周二*周三*周四*周五*周六*周日 join 只限定於list 內都是string 型態 ``` * 設置空的list,並指定大小 ``` lst =[None]*大小 ``` * [floor](https://vimsky.com/zh-tw/examples/usage/python-math-floor-function.html) ``` import math x = 33.7 print (math.floor(x)) ``` * [跳出程式](https://blog.csdn.net/Dontla/article/details/103344870) ``` import sys sys.exit() ``` * [轉換ASCII](https://www.itread01.com/content/1550009362.html) ``` chr(ord(character)): 先轉成ASCII code, 再轉成字元 ``` * [print 不換行] ``` print("Hello World!", end = '') ``` * [print list 方法](https://www.geeksforgeeks.org/print-lists-in-python-4-different-ways/) ``` # Python program to print list # without using loop a = [1, 2, 3, 4, 5] # printing the list using * operator separated # by space print(*a) # printing the list using * and sep operator print("printing lists separated by commas") print(*a, sep = ", ") # print in new line print("printing lists in new line") print(*a, sep = "\n") ``` * [list 切割字串方法](https://selflearningsuccess.com/python-list/) ``` >>>print(list('steak')) ['s', 't', 'e', 'a', 'k'] ``` * [字典的key value 交換](https://blog.csdn.net/ssswill/article/details/86618553) ``` new_dict={v:k for k, v in dict.items()} ``` * [遍訪字典中所有的鍵( keys()方法 )](https://ithelp.ithome.com.tw/articles/10202396) ``` car = {"color":"black","brand":"toyota"} # 建立一個car字典裡面包含兩個鍵值對 for key in car.keys(): # keys()方法會讓for迴圈將每個鍵存到key變數中 print("key : "+key) # 印出鍵 ``` * [遍訪字典中所有的值( values()方法 )](https://ithelp.ithome.com.tw/articles/10202396) ``` car = {"color":"black","brand":"toyota"} # 建立一個car字典裡面包含兩個鍵值對 for value in car.values(): # values()方法會讓for迴圈將每個值存到value變數中 print("value : "+value) # 印出值 ``` * [新增字典key] ``` animal={} animal['dog']=12 ``` * [呼叫字典的元素](https://www.learncodewithmike.com/2019/12/python-dictionary.html) ``` dict.get(key) ``` * [初始化2D list] ``` >> mat=[[0 for x in range(5)]for i in range(2)] [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]] ``` *[產生等差數列](http://softcans.blogspot.com/2019/01/python-range-func.html) ``` range(start,end,step) ``` *檢查list 是否為空 ``` len(list)==0 ``` ## other 1. 質數:prime 2. 因數:factor 3. 階乘:factorial 4. [python 視覺化網站](https://pythontutor.com/visualize.html#mode=edit) 5. [昌爸羅馬數字](http://www.mathland.idv.tw/history/roman.htm) 6. [羅馬數字換算](https://www.toolskk.com/roman-numerals-converter)