跳转至

Project2-Cats

一个打字游戏

Phase1: Typing

Python
# 1. pick
def pick(paragraphs: list[str], select, k: int) -> str:
    # BEGIN PROBLEM 1
    picked = []
    for i in paragraphs:
        if select(i):
            picked.append(i)
    if len(picked) >= k + 1:
        return picked[k]
    return ''
    # END PROBLEM 1

# 2.about
def about(keywords: list[str]):
    assert all([lower(x) == x for x in keywords]), "keywords should be lowercase."

    # BEGIN PROBLEM 2
    def about_help(string):
        reference_list = split(lower(remove_punctuation(string)))
        for i in keywords:
            for j in reference_list:
                if i == j:
                    return True
        return False
    return about_help
    # END PROBLEM 2

# 3.accuracy
def accuracy(entered: str, source: str) -> float:
    entered_words = split(entered)
    source_words = split(source)
    # BEGIN PROBLEM 3
    len_entered = len(entered_words)
    len_source = len(source_words)

    if len_source == 0:
        if len_entered == 0:
            return 100.0
        else:
            return 0.0

    if len_entered > len_source:
        for _ in range(len_entered - len_source):
            source_words.append('')

    num_correct = 0
    for i in range(len_entered):
        if source_words[i] == entered_words[i]:
            num_correct += 1
    return round(num_correct / len_entered * 100, 2) if len_entered != 0 else 0.0
    # END PROBLEM 3

# 4.wpm
def wpm(entered: str, elapsed: int) -> float:
    assert elapsed > 0, "Elapsed time must be positive"
    # BEGIN PROBLEM 4
    return len(entered) / 5 / elapsed * 60
    # END PROBLEM 4

Phase2: Autocorrect

Python
# 5. autocorrect
def autocorrect(entered_word: str, word_list: list[str], diff_function, limit: int) -> str:
    # BEGIN PROBLEM 5
    if entered_word in word_list:
        return entered_word
    else:
        check_lst = []
        for source_word in word_list:
            if diff_function(entered_word, source_word, limit) <= limit:
                check_lst.append(True)

        if not check_lst:
            return entered_word
        else:
            closest_word = word_list[0]
            for source_word in word_list:
                if diff_function(entered_word, source_word, limit) < diff_function(entered_word, closest_word, limit):
                    closest_word = source_word
            return closest_word 
    # END PROBLEM 5

# 6.furry_fixes

def furry_fixes(entered: str, source: str, limit: int) -> int:
    # BEGIN PROBLEM 6
    if limit < 0:
        return 0
    if not entered or not source:
        return len(entered) + len(source)

    if entered[0] == source[0]:
        return furry_fixes(entered[1:], source[1:], limit)
    else:
        return furry_fixes(entered[1:], source[1:], limit - 1) + 1

    # END PROBLEM 6

# 7.minimum_mewtations
def minimum_mewtations(entered: str, source: str, limit: int) -> int:
        # BEGIN
    if limit < 0:
        return 0
    if entered == source:
        return 0
    if len(entered) == 0:
        return len(source)
    if len(source) == 0:
        return len(entered)

    if entered[0] == source[0]:
        return minimum_mewtations(entered[1:], source[1:], limit)
    else:
        add_diff = 1 + minimum_mewtations(entered, source[1:], limit - 1)
        remove_diff = 1 + minimum_mewtations(entered[1:], source, limit - 1)
        sub_diff = 1 + minimum_mewtations(entered[1:], source[1:], limit - 1)
        return min(add_diff, remove_diff, sub_diff)
        # END