Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- def jumble_word(word):
- # indexes we're going to generate
- index = []
- # until we have the same number of indexes as our word
- while len(index) < len(word):
- # our random index
- rand = random.randint(0, len(word) - 1)
- # whether the index is equal
- equal = True
- while equal:
- # check whether it's equal to all other generated indexes
- # we want it to be unique
- innerEqual = False
- for i in index:
- if i == rand:
- innerEqual = True
- break
- # if it's not equal to any, we can exit this loop and
- # append it to the index list
- if not innerEqual:
- equal = False
- else:
- rand = random.randint(0, len(word) - 1)
- index.append(rand)
- # generate the jumbled word
- # by putting together the characters at the given indexes
- newWord = ""
- for i in index:
- newWord += word[i]
- return newWord
- # test word
- print(jumble_word("abcdef"))
Add Comment
Please, Sign In to add comment