Skip to content

Instantly share code, notes, and snippets.

@Tedfulk
Last active March 7, 2023 20:41
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 Tedfulk/1570aa5ba6c218a584cace8d325b0513 to your computer and use it in GitHub Desktop.
Save Tedfulk/1570aa5ba6c218a584cace8d325b0513 to your computer and use it in GitHub Desktop.
"""If you mix up the order of letters in a word, many people can slitl raed and urenadnstd tehm. Write a function that takes an input sentence, and mixes up the insides of words (anything longer than 3 letters)."""
def scramble(sentence: str):
import random
import string
new_sentence = sentence.translate(str.maketrans('', '', string.punctuation))
words = new_sentence.split()
scrambled = []
for word in words:
if len(word) > 3:
middle = list(word[1:])
random.shuffle(middle)
scrambled.append(word[0] + "".join(middle))
else:
scrambled.append(word)
return " ".join(scrambled) + "."
scramble("A quick brown fox's jumped over the lazy dog.")
# 'A qkciu bnrwo fsxo jepmud oevr the lzya dog.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment