Skip to content

Instantly share code, notes, and snippets.

@danieldiekmeier
Last active December 19, 2015 19:58
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 danieldiekmeier/6010031 to your computer and use it in GitHub Desktop.
Save danieldiekmeier/6010031 to your computer and use it in GitHub Desktop.
stringshuffle()
from random import shuffle
from re import match
def stringshuffle(s):
'''
Shuffles a string but keeps the first and last letter of every word at their original position.
'''
splitted = s.split()
shuffled = []
for str in splitted:
distance = -2 if match("[\.\!\?\,\;\:\(\)]", str[-1]) else -1
middle_list = list(str[1:distance])
shuffle(middle_list)
shuffled.append(str[0] + ''.join(middle_list) + str[distance:])
return ' '.join(shuffled)
print stringshuffle('Hier hat der Zufall seine Hand im Spiel')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment