This is the Python output to achieve:
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
This is the Python output to achieve:
Write a function named words Starting With that accepts the string sentence. The function returns a dictionary whose keys are the unique first letter of each word in the string and whose associated value is a list of words starting with that letter. For example, calling the function as below: words Starting With("this is the correct Terminal") returns: {"t":["this", "the", "terminal"], "i": ["is"], "c": ["correct"]} Do the following in the function: 1. Split the sentence into words. Note, the string has embedded new-line (\n) characters. You need to replace the new-line character with a space. (search for the replace() function in strings). 2. Initialize an empty dictionary. 3. Iterate over the words: A. Convert the first letter to lower case. B. If the first character of the current word is not in the dictionary: Initialize the dictionary entry for the current character with a list containing the current word. C. Otherwise: Append the current word to the value for the current character key. 4. Return the dictionary 1 # Write the function below 2 4 # Do not change the code below 5 sentence = """A satire, however hard it tries 6 Will always be delightful 7 Are you upset by how delicious it is 8 Does it tear you apart to see the satire so pleasing""" 9 print (words StartingWith (sentence))
{'a': ['A', 'always', 'Are', 'apart'], 's': ['satire,', 'see', 'satire', 'so'], 'h': ['however', 'hard', 'how'], 'i': ['it', 'it', 'is', 'it'], 't': ['tries', 'tear', 'to', 'the'], 'w': ['Will'], 'b': ['be', 'by'], 'd': ['delightful', 'delicious', 'Does'], 'y': ['you', 'you'], 'u': ['upset'], 'p': ['pleasing']}