Unit 2 Session 1 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
Q: What is the problem asking for?
Q: What are the inputs?
crew
and position
, both of length n
.Q: What are the outputs?
crew
) is mapped to their corresponding position (from position
).Q: Are there any constraints on the lists?
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`.
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