Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help with the following 5 Python Object-Oriented programming problems. Thanks! Question 1 1 pts Recall our recursive definition of a nested list: A nested

Please help with the following 5 Python Object-Oriented programming problems. Thanks!

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

Question 1 1 pts Recall our recursive definition of a nested list: A nested list is one of two types of values: A single integer A list of other nested lists ([ 1st_1, Ist_2,..., 1st_n ]). Each lst_i is called a sub-nested-list of the outer list. According to this definition, select all of the values that are valid nested lists below. [1,2,3] 10 O [1, [2, 3], [], [4, 5]] O [1, [2, 3], 'hello'] None Question 2 1 pts Nested lists can get very complex very quickly, and trying to go "down every level in a nested list is both time-consuming and hard to keep track of. This is why when we're thinking recursively, we rarely go down more than one level in a nested list. To prepare for this idea, review from the readings the definition of sub-nested-list, and then answer the following question: How many sub-nested-lists does [[15, [105, 13]], [0, 1, 2, 3], 4, [[1]] have? 06 04 0 1 13 O 8 Question 3 1 pts This series of exercises is designed to help you get used to the technique of partial tracing we use when dealing with recursive data structures and code. Consider the following function: def num_integers(obj: Union[int, List]) -> int "Return the number of integers stored in the given nested list.""" # Implementation hidden Before even getting to the implementation of this function, we prepare by designing an example and decomposing it into its main recursive calls. Our example is the following nested list. [[15, [105, 13]], [0, 1, 2, 3], 4, [1] In the table below, we've written each sub-nested-list of this list, as well as the list itself. Complete the table by writing down what would be returned when we call num_integers on the corresponding nested list. obj num_integers(obj) [15, (105, 13]] [0, 1, 2, 3] [[15, (105, 13]], [0, 1, 2, 3], 4, [)] Based on your table in the previous question, describe in English how you would calculate num_integersC[[15, [105, 13]], [0, 1, 2, 3], 4, [D] ) using the values of the recursive calls on its sub-nested-lists. Note: we aren't marking this question, but it's still very good practice to do before jumping right into the code! Question 4 1 pts Now, here is a correct implementation of the recursive case of num_integers : def num_integers(obj: Union[int, List]) -> int: if isinstance(obj, int): else: # Recursive part S = 0 for sublist in obj: S += num_integers(sublist) returns This implementation uses the basic "loop through each sub-nested-list" pattern you read about in the notes, together with an accumulator variables that's eventually returned. Practice tracing through the call num_elements([[15, [105, 13]], [0, 1, 2, 3], 4, [1]) by completing the following table, which shows how s changes after each loop iteration, assuming each recursive call is correct. sublist num_integers(sublist) Value of s after processing sublist O(initial value) (15,[105, 13]] [0, 1, 2, 3] You should find that the final value of s (in the bottom-right cell of the table) matches the correct output of num_elements([[15, [105, 13]], [0, 1, 2, 3], 4, []). This verifies that the recursive step is correct, assuming each recursive call is correct. Question 5 1 pts The assumption we made in our previous tracing---that the recursive calls always returned the right values--might seem odd. But as we've discussed in the readings, this assumption is valid as long as we're confident that the base case is correct. So to complete this exercise, select the correct base case implementation that should fill in the ... below: def num_integers(obj: Union[int, List]) -> int: if isinstance(obj, int): else: # Recursive part S = 0 for sublist in obj: s += num_integers(sublist) returns return [obj] return 1 return None returno return obj

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_2

Step: 3

blur-text-image_step3

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