CS 61A Fall 2020 Project 2 : cats

该代码实现了一个打字测试系统,包括选择含有特定主题的段落、计算打字准确性、速度(WPM)以及一个自动纠错函数。用户可以根据话题筛选段落,测试后会显示准确率和WPM。此外,还包含了一个编辑距离的计算方法用于自动纠正用户输入的单词。
"""Typing test implementation"""

from utils import lower, split, remove_punctuation, lines_from_file
from ucb import main, interact, trace
from datetime import datetime


###########
# Phase 1 #
###########


def choose(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.
    """
    # BEGIN PROBLEM 1
    "*** YOUR CODE HERE ***"

    if k < len(paragraphs) and k == 0 and select(paragraphs[k]):
        return paragraphs[k]
    elif k >= len(paragraphs):
        return ''

    else:
        if select(paragraphs[0]):
            return choose(paragraphs[1:], select, k-1)
        else:
            return choose(paragraphs[1:], select, k)
    # END PROBLEM 1


def about(topic):
    """Return a select function that returns whether a paragraph contains one
    of the words in TOPIC.

    >>> about_dogs = about(['dog', 'dogs', 'pup', 'puppy'])
    >>> choose(['Cute Dog!', 'That is a cat.', 'Nice pup!'], about_dogs, 0)
    'Cute Dog!'
    >>> choose(['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
    "*** YOUR CODE HERE ***"
    def helper(paragrph):
        words = split(lower(remove_punctuation(paragrph)))
        k = 0
        while k < len(topic):
            if topic[k] in words:
                return True
            k += 1
        return False
    return helper
    # END PROBLEM 2
# about_dogs = about(['dog', 'dogs', 'pup', 'puppy'])
# print(choose(['Cute Dog!', 'That is a cat.', 'Nice pup!'], about_dogs, 1))


def accuracy(typed, reference):
    """Return the accuracy (percentage of words typed correctly) of TYPED
    when compared to the prefix of REFERENCE that was typed.

    >>> 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
    >>> accurac
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值