"""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
CS 61A Fall 2020 Project 2 : cats
最新推荐文章于 2025-12-24 11:57:52 发布
该代码实现了一个打字测试系统,包括选择含有特定主题的段落、计算打字准确性、速度(WPM)以及一个自动纠错函数。用户可以根据话题筛选段落,测试后会显示准确率和WPM。此外,还包含了一个编辑距离的计算方法用于自动纠正用户输入的单词。

最低0.47元/天 解锁文章
894

被折叠的 条评论
为什么被折叠?



