Exercise 1
You’re tasked with decoding a secret message! You’re given two pieces of information: a list of scrambled words and a list of indices that reveal their correct order.
Python
scrambled_words = ["mystery", "is", "This", "a", "hidden"]
correct_order = [4, 1, 0, 2, 3]
- Use
zip()
to pair each word with its correct index. - Sort the pairs by their indices.
- Extract and combine the words into a single sentence, properly ordered.
- Write a function
decode_message(words, order)
that takes any scrambled list of words and a list of indices to decode the message. Ensure it handles edge cases like missing or extra indices. Here are the rules:- If there are more indices than words, ignore the extra indices.
- If there are more words than indices, assume the extra words are already in the correct order.