Skip to content

Instantly share code, notes, and snippets.

@jgomo3
Created November 9, 2015 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgomo3/332c9b92daf67d9aa421 to your computer and use it in GitHub Desktop.
Save jgomo3/332c9b92daf67d9aa421 to your computer and use it in GitHub Desktop.
Typoglycemia - Dailyprogrammers [2015-11-09] Challenge #240 [Easy]
import random
import re
import sys
def get_text(file):
return file.read()
def scramble(word):
return \
('' if len(word) == 0 else word[0]) + \
('' if len(word) <= 2 else ''.join(random.sample(word[1:-1], len(word) - 2))) + \
('' if len(word) <= 1 else word[-1])
def scramble_words(text):
ptrn = re.compile(r"(\w+'?\w*)|(\W+)")
for chunk in ptrn.findall(text):
if chunk[1] == '': # It's a word
yield scramble(chunk[0])
else:
yield chunk[1]
def main():
text = get_text(sys.stdin)
for chunk in scramble_words(text):
sys.stdout.write(chunk)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment