Breathing Room - 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 return a list of room names where the oxygen levels are outside the specified acceptable range.
-
Q: What are the inputs?
- A: A dictionary
oxygen_levels
mapping room names to current oxygen levels, and two integersmin_val
andmax_val
specifying the acceptable range of oxygen levels.
- A: A dictionary
-
Q: What are the outputs?
- A: A list of room names where the oxygen levels are outside the range defined by
min_val
andmax_val
.
- A: A list of room names where the oxygen levels are outside the range defined by
-
Q: Are there any constraints on the values in the dictionary or the range?
- A: The oxygen levels are numerical values, and
min_val
andmax_val
define the inclusive range.
- A: The oxygen levels are numerical values, and
P-lan
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Iterate through the dictionary and check if the oxygen levels in each room fall outside the specified range.
1. Initialize an empty list `out_of_range_rooms`.
2. Iterate through the items in the `oxygen_levels` dictionary.
- For each room and its oxygen level:
- Check if the level is less than `min_val` or greater than `max_val`.
- If true, append the room name to `out_of_range_rooms`.
3. Return the list `out_of_range_rooms`.
I-mplement
def check_oxygen_levels(oxygen_levels, min_val, max_val):
out_of_range_rooms = []
for room, level in oxygen_levels.items():
if level < min_val or level > max_val:
out_of_range_rooms.append(room)
return out_of_range_rooms