-
Notifications
You must be signed in to change notification settings - Fork 265
Pokemon Evolution
LeWiz24 edited this page Aug 25, 2025
·
3 revisions
Unit 5 Session 1 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
- What if the starting Pokemon has no evolutionary line?
- The function should still return a list containing only the starter Pokemon.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Traverse through the evolutionary chain of the given Pokemon and compile a list of all forms from the starter to its final evolution.
1) Initialize a list `evolution` with the `starter_pokemon` as its first element.
2) Set a variable `current_pokemon` to the `starter_pokemon`.
3) Use a while loop to follow the evolution chain:
a) While `current_pokemon.evolution` is not None:
i) Append the next evolution to the `evolution` list.
ii) Move to the next Pokemon in the evolutionary line by setting `current_pokemon` to `current_pokemon.evolution`.
4) Return the `evolution` list containing the starter Pokemon and all its evolutions.
- Forgetting to handle cases where a Pokemon does not evolve.
- Failing to update the current_pokemon variable within the loop, which could lead to an infinite loop.
def get_evolutionary_line(starter_pokemon):
line = []
current = starter_pokemon
# Follow the chain of evolutions
while current:
line.append(current.name)
current = current.evolution
return line
# Testing:
charizard = Pokemon("Charizard", ["fire", "flying"])
charmeleon = Pokemon("Charmeleon", ["fire"], charizard)
charmander = Pokemon("Charmander", ["fire"], charmeleon)
charmander_list = get_evolutionary_line(charmander)
print(charmander_list) # ['Charmander', 'Charmeleon', 'Charizard']
charmeleon_list = get_evolutionary_line(charmeleon)
print(charmeleon_list) # ['Charmeleon', 'Charizard']
charizard_list = get_evolutionary_line(charizard)
print(charizard_list) # ['Charizard']