Hugo Wang
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- type: slide title: SITCON Camp 2023 主線課程 課程簡報 - Python 基礎容器 tags: Side-Project slideOptions: transition: slide backgroundTransition: none parallaxBackgroundSize: 'cover' parallaxBackgroundHorizontal: 0 parallaxBackgroundVertical: 0 parallaxBackgroundImage: https://i.imgur.com/o8QwPsZ.png spotlight: enabled: false --- <!-- .slide: data-background="https://i.imgur.com/loSLrKS.png" --> --- ## About me Hugo + 112 特選生 + 金華 / 政附 / 臺師大資工 + 👉 https://linktr.ee/whyhugo --- ## 開始之前 + 下載 [實作 notebook](https://github.com/whyhugo/SITCON-Camp-23-DataStructure/blob/main/SITCON%20Camp%202023%20%E4%B8%BB%E7%B7%9A%E8%AA%B2%E7%A8%8B%20-%20Python%20%E5%9F%BA%E7%A4%8E%E5%AE%B9%E5%99%A8%20-%20%E5%AD%B8%E5%93%A1.ipynb) + notebook 是啥?怎麼用? + preview + cell --- + [解答 notebook](https://github.com/whyhugo/SITCON-Camp-23-DataStructure/blob/main/staff/SITCON%20Camp%202023%20%E4%B8%BB%E7%B7%9A%E8%AA%B2%E7%A8%8B%20-%20Python%20%E5%9F%BA%E7%A4%8E%E5%AE%B9%E5%99%A8%20-%20%E8%AC%9B%E7%BE%A9.ipynb) --- ## What is Data Structure? ---- ### 以不同形式將資料儲存於記憶體 + List + Tuple + Set + Dictionary --- # List List 就像是公車🚌,可以乘載不同類型的乘客 ---- * 放入的資料類型不拘 * 放入的資料可以重複 * 位置(index)與元素(element) * 使用情境:使用彈性大,較能應付多樣需求 --- ## Create List `[ ]、list()` ```python= a_list = [] b_list = list() c_list = [1, 'a', 2.3, True] ``` ```python= #查看資料型態 type(a_list) ``` --- ### index & slice ```python= d_list = ['a', 'b', 'c', 'd', 'e', 'f'] ``` | | | | | | | | | -------- | -------- | -------- | -------- | -------- | -------- | -------- | | index (左→右) | 0 | 1 | 2 | 3 | 4 | 5 | | `d_list` | a | b | c | d | e | f | | index (右→左) | -6 | -5 | -4 | -3 | -2 | -1 | ---- ### try it~ list_name[start\:end:step] ```python= print(d_list) print(d_list[4]) print(d_list[:2]) print(d_list[3:]) print(d_list[:-2]) print(d_list[-5:-1:2]) ``` --- ## Add Value to List + `append(element)`:新增一個值到 List 的最後 + `extend(List)`:將另一個 List 的值串到目前 List 的後面 + `insert(index, element)`:指定位置插入元素 ---- ### `append(element)` ```python= a_list.append(2) ``` ### `extend(List)` ```python= a_list.extend(c_list) ``` ### `append(List)` ```python= a_list.append(c_list) ``` ---- ### `insert(index, element)` ```python= a_list.insert(2, '我在哪裡') print(a_list) ``` ---- ### list的加和乘 ```python= print(f'a_list: {a_list}') print(f'c_list: {c_list}') a_list = a_list + c_list print(f'a_list: {a_list}') ``` ```python= zero = [0] zero = zero * 5 print(zero) ``` --- ## Practice 在 [1,2,3,4,5] 中的偶數位置插入字串 'even' 變成👇 ['even', 1, 'even', 2, 'even', 3, 'even', 4, 'even', 5] ---- ### ❌錯誤寫法 ```python= numbers = [1,2,3,4,5] for index in range(len(numbers)): if index % 2 == 0: numbers.insert(index, 'even') print(numbers) ``` ---- ### 💡方法1 ```python= numbers = [1,2,3,4,5] target_list = [] for number in numbers: target_list.append('even') target_list.append(number) print(target_list) ``` ---- ### 💡方法2 ```python= numbers = [1,2,3,4,5] for index in range(len(numbers)+len(numbers)-1): if index % 2 == 0: numbers.insert(index, 'even') print(numbers) ``` --- ## It's your turn🫵 再寫出兩種不同於方法 1,方法 2,方法 3 的其他方法 ---- ### 💡參考解法 --- ## Modify Value in List ---- ### `list[index]` = new_element ```python= a_list[2] = 3 a_list ``` --- ## Delete Value in List - `remove(element)`:刪除 List 中第一個 element - `del List[index]`:刪除 List 中指定位置的 element - `pop(index)`:刪除並取出 List 中指定位置的 element,預設最後一個 ---- ### `remove(element)` ```python= a_list.remove(1) a_list ``` ---- ### `del List[index]` ```python= del a_list[4] a_list ``` ---- ### `pop(index)` ```python= element = a_list.pop(2) print(a_list, element) ``` ```python= element = a_list.pop() print(a_list, element) ``` ```python= a_list.pop() a_list ``` --- ## Pick Value from List ---- ### index ```python= a_list[-2] ``` ### slice ```python= a_list[:-1] ``` --- ## 常用函式 * list 的長度 * list 中指定元素的個數 * list 中是否存在指定元素 * list 排序、倒轉 * list 的最大值、最小值、總和 * 串聯 ---- ### list 的長度 + len(List) ```python= numbers = [1,1,2,6,5,2,3,4,5] len(numbers) ``` ---- ### list 中指定元素的個數 + List.count(element) ```python= numbers = [1,1,2,6,5,2,3,4,5] numbers.count(1) ``` ```python= strings = ['a','a','f','c','c','e','g','a'] strings.count('a') ``` ---- ### list 中是否存在指定元素 + element in List ```python= numbers = [1,1,2,6,5,2,3,4,5] 8 in numbers ``` ---- ### list 排序 & 倒轉 List.sort()、List.reverse() ```python= numbers = [1,1,2,6,5,2,3,4,5] numbers.sort() ``` ```python= numbers = [1,1,2,6,5,2,3,4,5] numbers.sort(reverse=True) ``` ```python= name = ['p', 'm', 'a', 'C', '-', 'N', 'O', 'C', 'T', 'I', 'S'] reversed_list = name[::-1] ``` ```python= name = ['p', 'm', 'a', 'C', '-', 'N', 'O', 'C', 'T', 'I', 'S'] name.reverse() ``` ---- ### list 的最大值、最小值、總和 + max(List) ```python= numbers = [1,1,2,6,5,2,3,4,5] max(numbers) ``` + min(List) ```python= numbers = [1,1,2,6,5,2,3,4,5] min(numbers) ``` + sum(List) ```python= numbers = [1,1,2,6,5,2,3,4,5] sum(numbers) ``` ---- ### 串聯 \<sep>.join(\<list>):用 sep 來串連列表元素,列表元素需皆為字串。 ```python= t_str = 'SITCON_Camp_2023' t_list = list(t_str) t_join = '$'.join(t_list) ``` --- ## It's your turn🫵 利用 list 中刪除 element 的方法, 將你的手機號碼中是偶數的數字刪除,再重新組合成一個新的文字串(string),並印出。 > 範例輸入: ``` id = '41349018' ``` > 範例輸出: ``` 1391 ``` ---- ### 💡參考解法 --- 補充 ### 列表建構(list comprehension) [ expression for item in list (if condition) ] expression可為函數或運算式、condition為條件式 ```python= # example 1 numbers = [x*3 for x in range(10) if x%2==0] # example 2 a = 'O P E N - S O U R C E' b = [str(x) for x in a.split()] ``` --- # Tuple Tuple 就像是 UBER🚕,訂車後就不能換其他人坐 ---- + Tuple 建立資料後就不能修改 + 使用情境:不希望資料內容被任意修改 ---- #### tuple 讀音? ![](https://hackmd.io/_uploads/Hk-jN0rLh.jpg) --- ## Create Tuple `( )、tuple()` ```python= a_tuple = () b_tuple = tuple() c_tuple = (1, 'a', 2.3, True) ``` ```python= d_tuple = 1,2 d_tuple ``` ---- > Tuple Packing/Unpacking ```python= x, y = 1, 2 #packing x,y = y,x #unpacking print(x, y) ``` ```python= my_tuple = 1, 2, 3 a, b, c = my_tuple print(a, b, c) ``` --- ## Add Value to Tuple try it~ ```python= a_tuple.add(1) ``` ```python= a_tuple.append(1) ``` ```python= a_tuple.extend(1) ``` ```python= a_tuple.insert(0, 1) ``` ---- ## ❌不允許增加! --- ## Modify Value in Tuple ```python= c_tuple[2] = 1 ``` ---- ## ❌不允許修改! --- ## Delete Value in Tuple ---- ## ❌不允許刪除! --- ## Pick Value from Tuple index ```python= c_tuple[-2] ``` slice ```python= c_tuple[:2] ``` --- > tuple 轉成 list ```python= list(c_tuple) ``` --- ## It's your turn🫵 用 list 單元介紹過的刪除方法來試試能不能刪除一個 tuple 內的 element,至少試兩種方法。 ---- ## 💡參考解法 --- ## It's your turn🫵 如何將一句話 '老虎老鼠傻傻分不清楚' 表示成 unigram (一個字一個字分開) 存在 list ---- ### 💡參考解法 --- ## ⏳Take a break~ 10 mins. --- # Set Set 就像是重型機車🏍️,只能載獨一無二的女朋友 ---- * Set 只能儲存不重複的值 * Set 沒有位置關係 * 使用情境:不想收集重複資料 --- ## Create Set `set()` ```python= a_set = set() b_set = {1,2,3,2} c_set = {1, "1"} ``` --- ## Add Value to Set * `add()`:增加一個 element * `update()`:增加多個 element ---- ### `add()` ```python= c_set.add(5) c_set ``` ---- ### `update()` ```python= c_set.update(b_set) c_set ``` ```python= c_set.update([3,6,7]) c_set ``` --- ## Modify Value in Set ```python= c_set[3] = 6 ``` ---- ❌沒有位置關係,無法修改值 --- ## Delete Value in Set + `remove(element)`:找不到元素時會產生錯誤 + `discard(element)`:找不到元素時不會產生錯誤 ---- ### `remove(element)` ```python= c_set.remove(1) c_set ``` ```python= c_set.remove(10) c_set ``` ---- ### `discard(element)` ```python= c_set.discard(2) c_set ``` ```python= c_set.discard(10) c_set ``` --- ## Pick Value from Set * 無法使用位置關係取值 * 可透過迴圈取值 ---- ```python= for element in c_set: print(element) ``` --- ## Practice 如何知道 string 中有那些不重複字 ```python= string = ''' 六合縣有個六十六歲的陸老頭,蓋了六十六間樓, 買了六十六簍油,堆在六十六間樓, 栽了六十六株垂楊柳, 養了六十六頭牛,扣在六十六株垂楊柳。 遇了一陣狂風起,吹倒了六十六間樓, 翻了六十六簍油,斷了六十六株垂楊柳, 打死了六十六頭牛,急煞了六合縣的六十六歲的陸老頭。''' ``` ---- ### 💡參考解法 --- # Dictionary Dictionary 就像是火車🚂,每個位置對應著一個乘客 ---- - Dictionary 會以 key-value (鍵值對) 的方式儲存 - key 不能重複,但 value 可重複 - 使用情境:不想建立太多list,希望能夠簡化資料結構 --- ## Create Dictionary `{ }`、`dict()` ```python= a_dict = {} b_dict = dict() c_dict = {'小明':1, '小英':2, '小瑜':3} ``` --- ## Add Value to Dictionary - `Dict [key] = value` => 較常用 - `update (Dict)` ---- ### `Dict [key] = value` ```python= c_dict['小強'] = 4 c_dict ``` ---- ### `update (Dict)` ```python= c_dict.update({'小台':5}) c_dict ``` --- ## Modify Value in Dictionary 與 Add Value to Dictionary 方式相同 - `Dict [key] = value` - `update (Dict)` ---- ### `Dict [key] = value` ```python= c_dict['小強'] = 5 c_dict ``` ---- ### `update (Dict)` ```python= c_dict.update({'小台':4, '小王':6}) c_dict ``` --- ## Delete Value in Dictionary - `pop (key)` - `del Dict [key]` ---- ### `pop (key)` ```python= c_dict.pop('小王') c_dict ``` ---- ### `del Dict [key]` ```python= del c_dict['小台'] c_dict ``` --- ## Pick Value from Dictionary - `Dict [key]` - `keys()`:取得所有的 key - `values()`:取得所有的 value - `items()`:取得所有的 key、value ---- ### `Dict [key]` ```python= c_dict['小明'] ``` ---- ### `keys()` ```python= c_dict.keys() ``` ```python= type(c_dict.keys()) ``` ```python= list(c_dict.keys()) ``` ```python= for key in c_dict.keys(): print(key) ``` ---- ### `values()` ```python= c_dict.values() ``` ```python= for value in c_dict.values(): print(value) ``` ---- ### `items()` ```python= c_dict.items() ``` ```python= for key, value in c_dict.items(): print(f'key 是 {key}, value 是 {value}') ``` --- ### dict 互包 Create ```python= staff = { "person1": { "name": "Sophia", "age": 25, "city": "Taipei City" }, "person2": { "name": "Andrew", "age": 30, "city": "San Francisco" } } ``` ---- #### Add, Modify & Delete Values ```python= # add staff["person1"]["gender"] = "Female" # modify staff['person2']["age"] = 23 # delete del staff["person2"]["city"] ``` ---- #### pick values ```python= print(staff["person1"]["name"]) print(staff["person2"]["age"]) print(staff["person1"]["city"]) print(staff["person2"]) ``` ---- #### 補充:list 包 dict ```python= staff_list = ['haha', 'SITCON', 20.23, 'Camp'] staff_list.append(staff) staff_list ``` --- ## It’s your turn🫵 製作一段文章的字數字典 ```python= # 魔戒三部曲,電影劇情簡介 paragraph = paragraph = ''' 魔戒現身 編輯 凱蘭崔爾敍述的序言講及索倫鑄造至尊魔戒以圖征服中土大陸,人類及精靈組成聯盟發動最後同盟戰役推翻了索倫。至尊魔戒落入埃西鐸手中。埃西鐸後來被半獸人所殺,至尊魔戒失落,在數千年後才被哈比人史麥戈所獲,因受魔戒的影響,史麥戈成為咕嚕。至尊魔戒急欲返回它的主人索倫手裡,於是離棄咕嚕,被哈比人比爾博·巴金斯所獲。 在比爾博111歲生日當天,比爾博將至尊魔戒讓給堂姪佛羅多·巴金斯。比爾博好友甘道夫經過探查後發現那就是失落的至尊魔戒,由於索倫急欲取得至尊魔戒,甘道夫囑咐佛羅多要儘速帶同魔戒離開夏爾。甘道夫趕往艾辛格與巫師的首領薩魯曼商討對策,薩魯曼透露索倫已派出戒靈奪取魔戒,認為中土自由子民無從抵抗,並邀請甘道夫和他一起加入索倫陣營。甘道夫拒絕,薩魯曼遂將甘道夫囚禁在歐散克塔。 佛羅多和夥伴山姆在趕往布理途中遇到皮聘及梅里。他們在布理遇到神行客,神行客亞拉岡帶領他們趕往瑞文戴爾。他們在風雲頂被戒靈襲擊,佛羅多被戒靈刺傷,生命危在旦夕。女精靈亞玟帶同佛羅多趕往瑞文戴爾,亞玟的父親愛隆救治了佛羅多。 愛隆召開會議商討至尊魔戒的事情。唯一摧毀至尊魔戒的方法就是將它丟進末日火山的火燄裡。佛羅多自願執行這個任務,其哈比人夥伴、甘道夫、亞拉岡、精靈勒苟拉斯、矮人金靂及剛鐸大將波羅莫都加入,組成魔戒遠征隊。他們原本計劃攀越卡蘭拉斯山,但受薩魯曼阻撓。魔戒遠征隊遂穿越摩瑞亞礦坑,他們在礦坑內受到半獸人、食人妖及炎魔的襲擊。甘道夫阻擊炎魔,雙雙墮入深淵。 失去甘道夫的魔戒遠征隊趕往羅斯洛立安謁見女王凱蘭崔爾。整頓以後,眾人沿安都因河朝帕斯加蘭進發。波羅莫急切於利用魔戒拯救剛鐸,於是嘗試從佛羅多手中奪取魔戒。佛羅多有感於魔戒的誘惑力驚人,於是決定獨自前往魔多。此時,魔戒遠征隊受到薩魯曼的強獸人襲擊,後悔行為魯莽的波羅莫為了保護梅里及皮聘而受重傷,梅里及皮聘被強獸人擄走。亞拉岡原諒了波羅莫,波羅莫在死前終承認了亞拉岡是他的國王。亞拉岡、勒苟拉斯及金靂為了營救梅里及皮聘而追蹤強獸人。另一方面,佛羅多向北而行,渡過安都因河,尾隨而至的山姆誓死追隨佛羅多。 雙城奇謀 編輯 電影開首呼應首部曲,甘道夫在摩瑞亞的橋上與炎魔對峙,甘道夫與炎魔墜落深淵。佛羅多從夢境中醒過來,與山姆繼續行程,他們被希望奪回魔戒的咕嚕襲擊,佛羅多及山姆制伏了他,佛羅多正需要一名嚮導引領他們前往魔多,於是讓他引路。 在洛汗,薩魯曼那一支擄走梅里及皮聘的強獸人橫過草原向艾辛格進發,勒苟拉斯、金靂及亞拉岡尾隨追蹤。薩魯曼亦在洛汗國王身邊安插了葛力馬·巧言控制著希優頓。薩魯曼聚集大軍展開攻勢,沿途燒殺破壞,希優頓的兒子希優德戰死。希優頓之甥伊歐墨責難葛力馬,反被葛力馬驅逐,伊歐墨聚集了一些忠實的部下離去。 佛羅多及山姆橫過死亡沼澤,一路上躲避戒靈的空中游戈,終於抵達黑門。黑門被重兵駐守,佛羅多及山姆兩人束手無策,咕嚕建議他們透過另一條秘密的階梯潛入魔多。同時,被驅逐的伊歐墨領導騎兵在法貢森林附近殲滅了梅里及皮聘所在的那一支強獸人,梅里及皮聘逃進法貢森林,並遇到樹鬍。 勒苟拉斯、金靂及亞拉岡遭遇伊歐墨,得知梅里及皮聘生還機會渺茫,亞拉岡仔細查察之下發現梅里及皮聘進入了法貢森林,亦尾隨進入,他們在森林裡遇到白袍巫師,發現竟是重生的甘道夫。他們聯袂來到洛汗都城伊多拉斯,消除了薩魯曼對希優頓的控制,驅逐葛力馬。恢復自我的希優頓得知薩魯曼的威脅後決定將洛汗人民撤至要塞聖盔谷。甘道夫深明以洛汗的現有兵力堅守聖盔谷也不能抵擋薩魯曼的大軍,於是出發前往尋找伊歐墨。 佛羅多、山姆及咕嚕目擊剛鐸軍隊伏擊一支南方人,佛羅多及山姆被剛鐸軍隊擒獲。洛汗人在撤往聖盔谷途中遭遇薩魯曼的狼騎士襲擊,亞拉岡墜落峭壁失蹤。在瑞文戴爾,亞拉岡的戀人亞玟在父親的勸說下出發,準備離開中土大陸。 佛羅多及山姆被帶到漢那斯安南,剛鐸大將法拉墨得悉佛羅多持有魔戒,為了向父親證明他的價值,他決定將佛羅多及至尊魔戒帶到剛鐸。亞拉岡抵達聖盔谷,準備迎戰薩魯曼,一支受愛隆所命的精靈部隊亦赴援。法貢森林的樹鬍召開樹人會議,商討是否應介入戰爭。 聖盔谷之戰在大雨下展開,洛汗戰士奮力抵擋。樹人會議最終決定不介入戰爭,皮聘誘導樹鬍將他們帶到南面,樹鬍發現法貢森林南部的林木被薩魯曼砍伐用作燃料,樹鬍震怒,遂帶領樹人報復。 洛汗一方的形勢急轉直下,亞拉岡等人帶領守軍進行最後的衝鋒,清晨時分,甘道夫引領援軍抵達戰場,擊敗了強獸人大軍。樹人亦控制了艾辛格。法拉墨等人到達奧斯吉力亞斯,該處正在進行戰鬥,法拉墨得悉佛羅多等人的任務,決定釋放他們。咕嚕感到被佛羅多背叛,於是引導兩名哈比人前往「她」那裡。 王者再臨 編輯 電影開始時講述哈比人德戈及史麥戈發現魔戒的過程,史麥戈殺死德戈並奪取魔戒,從此隱匿深山裡。 返回現在,亞拉岡等人來到艾辛格,會合梅里及皮聘,薩魯曼想告知甘道夫魔多的事情時,被葛力馬‧巧言從後偷襲,跌下塔底死亡,真知晶球從袍內流出,皮聘拾起了薩魯曼的真知晶球,但被甘道夫取去。希優頓在返回伊多拉斯後舉行慶典。佛羅多等人逐漸靠近魔多,咕嚕的陰謀面也逐漸呈現,他計劃引導哈比人到西力斯昂哥,讓屍羅殺死他們,他便可獲得魔戒,又開始離間佛羅多與山姆。 出於好奇的皮聘偷看真知晶球,他看到燃燒中的聖白樹,索倫透過心靈接觸拷問皮聘。甘道夫以此推斷索倫準備進攻米那斯提力斯,於是攜同皮聘趕往警示剛鐸攝政王迪耐瑟二世。前往灰港岸路上的亞玟預見了她和亞拉岡的兒子,她返回瑞文戴爾說服父親,又讓父親重鑄納希爾聖劍。 甘道夫及皮聘到達米那斯提力斯,謁見因波羅莫之死而悲傷的迪耐瑟二世。皮聘向迪耐瑟效忠以回報波羅莫的犠牲,悲傷的迪耐瑟無意採取任何措施回應索倫的行動。佛羅多等人路經米那斯魔窟附近,目睹安格馬巫王派遣軍隊出擊的情況。甘道夫指示皮聘燃點烽火台向洛汗求援。 魔多大軍渡過安都因河向剛鐸控制的奧斯吉力亞斯進攻,以法拉墨為首的剛鐸守軍大敗,撤返米那斯提力斯。敗軍被戒靈追擊,甘道夫營救他們,法拉墨向甘道夫透露了佛羅多等人的情況。 攻克奧斯吉力亞斯後,安格馬巫王下令要殺絕米那斯提力斯的人類。迪耐瑟不滿法拉墨棄守奧斯吉力亞斯,要他不惜一切代價奪回奧斯吉力亞斯。咕嚕繼續離間佛羅多及山姆,終使佛羅多趕走山姆。 法拉墨的自殺式攻勢全軍覆沒,他本人亦重創而回。同時,愛隆趕來會面亞拉岡,奉上納希爾聖劍,並促請亞拉岡以此劍召喚登哈洛的亡者參戰,亞拉岡遂聯同金靂、勒苟拉斯連夜出發。一直傾慕亞拉岡的洛汗王女伊歐玟懇求亞拉岡留下來,亞拉岡從愛隆口中得知亞玟為了他放棄了精靈的永生,拒絕了伊歐玟。翌晨,希優頓領導六千騎兵赴援,伊歐玟及梅里亦暗中混入軍隊。 魔多大軍開始圍攻米那斯提力斯,迪耐瑟誤以為重傷的法拉墨已離世,絕望及失去子嗣打垮了迪耐瑟,他下令剛鐸守軍放棄抵抗,甘道夫制伏了他,並指揮作戰。咕嚕引領佛羅多來到屍羅巢穴,佛羅多被屍羅的毒針所傷,山姆趕到,並擊退屍羅。山姆誤以為佛羅多已死,昏迷的佛羅多被半獸人帶走。 魔多大軍擊破了城門,守軍不支退守內城。此時,洛汗援軍抵達一度擊潰了魔多大軍,但隨著南方人的增援,魔多大軍在猛瑪的掩護下再次集結兵力。安格馬巫王重創希優頓,伊歐玟為了保護國王挺身與安格馬巫王對峙,亦被重創,梅里在危急關頭介入,與伊歐玟聯手殺死安格馬巫王。亞拉岡透過海盜船帶領著亡者軍隊抵達,擊敗魔多軍隊。希優頓傷重逝世。亡者軍隊在履行了諾言後,亞拉岡將他們釋放,亡靈隨風而去。西力斯昂哥的守軍因佛羅多的一件秘銀甲衣而爆發內鬨,大部分半獸人守軍都死於這次衝突,山姆得以營救佛羅多,兩人向末日火山進發。 甘道夫得知一萬魔多士兵集結在西力斯昂哥與末日火山之間,堵住了佛羅多的去路。亞拉岡提議率領部隊開赴黑門以吸引索倫的注意力。 山姆及佛羅多即將抵達目的地,咕嚕在此時現身並襲擊兩人。佛羅多最終在末日火山被至尊魔戒控制,他戴上了魔戒將之據為己有,咕嚕上前搶奪魔戒,咬斷了佛羅多的手指,咕嚕得到魔戒後非常興奮,佛羅多上前糾纏,兩人在懸崖失足。佛羅多幸得山姆解救,咕嚕與至尊魔戒都被熾熱的熔岩摧毀。力量與至尊魔戒相連的索倫終告失敗,永遠失去了力量。正在黑門前作戰的半獸人及戒靈亦受到牽連。 末日火山爆發,正當介佛羅多及山姆都以為他們沒法再看見夏爾時,甘道夫與巨鷹拯救了他們。除了波羅莫外,魔戒遠征隊在米那斯提力斯團圓。亞拉岡被加冕為剛鐸國王,並再會亞玟。哈比人返回家鄉,山姆迎娶小玫·卡頓,佛羅多續作了西境紅皮書,他仍受到魔戒影響,他知道他無法在中土大陸平穩地生活,於是決定與甘道夫、比爾博、愛隆、凱蘭崔爾前往灰港岸,乘上離開中土大陸的船隻。山姆、皮聘及梅里各自回家,山姆最終以一句「我回來了」作終。 ''' # refer to https://zh.m.wikipedia.org/zh-tw/%E9%AD%94%E6%88%92%E9%9B%BB%E5%BD%B1%E4%B8%89%E9%83%A8%E6%9B%B2 ``` ---- #### 1 請將 paragraph 變成 unigram #### 2 請找出 paragraph 有那些不重複字 #### 3 請計算每個不重複字在 paragraph 中出現幾次,並存成 Dictionary #### 4 上題的 Dictionary 中出現最多次數的前 10 名是哪些字?各有多少次? > hint: 使用 sorted ---- ### 💡參考解法1 ---- ### 💡參考解法2 ---- ### 💡參考解法3 ---- ### 💡參考解法4 --- ## It's your turn🫵 寫一個程式,找出你的電話號碼中最大,最小的正整數(不含0)。 > 範例輸入: ``` id = '41341017' ``` > 範例輸出: ``` Max:7 Min:1 ``` ---- ### 💡參考解法 --- ## It's your turn🫵 如果將一組序號的數字兩兩一組依序取出,找出這組序號中最大,最小的正整數(不含0)。譬如 23034008 中,"23, 30, 3, 34, 40, 8" 最大的是 40, 最小的是 3。 > 範例輸入: ``` id = '4134100570745000223' ``` > 範例輸出: ``` Max: 74 Min: 2 ``` ---- ### 💡參考解法 --- ## It's your turn🫵 `name = '一二三'` `id = 'o123456789'` 寫一個程式,利用 list, insert or append or extend 等方法, 印出 `o12一345二678三9` ---- ### 💡參考解法 --- ### Dict 實作舉例 - affine cipher For each letter of cipher text its position in the alphabet is the position of the original letter multipied by 4 and shifted by 15 character. Alphabet consists of letters from 'a' to 'z' and symbol '_'. Shift over alphabet is cyclic. Letter 'a' has position 0, symbol '_' has position 26. 密文:auxkluglgrlxrrf ---- ```python= import string # 產出指定 alphabet s = string.ascii_lowercase # a-z s += '_' # dict 解密轉換表 d = {} for c in range(len(s)): d[s[(c*4 + 15) % 27]] = s[c] # 使用 dict 解密轉換表逐字解出答案 ciphertext = 'auxkluglgrlxrrf' s1 = '' for x in ciphertext : s1 += d[x] # 印出答案 print(s1) ``` --- ## Recap - `List [ ]`:使用彈性大 - `Tuple ( )`:不能修改資料 - `Set { }`:不能存重複資料,沒有位置關係 - `Dictionary { key:value }`:以鍵值對關係儲存 - 資料結構彼此間可以互包或自包. ex. Dict 的 value 為 Dict、List 裡面包含一個 Dict --- <!-- .slide: data-background="https://i.imgur.com/9LfQqla.png" --> --- <!-- .slide: data-background="https://i.imgur.com/BcqtlOG.png" -->

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully