Write the accuracy function, which takes a typed paragraph and a reference paragraph. It returns the percentage of words in typed that exactly match the corresponding words in reference. Case and punctuation must match as well. A word in this context is any sequence of characters separated from other words by whitespace, so treat "dog;" as all one word. If a typed word has no corresponding word in the reference because typed is longer than reference, then the extra words in typed are all incorrect. If typed is empty, then the accuracy is zero.
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
>>> accuracy('', 'Cute Dog.')
0.0
"""
typed_words = split(typed)
reference_words = split(reference)
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 >>> accuracy('', 'Cute Dog.') 0.0 """ typed_words = typed.split() reference_words = reference.split() total_count, matched_count = 0, 0 for i in range(len(typed_words)): if i < len(reference_words) and typed_words[i] == reference_words[i]: matched_count += 1 total_count += 1 if total_count == 0: return 0.0 return (matched_count * 100) / total_count # Testing the function here. ignore/remove the code below if not required print(accuracy('Cute Dog!', 'Cute Dog.')) print(accuracy('A Cute Dog!', 'Cute Dog.')) print(accuracy('cute Dog.', 'Cute Dog.')) print(accuracy('Cute Dog. I say!', 'Cute Dog.')) print(accuracy('Cute', 'Cute Dog.')) print(accuracy('', 'Cute Dog.'))
Write the accuracy function, which takes a typed paragraph and a reference paragraph. It returns the...