Last active
March 7, 2023 20:41
-
-
Save Tedfulk/1570aa5ba6c218a584cace8d325b0513 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
"""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