-
Notifications
You must be signed in to change notification settings - Fork 244
Filter Destinations
LeWiz24 edited this page Aug 13, 2024
·
1 revision
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Q
- What is the desired outcome?
- To remove destinations from the dictionary that have a rating below the given threshold.
- What input is provided?
- A dictionary
destinations
mapping destination names to their rating scores, and an integerrating_threshold
.
- A dictionary
- What is the desired outcome?
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Iterate through the dictionary, identify the destinations with ratings below the threshold, and remove them.
1) Initialize a list `keys_to_remove` to store the keys of destinations to be removed.
2) Iterate through the dictionary:
- If a destination's rating is below the threshold, add its key to `keys_to_remove`.
3) Remove the keys stored in `keys_to_remove` from the dictionary.
4) Return the updated dictionary.
- Modifying the dictionary while iterating over it.
def remove_low_rated_destinations(destinations, rating_threshold):
keys_to_remove = []
for dest, rating in destinations.items():
if rating < rating_threshold:
keys_to_remove.append(dest)
for key in keys_to_remove:
del destinations[key]
return destinations