Question: This problem require recursive. Should I use if, elif to separate types of argument and make it as my base case ? I don't know

This problem require recursive. Should I use if, elif to separate types of argument and make it as my base case ? I don't know how to make the problem simpler. Please help

This problem require recursive. Should I use if, elif to separate types

4. (5 pts) Define a recursive function named immutify; it is passed any data structure 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 dict's keys. If immutify ever encounters a value of another type (e.g., float) it should raise a TypeError exception. The following call (with many mutable data structures) 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) ) ) 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 Programming Questions!