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)
---------------------------------------------------------------------------
PageError Traceback (most recent call last)
Input In [4], in <cell line: 2>()
1 import wikipedia # import a 3rd-party library into runtime
----> 2 string_a = wikipedia.summary("Big_data", sentences = 10) # get data by 3rd-party function
4 import string
5 translator = str.maketrans('','',string.punctuation)
File ~/opt/anaconda3/lib/python3.9/site-packages/wikipedia/util.py:28, in cache.__call__(self, *args, **kwargs)
26 ret = self._cache[key]
27 else:
---> 28 ret = self._cache[key] = self.fn(*args, **kwargs)
30 return ret
File ~/opt/anaconda3/lib/python3.9/site-packages/wikipedia/wikipedia.py:231, in summary(title, sentences, chars, auto_suggest, redirect)
216 '''
217 Plain text summary of the page.
218
(...)
226 * redirect - allow redirection without raising RedirectError
227 '''
229 # use auto_suggest and redirect to get the correct article
230 # also, use page's error checking to raise DisambiguationError if necessary
--> 231 page_info = page(title, auto_suggest=auto_suggest, redirect=redirect)
232 title = page_info.title
233 pageid = page_info.pageid
File ~/opt/anaconda3/lib/python3.9/site-packages/wikipedia/wikipedia.py:276, in page(title, pageid, auto_suggest, redirect, preload)
273 except IndexError:
274 # if there is no suggestion or search results, the page doesn't exist
275 raise PageError(title)
--> 276 return WikipediaPage(title, redirect=redirect, preload=preload)
277 elif pageid is not None:
278 return WikipediaPage(pageid=pageid, preload=preload)
File ~/opt/anaconda3/lib/python3.9/site-packages/wikipedia/wikipedia.py:299, in WikipediaPage.__init__(self, title, pageid, redirect, preload, original_title)
296 else:
297 raise ValueError("Either a title or a pageid must be specified")
--> 299 self.__load(redirect=redirect, preload=preload)
301 if preload:
302 for prop in ('content', 'summary', 'images', 'references', 'links', 'sections'):
File ~/opt/anaconda3/lib/python3.9/site-packages/wikipedia/wikipedia.py:345, in WikipediaPage.__load(self, redirect, preload)
343 if 'missing' in page:
344 if hasattr(self, 'title'):
--> 345 raise PageError(self.title)
346 else:
347 raise PageError(pageid=self.pageid)
PageError: Page id "big date" does not match any pages. Try another id!
# Method 1: sorting tuples
# Method 2: sort by dict.get()
# Method 3: sort by Counter's most_common() function
data 23
and 15
of 11
to 9
big 8
the 8
with 5
that 4
or 4
analysis 4
in 4
analytics 4
sets 3
large 3
a 3
business 3
devices 3
refers 2
are 2
complex 2
by 2
software 2
higher 2
challenges 2
information 2
volume 2
variety 2
sampling 2
thus 2
for 2
veracity 2
can 2
value 2
from 2
size 2
is 2
available 2
new 2
scientists 2
including 2
internet 2
informatics 2
as 2
too 1
be 1
dealt 1
traditional 1
dataprocessing 1
application 1
many 1
fields 1
rows 1
offer 1
greater 1
statistical 1
power 1
while 1
complexity 1
more 1
attributes 1
columns 1
may 1
lead 1
false 1
discovery 1
rate 1
include 1
capturing 1
storage 1
search 1
sharing 1
transfer 1
visualization 1
querying 1
updating 1
privacy 1
source 1
was 1
originally 1
associated 1
three 1
key 1
concepts 1
velocity 1
presents 1
previously 1
allowing 1
only 1
observations 1
fourth 1
concept 1
quality 1
insightfulness 1
without 1
sufficient 1
investment 1
expertise 1
then 1
produce 1
costs 1
risks 1
exceed 1
an 1
organizations 1
capacity 1
create 1
capture 1
datacurrent 1
usage 1
term 1
tends 1
refer 1
use 1
predictive 1
user 1
behavior 1
certain 1
other 1
advanced 1
methods 1
extract 1
seldom 1
particular 1
set 1
there 1
little 1
doubt 1
quantities 1
now 1
indeed 1
but 1
thats 1
not 1
most 1
relevant 1
characteristic 1
this 1
ecosystem 1
find 1
correlations 1
spot 1
trends 1
prevent 1
diseases 1
combat 1
crime 1
so 1
on 1
executives 1
medical 1
practitioners 1
advertising 1
governments 1
alike 1
regularly 1
meet 1
difficulties 1
datasets 1
areas 1
searches 1
fintech 1
healthcare 1
geographic 1
systems 1
urban 1
encounter 1
limitations 1
escience 1
work 1
meteorology 1
genomics 1
connectomics 1
physics 1
simulations 1
biology 1
environmental 1
researchthe 1
number 1
have 1
grown 1
rapidly 1
collected 1
such 1
mobile 1
cheap 1
numerous 1
informationsensing 1
things 1
aerial 1
remote 1
sensing 1
logs 1
cameras 1
microphones 1
radiofrequency 1
identification 1
rfid 1
readers 1
wireless 1
sensor 1
networks 1
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