Question 4 (8 points): Purpose: To practice recursion on a more interesting prob
ID: 3730558 • Letter: Q
Question
Question 4 (8 points): Purpose: To practice recursion on a more interesting problem. Degree of Difficulty: Tricky Pokemon are fantastic creatures that can evolve2 into other, usually stronger, creatures. In their secret laboratory. the evil Team Rocket has been trying to evolve Pokemon in terrifying new ways! Might it now be possible for a meek Magikarp to evolve all the way into the mighty Mewtwo? Your job is to to write a program that uses the power of recursion to find out. For this problem, you will be given a pokemon evolution book as a dictionary. The keys to this dictionary are pokemon names, and the value for each key is a list of pokemon that can be evolved into. The following is a sample of what this dictionary might look like: book "squirtle" ["wartortle"], "wartortle": ["blastoise"], "blastoise": 1, "eevee" "flareon", jolteon", "vaporeon" In the example above, squirtle can (eventually) evolve into a blastoise by first evolving into a wartortle but never into an eevee. On the other hand, eevee can evolve into any one of flareon.jolteon or vaporeon. Note that a pokemon that can no longer evolve might be in the book and associated with an empty list (like blastoise above) or might not be in the book at all. Either case is fine and your program needs to handle both cases correctly. You are guaranteed, however, that the pokemon book will be structured such that a pokemon cannot 'devolve. Thus, if pokemon A can, either directly or indirectly. evolve into pokemon B. then it will not be possible for B to evolve back into A Your task is to write a recursive function that will answer the question of whether a source pokemon can eventually evolve into a given target pokemon. Your function should have 3 parameters: the source, the target and the pokemon dictionary to use to check for possible evolutions. Your function should return True if it is possible for source to become the target and False otherwise. This might sound a little difficult, but with the power of recursion, this is not a complicated problem. Your function should be no longer than 12 lines of code (not counting comments) and possibly less (ours is 8). If you find your solution is getting any longer than that, you are overthinking it! Note: For this question, you ARE allowed to use loops as part of your recursive function! You will likely use a loop to iterate over the lists that are in the evolution book. But your program should still use recursion to do the "real work" (in fact, this problem would be MUCH more difficult to solve without using recursion at all!! 6Explanation / Answer
:::::::::part1 :::::::::
book1 = {
"squirtle" : ["wartortle"],
"wartortle" : ["blastoise"],
"blastoise" : [],
"eevee" : ["flareon","jolteon","vaporeon"]
}
book2 = {
"squirtle" : ["wartortle"],
"wartortle" : ["blastoise"],
"blastoise" : [],
"eevee" : ["flareon","jolteon","vaporeon"]
}
book3 = {
"squirtle" : ["wartortle"],
"wartortle" : ["blastoise"],
"blastoise" : [],
"eevee" : ["flareon","jolteon","vaporeon"]
}
bookDic = {}
bookDic["book1"] = book1
bookDic["book2"] = book2
bookDic["book3"] = book3
def canEvolve(Book , EvolveFrom , EvolveTo) :
if EvolveFrom not in Book :
return False
curList = Book[EvolveFrom]
for pokemon in curList :
if(pokemon == EvolveTo or canEvolve(Book,pokemon,EvolveTo)) :
return True
return False
print("Enter command : ")
while(True) :
command = raw_input()
command = command.split()
inBook = bookDic[command[1]]
EvolveFrom = command[4]
EvolveTo = command[7]
print(canEvolve(inBook , EvolveFrom , EvolveTo))
::::::::::part2 ::::::::
book1 = {
"squirtle" : ["wartortle"],
"wartortle" : ["blastoise"],
"blastoise" : [squirtle],
"eevee" : ["flareon","jolteon","vaporeon"]
}
Above is given example of a book where pokemon can devolve as blastoise is devolving back to squirtle . With above Example if we apply recursion function as in part1 then it will stuck in infinite loop . As squirtle with call recursion for wartortle , wartortle for blastoise and then blastoise again back to squirtle like this it will stuck in infinite loop.
To prevent recursive function to get stuck in infinite loop we need to mantain a lookup array if we reach to some pokemon in recursion then we will mark it in lookup and for this current pokemon we will go to only those pokemon in list which are unmarked.
For Example
Let root pokemon be squirtle , and destination be flareon
Now we will mark squirtle and goto wartortle (form squirtle list)
from wartortle it will go to blastoise . In this call we will mark wartortle .
Now in blastoise list squirtle is there but it is already marked so recursion will terminate and it will not stuck in infinite loop.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.