Last active
December 27, 2015 10:19
-
-
Save kylestev/7309954 to your computer and use it in GitHub Desktop.
randomize each word in a string except the first and last letters
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 | |
def word_stream(sentence): | |
buff = '' | |
for c in sentence: | |
if 'A' <= c <= 'Z' or 'a' <= c <= 'z': | |
buff += c | |
else: | |
if buff: | |
yield buff | |
buff = '' | |
yield c | |
if buff: | |
yield buff | |
def scramble(word): | |
if word is None or len(word) < 4: | |
return word | |
sub = list(word[1:-1]) | |
random.shuffle(sub) | |
return word[0] + ''.join(sub) + word[-1] | |
def main(): | |
from sys import stdout | |
sentence = raw_input('Enter a sentence: ') | |
for match in word_stream(sentence): | |
stdout.write(scramble(match)) | |
stdout.write('\n') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example input:
i went to the zoo on friday where my friends and i saw many zebras, monkeys, cats and elephants
Example output:
i wnet to the zoo on fiadry werhe my feidnrs and i saw many zearbs, mkoynes, ctas and eelnthpas