Python code! Write a function lookupVal2 that takes a list of tuples (tL) and a key k as input. Each tuple in the input list
Python code!
Write a function lookupVal2 that takes a list of tuples (tL) and a key k as input. Each tuple in the input list includes an integer index value and a dictionary. The index in each tuple represent a link to another tuple in the list (e.g. index 3 refers to the 4th tuple, i.e., the tuple at index 3 in the list) lookupVal2 checks the dictionary in each tuple in tL starting from the end of the list and following the indexes specified in the tuples. For example, assume the following:
[(0,d0),(0,d1),(0,d2),(1,d3),(2,d4),(3,d5),(5,d6)]
0 1 2 3 4 5 6
The lookupVal2 function will check the dictionaries d6,d5,d3,d1,d0 in order (it will skip over d4 and d2) The tuple in the beginning of the list will always have index 0. It will return the first value found for key k. If k is couldnt be found in any dictionary, then it will return None. For example:
L2 = [(0,{"x":0,"y":True,"z":"zero"}), (0,{"x":1}), (1,{"y":False}), (1,{"x":3, "z":"three"}), (2,{})]
lookupVal2(L2,"x") returns 1
lookupVal2(L2,"y") returns False
lookupVal2(L2,"z") returns "zero"
lookupVal2(L2,"t") returns None (Note: I suggest you to provide a recursive solution to this problem. Hint: Define a helper function with an additional parameter that hold the list index which will be searched in the next recursive call.)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started