Created
November 9, 2015 15:42
-
-
Save jgomo3/332c9b92daf67d9aa421 to your computer and use it in GitHub Desktop.
Typoglycemia - Dailyprogrammers [2015-11-09] Challenge #240 [Easy]
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 | |
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