Skip to content

Instantly share code, notes, and snippets.

@icedraco
Created November 27, 2014 11:44
Show Gist options
  • Select an option

  • Save icedraco/0d0c006ce6aa07ef5b2d to your computer and use it in GitHub Desktop.

Select an option

Save icedraco/0d0c006ce6aa07ef5b2d to your computer and use it in GitHub Desktop.
A word scrambler script that keeps the first and the last letter in each word intact, and shuffles the letters in the middle. A person can often read the scrambled words just fine without realizing it.
from sys import argv
from random import shuffle
def scrambleWord(w):
i = len(w) - 1
# If it's a zero-length string, return it right away.
if i < 0:
return w
else:
# Handle non-alphabetical characters after the word/
while not w[i].isalpha() and i >= 0:
i -= 1
# If the word itself is 3 bytes or less, don't bother.
if i < 3:
return w
# Scramble & return
gutter = list( w[1:i] )
shuffle(gutter)
return w[0] + "".join(gutter) + w[i:]
def scrambleSentance(s):
# Split ever word by space and pass them all through scrambleWord().
return " ".join( map( scrambleWord, s.split(' ') ) )
if len(argv) > 1:
print scrambleSentance(argv[1])
else:
print "Syntax: %s <sentence>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment