Last active
December 19, 2015 19:58
-
-
Save danieldiekmeier/6010031 to your computer and use it in GitHub Desktop.
stringshuffle()
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
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