Initial_list¶
Save the following function into a file named initials.py.
Write the function initials() that takes a text
(str), and returns a dictionary (dict) where the key
is a lowercase letter and the value is a list with all the words that
begin with this letter found in the given text. The text only contains
letters, which may be upper or lowercase, and spaces. The dictionary
will not contain any letter that does not appear as an initial of a
word in the given text (incomplete dictionary). Words in the
dictionary lists will be lowercase, in alphabetical order and
without repetitions. Examples:
>>> d = initials('Genius is one percent inspiration and ninety-nine percent perspiration')
>>> if d != {'g': ['genius'], 'i': ['inspiration', 'is'], 'o': ['one'],
... 'p': ['percent', 'perspiration'], 'a': ['and'], 'n': ['ninety-nine']}:
... print(d)
>>> d = initials('Three can keep a secret if two of them are dead')
>>> if d != {'t': ['them', 'three', 'two'], 'c': ['can'], 'k': ['keep'],
... 'a': ['a', 'are'], 's': ['secret'], 'i': ['if'], 'o': ['of'], 'd': ['dead']}:
... print(d)
Note
More tests are provided in file test-initials.txt.
Solution
A solution of this function is provided in file
initials.py.