๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿƒ‍โ™€๏ธ ํ™œ๋™/ํ”„๋กœ์ ํŠธ ์ž‘์—…์ผ์ง€

Project 2: CS 61A Autocorrected Typing Software(1)

  • ์ด ํ”„๋กœ์ ํŠธ์—์„œ๋Š” ํƒ€์ดํ•‘ ์†๋„๋ฅผ ์ธก์ •ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•  ๊ฒƒ์ด๋‹ค.
  • ์‚ฌ์šฉ์ž๊ฐ€ ๋‹จ์–ด๋ฅผ ์ž…๋ ฅํ•œ ํ›„ ๋งž์ถค๋ฒ•์„ ์ˆ˜์ •ํ•˜๋Š” ๊ธฐ๋Šฅ์ธ 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