Skip to content

Instantly share code, notes, and snippets.

@hanigamal
Created January 3, 2013 01:00
Show Gist options
  • Save hanigamal/4439927 to your computer and use it in GitHub Desktop.
Save hanigamal/4439927 to your computer and use it in GitHub Desktop.
Shuffle Chars
import re, random
def shuffle_one(word):
if len(word) <= 3:
return word
middle = list(word[1:-1])
random.shuffle(middle)
return word[0] + ''.join(middle) + word[-1]
def repl(match):
return shuffle_one(match.groups()[0])
def shuffle(text):
return re.sub("(\w+)", repl, text)
shuffle("Normally, reading this shouldn't be too hard.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment