builds the list of words from there
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 specified in this document that operate on lists of words, should not itself try to read the wordlist file 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 file it came from.
Motivated students can take on as a recreational challenge to find 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 3 images