Skip to content

Instantly share code, notes, and snippets.

@kylestev
Last active December 27, 2015 10:19
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 kylestev/7309954 to your computer and use it in GitHub Desktop.
Save kylestev/7309954 to your computer and use it in GitHub Desktop.
randomize each word in a string except the first and last letters
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()
@kylestev
Copy link
Author

kylestev commented Nov 4, 2013

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment