Guest User

Untitled

a guest
Oct 18th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. import random
  2.  
  3. def jumble_word(word):
  4. # indexes we're going to generate
  5. index = []
  6.  
  7. # until we have the same number of indexes as our word
  8. while len(index) < len(word):
  9. # our random index
  10. rand = random.randint(0, len(word) - 1)
  11. # whether the index is equal
  12. equal = True
  13. while equal:
  14. # check whether it's equal to all other generated indexes
  15. # we want it to be unique
  16. innerEqual = False
  17. for i in index:
  18. if i == rand:
  19. innerEqual = True
  20. break
  21. # if it's not equal to any, we can exit this loop and
  22. # append it to the index list
  23. if not innerEqual:
  24. equal = False
  25. else:
  26. rand = random.randint(0, len(word) - 1)
  27. index.append(rand)
  28.  
  29. # generate the jumbled word
  30. # by putting together the characters at the given indexes
  31. newWord = ""
  32. for i in index:
  33. newWord += word[i]
  34. return newWord
  35.  
  36. # test word
  37. print(jumble_word("abcdef"))
Add Comment
Please, Sign In to add comment