Words with given shape def words_with_given_shape(words, shape): The shape of the given word of length n is a list of n - 1 integers, each one either -1, 0 or +1 to indicate whether the next letter following the letter in that position comes later (+1), is the same (0) or comes earlier (-1) in the alphabetical order of English letters. For example, the shape of the word 'hello' is [-1, +1, 0, +1], whereas the shape of 'world' is [-1, +1, -1, -1]. Find and return a list of all words that have that particular shape, listed in alphabetical order. Note that your function, same as all the other functions speci0ied in this document that operate on lists of words, should not itself try to read the wordlist 0ile words_sorted.txt, even when Python makes this possible with just a couple of lines of code. The tester script already reads in the entire wordlist and builds the list of words from there. Your function should use this given list of words without even caring which particular 0ile it came from. shape Expected result (using wordlist words_sorted.txt) [1, -1, -1, -1, 0, -1] ['congeed', 'nutseed', 'outfeed', 'strolld'] [1, -1, -1, 0, -1, 1] ['axseeds', 'brogger', 'cheddar', 'coiffes', 'crommel', 'djibbah', 'droller', 'fligger', 'frigger', 'frogger', 'griffes', 'grogger', 'grommet', 'prigger', 'proffer', 'progger', 'proller', 'quokkas', 'stiffen', 'stiffer', 'stollen', 'swigger', 'swollen', 'twiggen', 'twigger'] [0, 1, -1, 1] ['aargh', 'eeler', 'eemis', 'eeten', 'oopak', 'oozes', 'sstor'] [1, 1, 1, 1, 1, 1, 1] ['aegilops'] Motivated students can take on as a recreational challenge to 0ind the shape of length n - 1 that matches the largest number of words, for the possible values of n from 3 to 20. Alternatively, try to count how many possible shapes of length n - 1 do not match any words of length n at all. What is the shortest possible shape that does not match any words?
Words with given shape
def words_with_given_shape(words, shape):The shape of the given word of length n is a list of n - 1 integers, each one either -1, 0 or +1 to indicate whether the next letter following the letter in that position comes later (+1), is the same (0) or comes earlier (-1) in the alphabetical order of English letters. For example, the shape of the word 'hello' is [-1, +1, 0, +1], whereas the shape of 'world' is [-1, +1, -1, -1]. Find and return a list of all words that have that particular shape, listed in alphabetical order.
Note that your function, same as all the other functions speci0ied in this document that operate on lists of words, should not itself try to read the wordlist 0ile words_sorted.txt, even when Python makes this possible with just a couple of lines of code. The tester script already reads in the entire wordlist and builds the list of words from there. Your function should use this given list of words without even caring which particular 0ile it came from.
shape |
Expected result (using wordlist words_sorted.txt) |
[1, -1, -1, -1, 0, -1]
|
['congeed', 'nutseed', 'outfeed', 'strolld']
|
[1, -1, -1, 0, -1, 1]
|
['axseeds', 'brogger', 'cheddar', 'coiffes', 'crommel', 'djibbah', 'droller', 'fligger', 'frigger', 'frogger', 'griffes', 'grogger', 'grommet', 'prigger', 'proffer', 'progger', 'proller', 'quokkas', 'stiffen', 'stiffer', 'stollen', 'swigger', 'swollen', 'twiggen', 'twigger']
|
[0, 1, -1, 1] |
['aargh', 'eeler', 'eemis', 'eeten', 'oopak', 'oozes', 'sstor']
|
[1, 1, 1, 1, 1, 1, 1]
|
['aegilops']
|
Motivated students can take on as a recreational challenge to 0ind the shape of length n - 1 that matches the largest number of words, for the possible values of n from 3 to 20. Alternatively, try to count how many possible shapes of length n - 1 do not match any words of length n at all. What is the shortest possible shape that does not match any words?
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images