Marking Typed Answers Fairly

Working with TextMedium+20 XP+20

Marking Typed Answers Fairly

A quiz app marks answers that players type in. Comparing them exactly is too strict: "Paris." and "paris" mean the same thing, and so do "Blue Whale" and "blue whale". So before comparing, both the player's answer and the correct answer are normalised.

A normalised string:

  1. is entirely lowercase;
  2. has every hyphen - turned into a space, so "well-known" and "well known" match;
  3. contains none of these eight characters, which are simply deleted: . , ! ? ; : ' "
  4. has no whitespace at the start or the end, and exactly one space between words — whatever mix of spaces, tabs or line breaks was there before.

Task: write two functions.

  • normalise(text) returns the normalised version of text.
  • exact_match_rate(predictions, answers) takes two lists of the same length — the player's answers and the correct answers, in matching order — and returns the fraction of positions where the two normalised strings are identical, rounded with round(rate, 4). If the lists are empty, return 0.0.

"4" and "four" still don't match: normalising tidies up the typing, it doesn't understand meaning.

Examples

text = " The Eiffel-Tower! "
returns 'the eiffel tower'
predictions = ["Paris", "4", "blue whale"]
answers = ["paris.", "four", "Blue Whale"]
returns 0.6667

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run