In Python Your team is scrambling to decipher a recent message, worried it\'s a
ID: 3704130 • Letter: I
Question
In Python
Your team is scrambling to decipher a recent message, worried it's a plot to break into a major European National Cake Vault. The message has been mostly deciphered, but all the words are backwards! Your colleagues have handed off the last step to you.
Write a function reverse_words() that takes a message as a list of characters and reverses the order of the words in-place
Why a list of characters instead of a string? The goal of this question is to practice manipulating strings in place. Since we're modifying the message, we need a mutable. type like a list, instead of Python's immutable strings For example: Pythonv messageC a, k' 'e,'', reverse words (message # Prints: . steal pound cake' print "" join(message) When writing your function, assume the message contains only letters and spaces and all words are separated by one space.Explanation / Answer
def reverse_words(message): words = ''.join(message) word = words.split(" ") word = word[-1::-1] return ' '.join(word) if __name__ == '__main__': message = ['c', 'a', 'k', 'e', ' ', 'p', 'o', 'u', 'n', 'd', ' ', 's', 't', 'e', 'a', 'l'] message = reverse_words(message) print(''.join(message))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.