-
Notifications
You must be signed in to change notification settings - Fork 244
Identifying Endangered Species
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 count how many of the observed species are also considered endangered.
- What input is provided?
- Two strings,
endangered_species
andobserved_species
.
- Two strings,
- What is the desired outcome?
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Use a set for fast lookup of endangered species and iterate through the observed species to count how many are endangered.
1) Convert `endangered_species` to a set for fast lookup.
2) Initialize a counter `count` to 0.
3) Iterate through `observed_species`:
- If the species is in the endangered set, increment the counter.
4) Return the counter.
- Forgetting that species are case-sensitive.
def count_endangered_species(endangered_species, observed_species):
# Create a set of endangered species for fast lookup
endangered_set = set(endangered_species)
# Count the number of endangered species in the observed species
count = 0
for species in observed_species:
if species in endangered_set:
count += 1
return count