Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

python recursive and no for loop or local variable 6. (1 pt extra credit) Define a recursive function named my_str; it is passed an int

python recursive and no for loop or local variable

image text in transcribed

6. (1 pt extra credit) Define a recursive function named my_str; it is passed an int or list whose elements are ints or lists of ints, or lists of lists of ints, etc. It returns a string: the same string that str returns when called on such an argument (but, of course, you cannot call str or ___str__ etc. You must write your own recursive my_str function that solves this problem). my_str must deal with references inside the list that refer to the list. For example, if we wrote x = [1,1] and then x[1] = x the following structure results. list 0 1 str(x) returns [1, [...]]; my_str(x) should return the same. In a recursive call, when asked to convert a list that is already being converted, immediately return [...]. An even more subtle example (draw it) is x,y = [1],[1] and x [0] =y and y [0]=x; str(x) is [[[...]]]. The driver/bsc contain other, more complex examples. Create your own self-referential lists and call str to see what my_str should return. Hint: I wrote my function to define a set and a recursive helper function (that can examine/mutate the set); my_str returns the result of calling the helper. The helper converts the list (and sublists, and subsublists, ...) into a string. Before converting each list, put its id in the set; if the helper function is ever recursively required to compute the str of a list whose id is in the set, just immediately return [...]; actually recurring would result in an infinite recursion. Recall the id function: calling id (o) returns an int corresponding to the location in memory of object o. We know x is y is True exactly when id(x) ==id (y) is True. In this problem you may use for in a comprehension to help (I called .join on it).Don't worry if you cannot solve this problem, it is extra credit. 6. (1 pt extra credit) Define a recursive function named my_str; it is passed an int or list whose elements are ints or lists of ints, or lists of lists of ints, etc. It returns a string: the same string that str returns when called on such an argument (but, of course, you cannot call str or ___str__ etc. You must write your own recursive my_str function that solves this problem). my_str must deal with references inside the list that refer to the list. For example, if we wrote x = [1,1] and then x[1] = x the following structure results. list 0 1 str(x) returns [1, [...]]; my_str(x) should return the same. In a recursive call, when asked to convert a list that is already being converted, immediately return [...]. An even more subtle example (draw it) is x,y = [1],[1] and x [0] =y and y [0]=x; str(x) is [[[...]]]. The driver/bsc contain other, more complex examples. Create your own self-referential lists and call str to see what my_str should return. Hint: I wrote my function to define a set and a recursive helper function (that can examine/mutate the set); my_str returns the result of calling the helper. The helper converts the list (and sublists, and subsublists, ...) into a string. Before converting each list, put its id in the set; if the helper function is ever recursively required to compute the str of a list whose id is in the set, just immediately return [...]; actually recurring would result in an infinite recursion. Recall the id function: calling id (o) returns an int corresponding to the location in memory of object o. We know x is y is True exactly when id(x) ==id (y) is True. In this problem you may use for in a comprehension to help (I called .join on it).Don't worry if you cannot solve this problem, it is extra credit

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions