Exercise: Let's Make Sure You're Comfortable With zip() and enumerate()

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]
  1. Use zip() to pair each word with its correct index.
  2. Sort the pairs by their indices.
  3. Extract and combine the words into a single sentence, properly ordered.
  4. 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.

Exercise 2

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member? Sign-In

You must own this product to join the conversation.