- ์ด ํ๋ก์ ํธ์์๋ ํ์ดํ ์๋๋ฅผ ์ธก์ ํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ ๊ฒ์ด๋ค.
- ์ฌ์ฉ์๊ฐ ๋จ์ด๋ฅผ ์ ๋ ฅํ ํ ๋ง์ถค๋ฒ์ ์์ ํ๋ ๊ธฐ๋ฅ์ธ autocorrect(์๋ ์์ ์ ๋ ฅ)๊ธฐ๋ฅ์ ๊ตฌํํ ์ ์๋ค.
- ์ด ํ๋ก์ ํธ๋ typeracer์์ ์๊ฐ์ ๋ฐ์๋ค.
- Getting Started Video ์ฐธ๊ณ ๊ฐ๋ฅ
Phase 1: Typing
Problem 1: pickํจ์ ๊ตฌํ
- 3๊ฐ์ธ์ ๋ฐ๋๋ค
- paragraphs: strings์ list
- select ํจ์: ์ ํ๋ ์ ์๋ string๋ค์ true๋ฐํํ๋ ํจ์
- k: ์ธ๋ฑ์ค๋ก ์ฐ์ด๋ ์์ด ์๋ ์ ์ k
- ๋ฆฌํด๊ฐ์ select์์ true๋ง์กฑํ๋ ๊ฒ๋ค ๋ชจ์์ ๋ง๋ ๋ฆฌ์คํธ
- k๊ฐ ๋๋ฌด ์ปค์ ๋ง์กฑํ๋ paragraph๊ฐ ์์ผ๋ฉด empty string์ ๋ฆฌํดํ๋ค. => ์๋ก ๋ง๋ ๋ฆฌ์คํธ์์ out of list range๋๋ฉด '' ๋ฆฌํดํ๋ผ๋ ๋ป
def pick(paragraphs, select, k):
"""Return the Kth paragraph from PARAGRAPHS for which SELECT called on the
paragraph returns True. If there are fewer than K such paragraphs, return
the empty string.
Arguments:
paragraphs: a list of strings
select: a function that returns True for paragraphs that can be selected
k: an integer
>>> ps = ['hi', 'how are you', 'fine']
>>> s = lambda p: len(p) <= 4
>>> pick(ps, s, 0)
'hi'
>>> pick(ps, s, 1)
'fine'
>>> pick(ps, s, 2)
''
"""
# BEGIN PROBLEM 1
# paragraphs์ ์์๋ค ํ๋์ฉ ์ ๊ทผํ๋ฉด์ select๋ง์กฑํ๋ฉด new๋ฆฌ์คํธ์์ถ๊ฐ
# ๋ฆฌํด ์กฐ๊ฑด ๋ง์ถฐ์ ๋ฆฌํด
new_paragraphs = []
for i in paragraphs:
if select(i):
new_paragraphs.append(i)
if k < len(new_paragraphs):
return new_paragraphs[k]
else: return ''
# END PROBLEM 1
Problem 2: aboutํจ์ ๊ตฌํ
- topic ๋จ์ด๋ค์ ๋ฆฌ์คํธ๋ฅผ ๋ฐ์
- ๋ฆฌํด๊ฐ์ ํจ์์ธ๋ฐ, ์ด๊ฒ์
- paragraph๋ฅผ ๋ฐ๊ณ ๋ถ๋ฆฌ์ธ์ ๋ฆฌํดํจ
- paragraph๊ฐ topic์์ ์๋ ๋จ์ด๋ฅผ ํ๋๋ผ๋ ํฌํจ ํ๋์ง ์ ํ๋์ง์ ๋ํ ๋ถ๋ฆฌ์ธ
- ์ผ๋จ ์ฐ๋ฆฌ๊ฐ aboutํจ์๋ฅผ ๊ตฌํํ๊ณ ๋๋ฉด ์ฐ๋ฆฌ๋ ๋ฐํ๋ ํจ์๋ฅผ pickํจ์์ select์ธ์์ ์ฌ์ฉํ ์ ์๋ค.
- ๋น๊ต ์ ํํ๊ฒ ํ๋ ค๋ฉด ๋ฌธ๋จ์์ ๋์๋ฌธ์๋ฅผ ๋ฌด์ํ๊ณ , ๋ฌธ์ฅ๋ถํธ๋ฅผ ๋ฌด์ํจ
- ๋๋ฌธ์์ ์๋ฌธ์๊ฐ ๊ทธ๊ฒ ์ด๋ค ๋จ์ด์ธ์ง๋ฅผ ๋ฐ๊พธ์ง ์๋๋ค.
- ์ถ๊ฐ์ ์ผ๋ก, paragraph์ ํ ํฝ ์์ ์๋ ๋จ์ด๋ค๊ณผ์ ์ ํํ ๋งค์นญ๋ง ํ์ธํด๋ผ.
- ํํธ: utils.py์ ์๋ utility functions๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
- ๊ทธ๊ฒ์ docstrings๋ฅผ ๋ณด๋ฉด์ ์ด๋ป๊ฒ ์ฐ๋์ง ์ ์ ์๋ค.
def about(topic):
"""Return a select function that returns whether
a paragraph contains one of the words in TOPIC.
Arguments:
topic: a list of words related to a subject
>>> about_dogs = about(['dog', 'dogs', 'pup', 'puppy'])
>>> pick(['Cute Dog!', 'That is a cat.', 'Nice pup!'], about_dogs, 0)
'Cute Dog!'
>>> pick(['Cute Dog!', 'That is a cat.', 'Nice pup.'], about_dogs, 1)
'Nice pup.'
"""
assert all([lower(x) == x for x in topic]), 'topics should be lowercase.'
# BEGIN PROBLEM 2
def func(paragraph):
paragraph = split(remove_punctuation(lower(paragraph)))
contains = False
for i in topic:
if (i in paragraph):
contains = True
return contains
return func
# END PROBLEM 2
Problem 3: accuracy ํจ์ ๊ตฌํ
- Input: typed paragraph(์ฒซ๋ฒ์งธ ์ธ์)์ reference paragraph(๋๋ฒ์งธ ์ธ์)
- return: ์ ์์์ ํ์์ ๋จ์ด๋ค๊ณผ ์ ํํ ๋งค์นญ๋๋ ์ ๋๋ฅผ ๋ฆฌํดํ๋ค. => ๋จ์ด๋ง๋ค๋ก ๋ด์ผ ํ๋ค!!
- ๋์๋ฌธ์์ ๊ตฌ๋์ (, . ( ) ๋ฑ๋ฑ)๋ ์ญ์ ๋๊ฐ์์ผ ํ๋ค.
- ์ฌ๊ธฐ์ "corresponding"์ ์๋ฏธ๋ typed์ reference ๋ ๋จ์ด๊ฐ ๋ฐ๋์ ๊ฐ์ ์ธ๋ฑ์ค์ ์์ด์ผ ํ๋ค๋ ๋ป์ด๋ค.
- ๋ ๋จ์ด์ ์ฒซ ๊ธ์๋ ๋ฐ๋์ ์๋ก ๋งค์นญ๋์ด์ผ ํ๊ณ , ๋๋ฒ์งธ ๋จ์ด๋ ์ผ์นํด์ผ ํ๋ค.
- ์ฌ๊ธฐ์ word๋ ๊ณต๋ฐฑ์ผ๋ก ๋ค๋ฅธ ๋จ์ด์ ๊ตฌ๋ถ๋ ๋ฌธ์์ ์์์ด๋ค. ex) "dog;" ๋ ํ๋์ ๋จ์ด์ด๋ค.
- ๋ง์ฝ typed๊ฐ reference๋ณด๋ค ๊ธธ๋ค๋ฉด, typed์ ์๋ ์ถ๊ฐ ๋จ์ด๋ค(reference์ ์ผ์นํ์ง ์๋)์ด ๋ชจ๋ ์ฌ๋ฐ๋ฅด์ง ์๋ค.
- ๋ง์ฝ typed์ reference๊ฐ ๋ ๋ค empty๋ผ๋ฉด accuracy๋ 100.0์ด๋ค.
- ๋ง์ฝ typed๊ฐ empty์ธ๋ฐ reference๊ฐ ๋น์ด์์ง ์์ผ๋ฉด accuracy๋ 0์ด๋ค.
def accuracy(typed, reference):
"""Return the accuracy (percentage of words typed correctly) of TYPED
when compared to the prefix of REFERENCE that was typed.
Arguments:
typed: a string that may contain typos
reference: a string without errors
>>> accuracy('Cute Dog!', 'Cute Dog.')
50.0
>>> accuracy('A Cute Dog!', 'Cute Dog.')
0.0
>>> accuracy('cute Dog.', 'Cute Dog.')
50.0
>>> accuracy('Cute Dog. I say!', 'Cute Dog.')
50.0
>>> accuracy('Cute', 'Cute Dog.')
100.0
>>> accuracy('', 'Cute Dog.')
0.0
>>> accuracy('', '')
100.0
"""
typed_words = split(typed)
reference_words = split(reference)
# BEGIN PROBLEM 3
# split๋ผ์ ์ ์ฅ๋ word list์ ์์๋ค์ ๋น๊ต
# ์ ํํ ๋๊ฐ์ ๊ฒ์ ๊ฐ์๋ฅผ ์ธ์ count ๋ณ์๋ฅผ +1ํ๊ธฐ
# if ๋ ๋ฆฌ์คํธ๊ฐ ๋ชจ๋ empty๋ผ๋ฉด return 100.0
# elif ๋ ๋ฆฌ์คํธ ์ค ํ๋๋ผ๋ empty๋ผ๋ฉด return 0.0
# else ๋ ๋ฆฌ์คํธ ์ค ์งง์ ๊ธธ์ด๋งํผ ์ธ๋ฑ์ค๋ก ๋จ์ด์ ์ ๊ทผํด์ ๋๊ฐ์ ๊ฒ์ ๊ฐ์๋ฅผ ์ผ๋ค.
# ์ต์ข
๋ฆฌํด์ typed์ ์ ์ฒดword list์ ๊ฐ์๋ก count๋ณ์๋ฅผ ๋๋ ๊ฐ
count = 0
if typed_words == [] and reference_words == []: return 100.0
elif typed_words == [] or reference_words == []: return 0.0
else:
for i in range(len(typed_words)):
if i >= len(reference_words): break
if typed_words[i] == reference_words[i]: count += 1
return (count / len(typed_words)) * 100
# END PROBLEM 3
Problem 4: wpm๊ตฌํํ๊ธฐ
- wpm: word per minute, ํ์ดํ ์๋ ์ธก์ .
- string์ธ typed์ ์ด๋ก ์ฃผ์ด์ง๋ ๊ฒฝ๊ณผ์๊ฐ elapsed๋ฅผ input์ผ๋ก ๋ฐ๋๋ค.
- ์ด๋ฆ์ด wpm์ด์ง๋ง ์ด๊ฑด ํ์ดํ๋ ๊ธ์์ ๋ฐ๋ผ์ ์ ํด์ง์ง ์๋๋ค. ๋์ ์, 5๊ฐ์ ๋ฌธ์ ๊ทธ๋ฃน์ ๊ธฐ์ค์ผ๋ก ์๊ฐํ๋ค. ๊ทธ๋ฌ๋ฏ๋ก ์ด ํ์ดํ ํ ์คํธ๋ ๋จ์ด์ ๊ธธ์ด์ ๊ธฐ๋ฐ์ ๋๊ณ ์์ง ์๋ค.
- ๋ถ๋น ๋จ์ด์ ๊ณต์์, ์ ๋ ฅ๋ ๋ฌธ์ ์(๊ณต๋ฐฑ ํฌํจ)๋ฅผ 5(์ผ๋ฐ์ ์ธ ๋จ์ด ๊ธธ์ด)๋ก ๋๋ ์๊ฐ(๋ถ)์ด๋ค.
- ์๋ฅผ ๋ค์ด, ๋ฌธ์์ด "I am glad!"๋ 3๋จ์ด์ 10๊ฐ์ ๋ฌธ์(""๋ ํฌํจX)๋ฅผ ํฌํจํ๋ค.
- ๋ถ๋น ๋จ์ด ์๋ ์ ๋ ฅ๋ ๋จ์ด ์๋ฅผ 2๋ก ์ฌ์ฉํ๋ค(10/5=2).
- ์ด ๋ฌธ์์ด์ 30์ด(1๋ถ์ ๋ฐ)๋ง์ ์ ๋ ฅํ ๊ฒฝ์ฐ, ๊ทธ ์๋๋ ๋ถ๋น 4๋จ์ด์ด๋ค.
def wpm(typed, elapsed):
"""Return the words-per-minute (WPM) of the TYPED string.
Arguments:
typed: an entered string
elapsed: an amount of time in seconds
>>> wpm('hello friend hello buddy hello', 15)
24.0
>>> wpm('0123456789',60)
2.0
"""
assert elapsed > 0, 'Elapsed time must be positive'
# BEGIN PROBLEM 4
return (len(typed) / 5) / elapsed * 60
# END PROBLEM 4
'๐โโ๏ธ ํ๋ > ํ๋ก์ ํธ ์์ ์ผ์ง' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Project: Ants vs SomeBees(2) (0) | 2022.07.26 |
---|---|
Project: Ants vs SomeBees(1) (0) | 2022.07.26 |
Phase 3: Strategies of the Game (0) | 2022.07.07 |
Hog(2): Playing the Game (0) | 2022.07.07 |
Project 1: The Game of Hog(1) (0) | 2022.07.07 |