Question

Write the accuracy function, which takes a typed paragraph and a reference paragraph. It returns the...

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)

0 0
Add a comment Improve this question Transcribed image text
Answer #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
    >>> 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.'))
Add a comment
Know the answer?
Add Answer to:
Write the accuracy function, which takes a typed paragraph and a reference paragraph. It returns the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT