Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. # Function to generate all strings for the word wrangler game
  2.  
  3. def gen_all_strings(word):
  4. """
  5. Generate all strings that can be composed from the letters in word
  6. in any order.
  7.  
  8. Returns a list of all strings that can be formed from the letters
  9. in word.
  10.  
  11. This function should be recursive.
  12. """
  13. if word == "":
  14. return [""]
  15. else:
  16. first = word[0]
  17. rest_word = word[1:]
  18. rest_strings = gen_all_strings(rest_word)
  19. all_strings = []
  20.  
  21. for string in rest_strings:
  22. for letter_pos in range(len(string) +1):
  23. all_strings.append(string[0:letter_pos] + \
  24. first + string[letter_pos:])
  25.  
  26. return all_strings + rest_strings
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement