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 (unique) 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!

Login and get coding
go back Intermediate level
Bitecoin 3X

Will you be Pythonista #229 to crack this Bite?
Resolution time: ~67 min. (avg. submissions of 5-240 min.)
Pythonistas rate this Bite 5.64 on a 1-10 difficulty scale.
» Up for a challenge? 💪

Focus on this Bite hiding sidebars, turn on Focus Mode.

Ask for Help