Professional Documents
Culture Documents
• 題目說明: 使用者資訊紀錄在user_info.json裡,包含帳號、密碼和餘額
• 執行時,執行畫面中使用者有三個選項:
1. 輸入“登入” user_info.json沒有特定格式,
• 要求使用者輸入帳號密碼
• 確認帳號密碼是否正確 程式可以正確執行即可
• 正確: 印出帳號餘額,並進入遊戲大廳
• 錯誤: 跟使用者說查無帳號或密碼錯誤,並跳回執行畫面
2. 輸入“新帳號”
• 請使用者輸入帳號
• 確認帳號跟user_info.json裡是否重複
• 重複: 跟使用者說帳號已被使用,並跳回執行畫面
• 沒重複: 請使用者輸入密碼,給玩家1000元初始資金,新增至user_info.json,並進
入遊戲大廳
3. “離開”: 結束遊戲
1
• 成功登入後,進入遊戲大廳,進行遊戲或儲值後,更新user_info.json National Taiwan University
• 遊戲大廳中使用者有五個選項:
1. 輸入“撲克牌”進行 和電腦抽㇐張撲克牌比大,賠率1:1
• 請使用者輸入下注金額,若帳戶餘額不足,告知餘額不足,跳回遊戲
大廳 先依照數字比較撲克
• 輸出執行結果,並更新user_info.json,跳回遊戲大廳 牌之間的大小,若遇
到相同數字,再比較
2. 輸入“骰寶” 花色
• 流程參照HW5_3,若帳戶餘額不足,告知餘額不足,跳回遊戲大廳
• 輸出結果後,更新user_info.json,跳回遊戲大廳
3. “儲值” :輸入儲值金額,更新user_info.json,跳回遊戲大廳
4. “查詢餘額”: 輸出使用者的餘額
每個功能都必須實作,如有錯誤,
5. “離開” 助教將斟酌扣分
2
National Taiwan University
3
National Taiwan University
4
National Taiwan University
5
National Taiwan University
6
National Taiwan University
7
National Taiwan University
8
National Taiwan University
9
National Taiwan University
10
National Taiwan University
HW8_1 (50分)
• 題目:請寫程式計算在股票版的文章中,使用者輸入的任意㇐檔股
票被提到的次數
• Example: 同學們處理資料時可能會遇到資
料不完整或結構不相同的情況,
• Input: 台積電
該情況並非提供的資料有誤,請
• Output: 4620
同學們寫程式處理例外情況,使
程式能順利執行
• Input: 三星
• Output: 1333 本題需計算article_title、content、
push_content內提到該股票的數量
11
National Taiwan University
12
National Taiwan University
13
本題計算content內的數量即可 National Taiwan University
HW8_2 (50分)
• 題目:請寫程式計算股票版的文章中
1. 包含E_term 的文章有幾篇
2. 包含U_term的文章有幾篇
3. 包含P_term的文章有幾篇
4. 同時包含E_term, U_term和P_term的文章有幾篇
並將結果存成 “學號_EPU_count.json”, 格式如下圖
14
EPU 關鍵字 National Taiwan University
• E_term = ['經濟','景氣']
• U_term = ['不確定', '難以預料', '不安', '惶恐', '不穩定', '金融危機', '疑惑', '不
透明', '難以估計', '動盪', '不明朗', '難以預計', '未明', '不明確', '難以預測', '歧
見']
• P_term = ['政策', '措施', '施政', '稅', '稅制', '課稅', '稅務', '稅改', '年金', '年改',
'年金改革', '預算', '財政', '財源', '赤字', '政府支出', '國債', '政府債務', '舉債', '
法案', '法規', '投資', '改革', '外匯存底', '外匯儲備', '準備金', '利率', '匯率', '貨
幣政策', '公開市場操作', '政府', '官邸', '國會', '立法院', '行政院', '總統府', '內
閣', '立法院院長', '行政院院長', '總統', '中央銀行', '央行', '經濟結構', '產業結
構', 'ECFA', '服貿協議', '貿易協議']
http://www.econ.ntu.edu.tw/ter/new/data/new/forthcoming/108-001.pdf 15
National Taiwan University
16
National Taiwan University
17
National Taiwan University
18
National Taiwan University
19
Crawl PTT National Taiwan University
20
National Taiwan University
GitHub
21
National Taiwan University
Data Preprocessing
顏安孜 An-Zi Yen
azyen@nlg.csie.ntu.edu.tw
National Taiwan University
Pandas
• pandas is a fast, powerful, flexible and easy to use open source data
analysis and manipulation tool, built on top of the Python
programming language
• Data structures: Series and DataFrame
• https://pandas.pydata.org/pandas-docs/stable/index.html
23
No module named ' ' National Taiwan University
24
National Taiwan University
Series
• A Series is a one-dimensional array-like object containing an array of
data (of any NumPy data type) and an associated array of data labels,
called its index.
25
National Taiwan University
26
• Another way to think about a Series is as a fixed-length, ordered dict, National Taiwan University
27
• The isnull and notnull functions in pandas should be used to detect National Taiwan University
missing data
28
National Taiwan University
29
練習
National Taiwan University
30
參考解答
National Taiwan University
31
National Taiwan University
DataFrame
• A DataFrame represents a tabular, spreadsheet-like data structure
containing an ordered collection of columns, each of which can be a
different value type (numeric, string, boolean, etc.).
• The DataFrame has both a row and column index; it can be thought of
as a dict of Series (one for all sharing the same index).
32
• The resulting DataFrame will have its index assigned automatically as
National Taiwan University
33
National Taiwan University
34
National Taiwan University
35
• Another common form of data is a nested dict of dicts format: National Taiwan University
numpy.ndarray
36
National Taiwan University
37
練習
National Taiwan University
38
參考解答
National Taiwan University
39
National Taiwan University
Reindexing
40
Dropping entries from an axis National Taiwan University
41
National Taiwan University
42
National Taiwan University
43
National Taiwan University
44
National Taiwan University
45
National Taiwan University
Read csv
46
National Taiwan University
47
National Taiwan University
48
National Taiwan University
49
National Taiwan University
50
National Taiwan University
51
National Taiwan University
52
National Taiwan University
HW9
1. 命名方式:
• 學號_HW9.py
• (只有㇐題)
2. 繳交程式碼即可 (修正版的作業需繳交報告)
3. 第㇐版作業繳交期限: 2020/12/9(三) 23:59:59
4. 修正版作業繳交期限: 2020/12/13(日) 23:59:59
5. 上傳至數位學習平台(iLMS)
53
HW9 (100分) 投資有風險,理財需謹慎 National Taiwan University
• 題目:小明是投資新手,他想進場買股票,可是不知道買哪幾檔股
票比較好。他聽說跟著外資買準沒錯(?),請大家幫他寫程式計算每
㇐檔股票的買賣超平均值,並且將買賣超平均值以降序排序,取 top
N 的股票當作建議的投資標的。
• 本題必須使用 pandas (只能import os, datetime, pandas, math,若需
要 import 其他 library,可以寄信詢問助教)
• 助教測試程式時,會把 m 份 csv 檔放在和同學們的程式同㇐層資料
夾,請同學們按照此情境撰寫讀取程式。
• 助教所使用的測資不同,但是格式和提供給大家的範例csv格式相同,
檔名命名方式也相同(年月日.csv)
54
National Taiwan University
HW9
• 資料:
• 每天的股票數量不同是原始下載的檔案就有這樣的情況,並非提
供的資料有誤,請同學們寫程式處理此情況,不讓程式發生error
55
參考結果 • 讓使用者輸入N
National Taiwan University
• 輸出 DataFrame
• 若同學們需要更多
測資,可以自己動
手算
56
HW9 提示
National Taiwan University
• 日期處理:
• strptime()
• strftime() • 語法詳細用法留給同學們
自行查詢
• 計算平均:
• mean()
• 留意NaN的處理
57
HW9 加分題
National Taiwan University
1. 命名方式: 學號_HW9_plus.py
2. 題目: HW9 改使用 csv library,而不用 pandas (除了 pandas,可以
import 其他 library)
3. 請繳交程式碼,程式碼中需有註解輔助說明。
4. 繳交期限: 2020/12/9(三) 23:59:59 • 讓使用者輸入N
5. 上傳至數位學習平台(iLMS) • 輸出 top N 的證券名稱
6. 於繳交期限前上傳程式的同學,且程式可以執行,HW9加 10 分
7. (沒有修正作業上傳的機會)
58
National Taiwan University
LAB
• Given an array nums containing n distinct numbers in the range [0, n],
return the only number in the range that is missing from the array.
• Example 1:
• Input: [3,0,1]
• Output: 2
• Example 2:
• Input: [9,6,4,2,3,5,7,0,1]
• Output: 8
59
參考解答
National Taiwan University
60