存檔與填寫注意事項:
假設你叫王小明,學號41078392。
存檔時,請用你的學號當延伸檔名。學號為41078392,則請存成homeworkX-41078392.Rmd, X為這次作業號碼。(記得使用:save with enconding, 然後選utf-8)
本文檔開始的Frontmatter中,studentID(即key)其value目前為“你的學號”,以上面學號為例則必需改成“41078392”;而studentName(key)其value目前為“你的名字”,以上面名字為例則必需改成“王小明”
請先執以下code chunk開啟RStudio底下的Python環境:
use_python
的路徑及use_condaenv
的沙盒環境必需改成你電腦的對應設定。每一題會有設好的ans chunk,請將你的程式寫在裡面,切勿自行加新的python chunk。
每一題都需要你把答案存在指定的物件,並在「最後一行」打物件名稱。以下例,若答案是2+3
並要你存在答案物件test裡,那你的程式如下:
test = 2+3 # 答案存在指定test物件
test # 最後一行打指定物件名稱
執行以下程序讀入第一次作業解答內容成為list of strings形式的物件filelines
import requests
response=requests.get("https://www.dropbox.com/s/58e3g2dq8urecw0/hw1-ans.Rmd?dl=1")
filelines=response.text.split("\n")
你也可以執行以下R指示在獨立視窗看filelines的各行內容。
View(py$filelines)
```{<語言> ...} codes ```
其中開頭的```{<語言> …}
標明了是使用python或R程式語言。寫一個chunk_lang(chunk_start)
函數:
chunk_start: a string. ```{<語言> …}
內容的文字。
return: a string. 若<語言>為r則回傳’r’, 為python則回傳’python’。(本題可假設只有r, python兩種可能)
hint: 答案正確的話,執行
chunk_start=chunk_start_example1="```{python}"
chunk_start_example2="```{r }"
chunk_lang(chunk_start_example1)
chunk_lang(chunk_start_example2)
會分別得到
'python'
'r'
# chunk_lang
找出filelines中所有以「```」開頭的元素位置index,並計錄在index_3ticks這個list答案物件裡。
hint: index_3ticks的前3個值是26,32,39。
# index_3ticks
產生一個名為dict_chunks的dictionary答案物件, 它有三個keys:
‘start’: a list. 每個code chunk開始的位置值為其value。
‘end’: a list. 每個code chunk結束的位置值為其value。
‘language’: a list. 每個code chunk所使用的程式語言。
此題可使用1.1小題的chunk_lang()
函數。
hint: 答案正確的話,執行:
dict_chunks['start'][0]
dict_chunks['end'][0]
dict_chunks['language'][0]
會顯示:
27
31
'r'
# dict_chunks
產生一個list of strings答案物件python_code_list,其內容由hw1-ans.Rmd裡所有python code lines取出形成。
hint: 答案正確的話,執行:
python_code_list[0]
python_code_list[1]
python_code_list[2]
會顯示:
'test = 2+3 # 答案存在指定test物件'
'test # 最後一行打指定物件名稱'
'import random'
# python_code_list
完成python_code_list後,若執行以下程序將得到一個只有python code的hw1-ans.py檔案。
f2=open("hw1-ans.py","w")
f2.writelines(python_code_list)
f2.close()
執行以下程序得到台灣2019年不同舞蹈的表演資訊danceInfo:
import requests
response=requests.get("https://cloud.culture.tw/frontsite/trans/SearchShowAction.do?method=doFindTypeJ&category=3")
danceInfo=response.json()
每一個danceInfo記錄皆有一個獨一無二的UID值,請建立一個名為allUIDs的list答案物件,其內容為danceInfo的所有UID值,且danceInfo裡的第i筆dance及UID值會存在allUIDs對應的相同i位置,即第i筆dance的UID會是allUIDs[i]: hint: 答案正確的話,執行:
allUIDs[0]
allUIDs[1]
會顯示:
'5c76c26ed083a35404c3dce7'
'5c79825ad083a35404c3de58'
# allUIDs
寫一個search_by_uid(uid)
函數:
uid: a string. 表演的uid值
return: a list. 為該表演在danceInfo裡的showInfo訊息。
答案物件為search_by_uid
沒有小括號。
hint: 答案正確時,打search_by_uid('5d93a403d083a331549834d6')
會得到:
[{'time': '2019/10/19 15:00:00', 'location': '臺東縣綠島鄉將軍岩20號', 'locationName': '綠島人權紀念公園', 'onSales': 'N', 'price': '', 'latitude': '0.0', 'longitude': '0.0', 'endTime': '2019/10/19 17:00:00'}]
# search_by_uid
找出danceInfo裡uid為’5d93a403d083a331549834d6’的資料並在其’showInfo’添加如下的dictionary形式資訊:
{'time': '2019/10/30 15:00:00', 'location': '新北市三峽區大學路151號', 'locationName': '國立臺北大學'}
本題完成後請unmarkd (remove #) 答案區的兩行程式並保持那兩行在最後的位置,用來double check該展演資訊是否有真的有更改。
hint: 若答案正確,updated_result
內容值為:
[{'time': '2019/10/19 15:00:00', 'location': '臺東縣綠島鄉將軍岩20號', 'locationName': '綠島人權紀念公園', 'onSales': 'N', 'price': '', 'latitude': '0.0', 'longitude': '0.0', 'endTime': '2019/10/19 17:00:00'}, {'time': '2019/10/30 15:00:00', 'location': '新北市三峽區大學路151號', 'locationName': '國立臺北大學'}]
# updated_result
考慮如下的矩陣M:
\[M=\left[\begin{array}{ccccc} 7 & -9 & -5 & 10 & -13\\ 12 & -6 & -3 & -6 & -14\\ 7 & -5 & -4 & 1 & 5 \end{array}\right]\] 我們可以使用list呈現如下:
M=[[7, -9, -5, 10, -13],
[12, -6, -3, -6, -14],
[7, -5, -4, 1, 5]]
令\(M_{ij}\)代表M中第i行(row)第j列(column)的值,請取出\(M_{24}\) 存在答案物件M24
# M24
取出矩陣M的第2,3 row的第3到5 colum的所有值,即: \[M_{2:3,3:5}=\left[\begin{array}{ccccc} -3 & -6 & -14\\ -4 & 1 & 5 \end{array}\right]\] 並將它存在答案物件M_partial
# M_partial
請寫一個matrix_get(matrix, rangeR, rangeC)
函數:
matrix: a list. list形式的矩陣
rangeR: 由range()
產生,構成代表所要取的row index範圍
rangeC: 由range()
產生,構成代表所要取的col index範圍
return: a list. 為對應rangeR,rangeC所取出的子矩陣,以nested list形式呈現矩陣。
hint: 答案正確的話例如,輸入matrix_get(M,range(1,3),range(2,5))
會得到:
[[-3, -6, -14], [-4, 1, 5]]
即前一小題答案值。
# matrix_get