"""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 ############defchoose(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 ***"
eligible_paragraphs =[x for x in paragraphs if select(x)]iflen(eligible_paragraphs)> k:return eligible_paragraphs[k]else:return''# END PROBLEM 1defabout(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.'
"""assertall([lower(x)== x for x in topic]),'topics should be lowercase.'# BEGIN PROBLEM 2"*** YOUR CODE HERE ***"defhelper(a_sentense):
separate_words = split(lower(remove_punctuation(a_sentense)))
k =0while k <len(topic):for x in separate_words:if x == topic[k]:returnTrue
k +=1returnFalsereturn helper
# END PROBLEM 2defaccuracy(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
>>> accuracy('', 'Cute Dog.')
0.0
"""
typed_words = split(typed)
reference_words = split(reference)# BEGIN PROBLEM 3"*** YOUR CODE HERE ***"
i =0
accuracte_words =0.0iflen(typed_words)==0:return accuracte_words
iflen(typed_words)>=len(reference_words):while i <len(reference_words):if typed_words[i]== reference_words[i]:
accuracte_words +=1
i +=1
accuracy = accuracte_words/len(typed_words)else:#if len(typed_words) < len(reference_words):while i <len(typed_words):if typed_words[i]== reference_words[i]: