Question: no loops recursive function it is passed data that contains int, str, tuple, list, set, frozenset, and dict values (including nested versions of any of

no loops

recursive function it is passed data that contains int, str, tuple, list, set, frozenset, and dict values (including nested versions of any of these data structures as an argument). It returns an immutable equivalent data structure (one that could be used for values in a set or keys in a dict). The types int, str, and frozenset are already immutable. Convert a set to a frozenset; convert all the values in a tuple to be their immutable equivalents, in the same order); convert a list to a tuple (with immutable equivalents of its values, in the same order); convert a dict to tuple of 2-tuples (see the association tuple in question 2) but here with the 2-tuples sorted by the dicts keys. If immutify ever encounters a value of another type (e.g., float) it should raise a TypeError exception.

immutify( {'b' : [1,2], 'a' : {'ab': {1,2}, 'aa' : (1,2)}} )

returns the immutable data structure

(('a', (('aa', (1, 2)), ('ab', frozenset({1, 2})))), ('b', (1, 2)))

Example 2:

immutify( [{1,2}, {3,frozenset([4,5])}, {6,7}])

Returns (frozenset({1, 2}), frozenset({3, frozenset({4, 5})}), frozenset({6, 7}))

Hints: What is the base case, returning simple results? You can convert a set into a frozenset just by using its constructor. When converting values is a tuple to be immutable, or converting a list to a tuple, you must use recursion (so what are the base and recursive cases?) - not use a for loop or comprehension. In this problem, for dict only, you may use a for loop or comprehension to iterate through the dict to convert it to an association tuple (although one is not necessary, it is more efficient). Note that values in sets and keys in dicts must already be immutable.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!