Space Crew - codepath/compsci_guides GitHub Wiki

Unit 2 Session 1 (Click for link to problem statements)

U-nderstand

Understand what the interviewer is asking for by using test cases and questions about the problem.

  • Q: What is the problem asking for?

    • A: The problem asks to map each crew member to their position on board the International Space Station using two lists of equal length.
  • Q: What are the inputs?

    • A: Two lists, crew and position, both of length n.
  • Q: What are the outputs?

    • A: A dictionary where each crew member (from crew) is mapped to their corresponding position (from position).
  • Q: Are there any constraints on the lists?

    • A: The lists are of equal length and the indices of the lists correspond to each other.

P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Use a dictionary to map each crew member to their position by iterating over the indices of the lists.

1. Initialize an empty dictionary `crew_position_map`.
2. Iterate through the indices of the lists using a `for` loop.
   - For each index `i`, map `crew[i]` to `position[i]` in the dictionary.
3. Return the dictionary `crew_position_map`.

I-mplement

def space_crew(crew, position):
    # Step 1: Initialize an empty dictionary
    crew_position_map = {}
    
    # Step 2: Iterate through the lists using indices and add to the dictionary
    for i in range(len(crew)):
        crew_position_map[crew[i]] = position[i]
    
    return crew_position_map