Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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))

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote