Created
October 18, 2018 17:17
-
-
Save facekapow/c8953c6a66a85540bd0a86cfef3100d3 to your computer and use it in GitHub Desktop.
word-jumbler created by facekapow - https://repl.it/@facekapow/word-jumbler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment