GH avatar Bite 156. Make an index of story characters

You know the index at the end of a book where you look up which pages people and terms occur? Useful right?

In this Bite you will build your own, but instead of page numbers we will use line numbers.

We will be using the story of Little Red Riding Hood (the tests also run your code against The Needle Tree).

Complete make_character_index (scroll down) that takes a multiline text (story) string and a list of characters.

Parse the text and match the characters (case insensitively) keeping track of the line numbers where they appeared.

One twist: some characters have synonyms, in that case you have to match them all and return the first one, e.g. when one of ('Grandmother', 'Grandma', 'Granny') matches, you return the first = Grandmother.

The return data is a dict where the keys are the characters and the values are lists of line numbers of appearances in sorted order.

See also the tests for the expected data.

We hope this is a fun exercise that keeps your Python fresh. Now go crack this Bite and remember: keep calm and code in Python!

Will you be Pythonista #20 to crack this Bite?
It takes an average of ~85 minutes to solve this Bite (submissions 5-240 min).
Pythonistas rate this Bite 5.0 on a 1-10 difficulty scale.
» Up for a challenge? 💪

Login and get coding
We use Python 3.7