Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Python3 do not use for loop 3. (12 pts) The three functions below all concern a functional way to store a dictionary. We store a
Python3
do not use for loop
3. (12 pts) The three functions below all concern a functional way to store a dictionary. We store a dictionary as a tuple of 2-tuples: index 0 of each 2-tuple is a key; index 1 is that key's associated value. The order of the keys is irrelevant, but keys are unique: no 2-tuples can have the same keys. For example the dictionary {'a ' : 1, 'b' : 2, 'c' : 3 } might be stored as ( ('b' ,2) , ('a ' , 1) , ( 'c' , 3) )-or any other tuple with these 2-tuples in any order. We call such a data structure an association tuple . (4 pts) Define a recursive function named get assoc; it is passed an association tuple and key as arguments; it returns the value in the association tuple associated with the key. If the key is not in the association tuple, raise the KeyError exception. (4 pts) Define a recursive function named del assoc; it is passed an association tuple and key as arguments; it returns an association tuple that contains all associations except the one specified by the key parameter (keep the order of the other keys in the association tuple the same). If the key is not in the association tuple, raise the KeyError exception. Hint: build a new association tuple with all associations but the deleted one. (4 pts) Define a recursive function named set assoc; it is passed an association tuple, key, and associated value as arguments; it returns an association tuple that contains all associations with (a) a new 2-tuple added at the end for this association (if the key is nowhere in the association tuple) or (b) one of the associations changed (if the key is already in the tuple). Keep the key order the same. For example, 3. (12 pts) The three functions below all concern a functional way to store a dictionary. We store a dictionary as a tuple of 2-tuples: index 0 of each 2-tuple is a key; index 1 is that key's associated value. The order of the keys is irrelevant, but keys are unique: no 2-tuples can have the same keys. For example the dictionary {'a ' : 1, 'b' : 2, 'c' : 3 } might be stored as ( ('b' ,2) , ('a ' , 1) , ( 'c' , 3) )-or any other tuple with these 2-tuples in any order. We call such a data structure an association tuple . (4 pts) Define a recursive function named get assoc; it is passed an association tuple and key as arguments; it returns the value in the association tuple associated with the key. If the key is not in the association tuple, raise the KeyError exception. (4 pts) Define a recursive function named del assoc; it is passed an association tuple and key as arguments; it returns an association tuple that contains all associations except the one specified by the key parameter (keep the order of the other keys in the association tuple the same). If the key is not in the association tuple, raise the KeyError exception. Hint: build a new association tuple with all associations but the deleted one. (4 pts) Define a recursive function named set assoc; it is passed an association tuple, key, and associated value as arguments; it returns an association tuple that contains all associations with (a) a new 2-tuple added at the end for this association (if the key is nowhere in the association tuple) or (b) one of the associations changed (if the key is already in the tuple). Keep the key order the same. For exampleStep 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