P01-Practice#
Q1. Counting fruits#
現有一堆水果,假設有6個,請計算每種水果各出現幾次。假設水果為['apple', 'banana', 'cherry', 'apple', 'banana', 'apple']
。這個練習主要是為了讓你熟悉程式碼的撰寫,來增加手感,所以請務必自己打打看。
# Create a list of fruits
# Create a fruit_dict dictionary to count the number of fruits
# Using a for loop, count the number of each fruit in the list
# Print the fruit_dict dictionary
Q2. NTU’s GPA Grading#
參考「成績百分制轉等第制」的程式碼(如下),再參考台大百分制轉等第的規定,寫出完整且合乎規定的百分制的轉換if-else邏輯判斷式。
# Initializing level frequency
grade_dict = {'F':0, "C":0, "B":0, "A":0}
# Counting level frequency
for g in grades:
if 100 >= g >= 80:
grade_dict["A"] += 1
elif 79 >= g >= 72:
grade_dict["B"] += 1
elif 71 >= g >= 60:
grade_dict["C"] += 1
else:
grade_dict["F"] += 1
print(grade_dict)
grades = [45, 55, 100, 90, 89, 79, 38, 61, 71, 91, 85, 75, 65, 44, 66, 77, 88]
# Your code should be here
用for-loop列印出每個等第個有多少人?
# Your code should be here
Q3. Sorting keyword frequency#
仿照教學範例中最後一節的排序函式,將Wikipedia詞頻計算結果由大而小排序,並用for-loop將排序結果印出來。觀察一下,詞頻高的字詞有哪些呢?一共有三種寫法,請三種寫法都寫寫看。
import wikipedia # import a 3rd-party library into runtime
string_a = wikipedia.summary("Big_data", sentences = 10) # get data by 3rd-party function
import string
translator = str.maketrans('','',string.punctuation)
string_a = string_a.translate(translator)
string_a = string_a.lower()
from collections import Counter
words = string_a.split()
word_freq = Counter(words)
# Method 1: sorting tuples
# Method 2: sort by dict.get()
# Method 3: sort by Counter's most_common() function
Q4. Plotting#
上述Counter()
的計算結果,我希望把他繪製成橫向的長條圖,且需要排序過資料,讓最多的項目在最上面。請問要怎麼做?(問ChatGPT看看會得到什麼答案?)
# your code should be here
Visualized by seaborn or plotly?: 還有沒有其他種視覺化方法
# Your code should be here
Q5. (Option) Frequency of typing#
我想知道,讓一個人隨機打字的時候,除了按到標點符號外,他最常打的26個英文字母為何,我該如何寫程式來測試?問問看ChatGPT。
# your code should be here