Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

HELP WITH THIS PYTHON RECURSION FOR ABOVE 1. Identify the recursive structure of the input (in this case, always a nested list), and write down

image text in transcribed

HELP WITH THIS PYTHON RECURSION FOR ABOVE

1. Identify the recursive structure of the input (in this case, always a nested list), and write down the code template for nested lists:

def f(obj: Union[int, List]) -> ...: if isinstance(obj, int): ... else: ... for sublist in obj: ... f(sublist) ... ...

2. Implement the base case(s) directly (in this case, a single integer). 3. Write down a concrete example with a somewhat complex argument, (in this case, a nested list with around 3 sub-nested-lists), and then write down the relevant recursive calls and what they should return. 4. Determine how to combine the recursive calls to compute the correct output. Make sure you can express this in English first, and then implement your idea.

HINT: The implementations here should be similar to ones you've seen before in the readings or comprehension questions. """ from typing import Union, List

from typing import Union, List def num_positives(obj: Unionint, List])int: ""Return the number of positive integers in sobj>. Remember, 0 is *not positive num_positives(17) num_positivesC-10) num-positivesC[1, 2, [-10, 2, [3], 4, -5], 4]) pass def nested_max(obj: Union[int, List]) -> int: "" "Return the maximum integer stored in nested list does not contain any integers Precondition: all integers in are0 nested_max(17) 17 >>nested_max([1, 2, [1, 2, [3], 4, 5], 4]) pass def max_lengthCobj: Union[int, List] int: ""Return the maximum length of any list in nested list . The maximum length of a nested list is defined as: 1. 0, f is a number. 2. The maximum of len(obj) and the lengths of the nested lists contained in , if is a list >max_length(17) max length(1, 2, [1, 2], 4] 4 >max_length(L1, 2, [1, 2, [3], 4, 5], 4]) pass if name-- main-' import doctest doctest.testmodO)

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

Transactions On Large Scale Data And Knowledge Centered Systems Xxiv Special Issue On Database And Expert Systems Applications Lncs 9510

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Hendrik Decker ,Lenka Lhotska ,Sebastian Link

1st Edition

366249213X, 978-3662492130

Students also viewed these Databases questions

Question

Which keyword is used to create an immutable class in Java?

Answered: 1 week ago

Question

2. Define communication.

Answered: 1 week ago