-
Notifications
You must be signed in to change notification settings - Fork 265
Wildlife Reintroduction
LeWiz24 edited this page Aug 13, 2024
·
2 revisions
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Q
- What is the desired outcome?
- To determine the maximum number of copies of
target_species
that can be formed fromraised_species
.
- To determine the maximum number of copies of
- What input is provided?
- Two strings,
raised_species
andtarget_species
.
- Two strings,
- What is the desired outcome?
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Count the frequency of each species in both strings, then determine the maximum number of copies of target_species
that can be formed.
1) Use `Counter` to count the frequency of each species in `raised_species` and `target_species`.
2) Initialize `max_copies` to infinity.
3) For each species in `target_species`, calculate how many times it can be formed from `raised_species`.
4) Return the minimum value from the above calculations.
- Not handling cases where a species in
target_species
is missing fromraised_species
.
from collections import Counter
def max_species_copies(raised_species, target_species):
# Count the occurrences of each species in raised_species and target_species
raised_count = Counter(raised_species)
target_count = Counter(target_species)
# Initialize the maximum number of copies to a large value
max_copies = float('inf')
# Determine the maximum number of copies that can be formed
for species in target_count:
if species in raised_count:
max_copies = min(max_copies, raised_count[species] // target_count[species])
else:
return 0
return max_copies