Homework 9:
Dictionaries
CS-UY 1114 NYU Tandon
Submit your solution to all problems in a single Python file named YourNetID_hw9.py.
Problem 1
Consider the following dict, which provides a mapping of commonly misspelled words to
their correct spelling. Type the dictionary, exactly as given, into your file.
respellings = {
"teh": "the",
"relevent": "relevant",
"lite": "light",
"lol": "haha",
}
Write a function respell, which takes a str argument and returns a string with the
spelling corrected according to the content of the dict. Your solution must reference the
respellings dictionary as its source of information. It can be used like this:
>>> respell("I ate teh whole thing lol")
'I ate the whole thing haha'
Hint: use the split function to convert the input string into a list of words. Then use
you can use the join function to convert a (corrected) list of words back into a string.
Hint: iterate over the list of words, replacing the current word if necessary and appending
it to an accumulator.
Hint: use the in operator to determine if a particular word is a key in the dict. Use
respellings[word] to get the value associated with word from the dict. If the word
isn’t in respellings, then the word is added to the output unchanged.
Problem 2
The position of a word in a string is the index of that word in list of words comprising
the string. For example:
It
was
φ
the
best
of
Κ
times
Θ
it
was
the
worst
ν
of
φ
times
φφ
Write a function called “word_positions” with signature str -> dict (str, list (int)).
It should return a dictionary whose keys are the words that occur in the argument string,
and where the value associated to a given key is the list of positions at which that word
occurs.
For example:
1
>>> word_positions ('It was the best of times it was the worst of times ')
{'It': [0], 'was ': [1, 7], 'the ': [2, 8], 'best ': [3], 'of': [4, 10],
'times ': [5, 11], 'it': [6], 'worst ': [9]}
Hint: for the purposes of this problem you may assume that the string does not include
any punctuation. Also, you do not need to do any case normalization, so in this problem
'It' and 'it' are considered different words. To obtain the list of words in a string, call
the split function:
>>> 'welcome to the future '.split()
['welcome ', 'to', 'the', 'future ']
>>> "now's the time".split ()
["now's", 'the', 'time ']
Problem 3
Write a function called “commonest” that takes a dictionary like the one produced by
word_positions as an argument and returns a key whose value list is the longest:
>>> word_positions("He thought a thought he'd never thought before")
{'He':[0], 'thought ':[1, 3, 6], 'a':[2], "he'd":[4], 'never ':[5], 'before ':[7]}
>>> commonest(word_positions("He thought a thought he'd never thought before"))
'thought '
If the dictionary is empty, then your function should return the empty string.
If there is a tie for the word with the longest list of positions, then your function
may return any one of the most common words.
Hint: iterate over the dictionary and compare the length of the list of positions for each
word to the length of the longest list seen thus far. Each time you find a new longest list,
you will need to update both the commonest word its number of occurrences.
版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。