You are on page 1of 60

HW7 遊戲平台(100分) National Taiwan University

• 題目說明: 使用者資訊紀錄在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

• pip install pandas

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

as it is a mapping of index values to data values. It can be substituted


into many functions that expect a dict:

27
• The isnull and notnull functions in pandas should be used to detect National Taiwan University

missing data

• Replace NaN with


other value

28
National Taiwan University

• Both the Series object itself and its index have a


name attribute, which integrates with other key
areas of pandas functionality

• A Series’s index can be altered in


place by assignment

29
練習
National Taiwan University

• 題目: ch10_stocks.txt 記錄了四檔股票於2020/11/27的收盤價,


請同學們寫程式讀取股票及其對應的收盤價,並轉成 Series 物件
表示(如下圖所示)

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

with Series, and the columns are placed in sorted order:

• If you specify a sequence


of columns, the
DataFrame’s columns will
be exactly what you pass

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

Possible data inputs to DataFrame constructor

37
練習
National Taiwan University

• 題目:請用 DataFrame 建立表格中的資料,計算買賣超股數,並


且加入 DataFrame
證券代號 證券名稱 買進股數 賣出股數
6116 彩晶 14,676,000 10,729,000
1303 南亞 6,528,592 5,056,000
2884 玉山金 8,118,806 13,145,000
1102 亞泥 1,578,967 2,311,000

38
參考解答
National Taiwan University

39
National Taiwan University

Reindexing

40
Dropping entries from an axis National Taiwan University

41
National Taiwan University

Dropping entries from an axis

42
National Taiwan University

Indexing, selection, and filtering

43
National Taiwan University

Indexing, selection, and filtering

44
National Taiwan University

Indexing, selection, and filtering

45
National Taiwan University

Read csv

46
National Taiwan University

set DataFrame’s index

47
National Taiwan University

set DataFrame’s index

48
National Taiwan University

Find which pokemon’s HP higher than 100

49
National Taiwan University

50
National Taiwan University

51
National Taiwan University

Change values in DataFrame

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

You might also like