Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Question 1: Recursive Dictionaries Using Tuples (4 points) A dictionary is a set of keys associated with values. We can construct data similarly using tuples.
Question 1: Recursive Dictionaries Using Tuples (4 points) A dictionary is a set of keys associated with values. We can construct data similarly using tuples. The dictionary, D = {1 : "a", 2 : "5", 3: "c"} can be expressed as T = ((1, "a"),(2, "b"),(3,"c")) using nested tuples. Let's implement the most basic operations of Dictionaries using this nested tuple structure... recursively! Write a python function, recursiveLookup() that, taking a tuple of tuples of the form above and search_key, the key to be searched for, returns the second element of the first tuple matching the search key. If the key is not found, return false. In order to turn this into a recursive algorithm, we'll use something called "tail recursion". Similar to a greedy algorithm, tail recursion operates on the first element of a data structure (like a list or tuple), and recurses on the rest of the data structure. The first element is called the "head", and the rest is called the "tail", just like a snake! Hence, "tail recursion". In [ ]: def recursiveLookup(data, search_key): return # YOUR CODE HERE raise Not ImplementedError() In [ ]: In [ ]: data = [ ("potato",4) , ("carrots", 3) ("onions",2) , ("cans o' clams",3) ,("cream (cups)",1) , ("milk (cups)", 2) , ("flour (tbsp)",2) , ("Salt and pepper", "To Taste") , ("Mr Moore's Dinner","Delicious Clam Chowder")] print("Test 1 :", (recursiveLookup (data, "onions") == 2) ) print("Test 2 : ', (recursiveLookup (data, "Salt and pepper") == "To Taste") ) print("Test 3 : ', (not recursiveLookup (data, "old Boot Leather") ) ) In [ ]: In [ ]: In [ ]
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
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