Created
January 3, 2013 01:00
-
-
Save hanigamal/4439927 to your computer and use it in GitHub Desktop.
Shuffle Chars
This file contains hidden or 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 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