Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed
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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions

Question

How many Americans are members of labor unions?

Answered: 1 week ago

Question

1. Socialization policy in mass media?

Answered: 1 week ago

Question

1. What is employment? 2. What is the rewards for employment?

Answered: 1 week ago

Question

1. What is meant by Landslide? 2.The highest peak in Land?

Answered: 1 week ago