I'm creating a little program that requires me to do the following:
Starting from a word
I need to make all possible words according to the following rule: starting with the word 'help'
, 'help'
me become root of the tree, then every time I raise a point of the first type ('h'
) and then the word becomes me 'elp'
, then I take always the word 'help'
but at this point I raise the second letter ('e'
) and then the word becomes me 'hlp'
, then I take always the initial word 'help'
and lift up the third letter ('l'
), and then the word becomes me 'hep'
, then I take always the initial word 'help'
, and I raise the fourth letter ('p'
), and then the word becomes me 'hel'
.
Then later, in the words found ('elp'
, 'hlp'
, 'hep'
, 'hel'
) I have to repeat the same thing until you get to the leaves. All of these words should be included in a list(also in the tree, obviously).
Obviously there is a recursion, but my problem is this ... in the recursion are not any good! :(
Thank you if you can help me, it's really important.
PS: Or rather, as I understand it, I have to create all possible combinations without changing the order of letters
You need to code recursive function. This function should create new words and call itself with each new word as an argument. I would recommend you to read some documentation about strings in Python.
The sample function:
Recursion is here:
result[newWord] = getWords(newWord)
. We call our function for every new word.You may improve it with some if/else statements. Now it returns dictionary even for one char word.