林德恩、陳睿倬 Nov 12,2021
python
tcirc39th
社課
臺中一中電研社
社網:tcirc.tw
online judge:judge.tcirc.tw
IG:TCIRC_39th
函式是一種將功能先寫好,在後面可使其包裝起來,重複利用。
ex.
print() .append() .sort()
我們可以自己寫一個函式,並自訂它的功能。
def 函式名稱(參數1,參數2,參數3...): #參數數量可以0~∞ return 回傳值 #此行可寫可不寫,沒寫就只是不會回傳資料
在創建完就可以使用函式,並可以重複使用
ex.
def a(x,y): print(x+y) a(1,2)
在將函式放入其他函式或將它作為一個值時,必須有回傳值(return),在回傳之後,函式就會結束
def a(x,y): print(x+y) return x-y print(a(1,2)) b=a(3,5) print(b)
output
3
-1
8
-2
將函式多次在回傳值呼叫自己就是遞迴,多搭配if,else,elif使用
注意:在實作遞迴時要記得設置終止條件,否則會出現錯誤
def a(x,y): print(x,y,"/") if(x>y): return a(x-y,y+1) else: return 1 print(a(5,2))
範例: 費波那契數列
f(n) = f(n-1) + f(n-2)
f(0) = 0, f(1) = 1
錯誤的寫法:
def fib(x): return fib(x-1) + fib(x-2) print(fib(9))
output
RecursionError: maximum recursion depth exceeded
因為沒有設定結束
正確寫法
def fib(x:int): if (x <= 1):return x # <-- stop condition return fib(x-1) + fib(x-2) # 0 1 2 3 4 5 6 7 8 9 print(fib(9)) # 0 1 1 2 3 5 8 13 21 34
output
34
像數值、字串、list、函式都是物件
基本上所有東西都是物件
可以import
ex. int、random、string
注意,在寫class時,一定要寫初始化程式,定義屬性 _init_
class TCFSH: def __init__(self, id, name, grade): self.id = id self.name = name self.grade = grade self.score = None john = TCFSH(910943, "John", 2) print(john.id) print(john.name) print(john.grade) print(john.score) john.score = 80 print(john.score)
output
910943
John
2
None
80
我們可在類別class裡定義函式,方便我們對物件更改,訪問屬性
class TCFSH: def __init__(self, id, name, grade): self.id = id self.name = name self.grade = grade self.score = None def show_inf(self): name = self.name # 在類別裡呼叫物件屬性用self print("show", name, "inf") print(self.id) print(self.name) print(self.grade) print(self.score) def change_std_score(self, score): self.score = score print(str(self.name)+"'s grade is", self.score ) john = TCFSH(910943, "John", 2) john.show_inf() # <-- 呼叫物件函式時,需要先打上 物件名稱.函式名稱 print("=========================") john.change_std_score(100)
output
show John inf
910943
John
2
None
=========================
John's grade is 100
可以在class底下建立class在外層的是父類別,下一層的是子類別
以此類推,而呼叫子類別時要先呼叫父類別才能運作,中間如何呼叫內部的函式一樣用.隔開
class a: class b: def __init__(self): self.z=1 def k(y): print(y) def __init__(self): self.w=1 def n(x): print(x**2) a.b.k(15) a.n(10)
output
15
100
樹是一種電腦的資料結構,如同容器一樣可儲存資料,並有效地插入,搜尋資料
image link
Root:根,最上面的一個點
Node:節點,相連的點
Parent:父節點
Child:子節點
Grandchild:子節點的子節點
Ancestor:在往上跨一代的節點都是該點的Ancestor(不含父節點)
Descendant:在往下跨一代的節點都是該點的Descendant(不含子節點)
Subtree:子樹,下面有child的child
Leaves:沒有child的子節點
Key:裡面的值
Edge:節點之間的連接
Sibling:兄弟節點
Level:由上往下算(ex.Level 1,Level 2,Level 3…)
Hight:高度,由下往上算有幾層
Tree Degree:子節點數
二元樹是樹的其中一種結構,它只有每一層只有兩個分支
習慣上認為兩個子樹是不同的tree(只有在二元樹)
二元樹(排序)
排序後右邊的樹一定比左邊大,右子樹比左子樹大
image link
完美二元樹(Perfect Binary Tree):每一層都是最大節點數
完全二元樹(Complete Binary Tree):是完美二元樹或除了最後一層以外是滿的,並只連續缺少右子樹。
import random as r class binary_tree: def __init__(self, data): self.left = None self.right = None self.data = data def SetChild(self, n): if ( n.data > self.data): if (self.right == None): self.right = n return 0 else: self.right.SetChild(n) elif (n.data < self.data): if (self.left == None): self.left = n return 0 else: self.left.SetChild(n) def show(self): print("(", end = " ") if (self.left != None): self.left.show() #print(self.left.data) else: print(None, end = ", ") print(self.data, end = ", ") if(self.right != None): self.right.show() #print(self.right.data) else: print(None, end = ", ") print(")", end = " ") # set root a = [] n = r.randint(0, 10) a.append(n) a[0] = binary_tree(n) for i in range(1, 6): n = r.randint(0, 50) a.append(n) a[i] = binary_tree(a[i]) a[0].SetChild(a[i]) for i in a: print(i.data, end = " ") print("") a[0].show()
output
3 49 47 1 14 11
( ( None, 1, None, ) 3, ( ( ( ( None, 11, None, ) 14, None, ) 47, None, ) 49, None, ) )
1232