-
Notifications
You must be signed in to change notification settings - Fork 243
Souvenir Declutter
Andrew Burke edited this page Aug 29, 2024
·
3 revisions
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Q
- What is the desired outcome?
- To filter out souvenirs whose occurrences are greater than or equal to the threshold.
- What input is provided?
- A list of strings
souvenirs
and an integerthreshold
.
- A list of strings
- What is the desired outcome?
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Count the frequency of each souvenir and filter those below the threshold.
1) Create a dictionary `souvenir_count` to count the frequency of each souvenir.
2) Iterate over the `souvenirs` list to populate the dictionary.
3) Use a list comprehension to filter and return only those souvenirs with a count less than the threshold.
- Counting souvenirs incorrectly by not checking their presence in the dictionary first.
def declutter(souvenirs, threshold):
# Count the frequency of each souvenir
souvenir_count = {}
for souvenir in souvenirs:
if souvenir in souvenir_count:
souvenir_count[souvenir] += 1
else:
souvenir_count[souvenir] = 1
# Filter souvenirs based on the threshold
decluttered_souvenirs = []
for souvenir in souvenirs:
if souvenir_count[souvenir] < threshold:
decluttered_souvenirs.append[souvenir]
return decluttered_souvenirs