-
Notifications
You must be signed in to change notification settings - Fork 242
Wildlife Reintroduction
kyra-ptn edited this page Aug 29, 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) 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
.
def max_species_copies(raised_species, target_species):
# Step 1: Create frequency dictionaries for raised_species and target_species
raised_count = {}
target_count = {}
# Count the occurrences in raised_species
for species in raised_species:
if species in raised_count:
raised_count[species] += 1
else:
raised_count[species] = 1
# Count the occurrences in target_species
for species in target_species:
if species in target_count:
target_count[species] += 1
else:
target_count[species] = 1
# Step 2: Initialize the maximum number of copies to a large value
max_copies = float('inf')
# Step 3: Determine the maximum number of copies that can be formed
for species in target_count:
if species in raised_count:
# Calculate how many times this species in target can be formed
max_copies = min(max_copies, raised_count[species] // target_count[species])
else:
# If a species in target is not in raised_species, return 0
return 0
return max_copies