Question

def max_length(obj: Union[int, List]) -> int: """Return the maximum length of any list in nested list...

def max_length(obj: Union[int, List]) -> int:
"""Return the maximum length of any list in nested list <obj>.

The *maximum length* of a nested list is defined as:
1. 0, if <obj> is a number.
2. The maximum of len(obj) and the lengths of the nested lists contained
in <obj>, if <obj> is a list.

>>> max_length(17)
0
>>> max_length([1, 2, [1, 2], 4])
4
>>> max_length([1, 2, [1, 2, [3], 4, 5], 4])
5
"""
pass

Homework Answers

Answer #1

def max_length(accept)->int:
currentMaxLength=0;
if(isinstance(accept, int)):
return 0;
else :
subListsLength = 0;
currentMaxLength = len(accept);
for lists in accept:
if(not isinstance(lists, int)):
subListsLength = len(lists)
if(subListsLength>currentMaxLength):
currentMaxLength = subListsLength
return currentMaxLength
  
return 0
  
print(max_length([5,4,[1,2,3,4,5],1]))

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
from typing import List def longest_chain(submatrix: List[int]) -> int: """ Given a list of integers, return...
from typing import List def longest_chain(submatrix: List[int]) -> int: """ Given a list of integers, return the length of the longest chain of 1's that start from the beginning. You MUST use a while loop for this! We will check. >>> longest_chain([1, 1, 0]) 2 >>> longest_chain([0, 1, 1]) 0 >>> longest_chain([1, 0, 1]) 1 """ i = 0 a = [] while i < len(submatrix) and submatrix[i] != 0: a.append(submatrix[i]) i += 1 return sum(a) def largest_rectangle_at_position(matrix: List[List[int]], x:...
def count_evens(values: List[List[int]]) -> List[int]: """Return a list of counts of even numbers in each of...
def count_evens(values: List[List[int]]) -> List[int]: """Return a list of counts of even numbers in each of the inner lists of values.    >>> count_evens([[10, 20, 30]]) [3] >>> count_evens([[1, 2], [3], [4, 5, 6]]) [1, 0, 2] """   
def count_evens(values: List[List[int]]) -> List[int]: """Return a list of counts of even numbers in each of...
def count_evens(values: List[List[int]]) -> List[int]: """Return a list of counts of even numbers in each of the inner lists of values.    >>> count_evens([[10, 20, 30]]) [3] >>> count_evens([[1, 2], [3], [4, 5, 6]]) [1, 0, 2] """
#Constructor def tree(label, branches=[]): for branch in branches: assert is_tree(branch) return [label] + list(branches) #Selectors def...
#Constructor def tree(label, branches=[]): for branch in branches: assert is_tree(branch) return [label] + list(branches) #Selectors def label(tree): return tree[0] def branches(tree): return tree[1:] def is_tree(tree): if type(tree) != list or len(tree) < 1: return false return True def is_leaf(tree): return not branches(tree) def print_tree(t, indent=0):    print(' ' * indent + str(label(t))) for b in branches(t): print_tree(b, indent + 1) Write a function that takes in a tree and doubles every value. It should return a new tree. You can...
def is_even(value: int) -> bool: """Return True if and only if value is an even number....
def is_even(value: int) -> bool: """Return True if and only if value is an even number.    >>> is_even(108) True >>> is_even(135) False """ def convert_time(hour: int) -> int: """Return the given time in hours hour from the 24 hour clock to a time in hours for the 12 hour clock. Precondition: 0 <= hour <= 23 >>> convert_time(0) 12 >>> convert_time(4) 4 >>> convert_time(15) 3 """
"""Prep 7 Synthesize === CSC148 Fall 2020 === Department of Mathematical and Computational Sciences, University of...
"""Prep 7 Synthesize === CSC148 Fall 2020 === Department of Mathematical and Computational Sciences, University of Toronto Mississauga === Module Description === Your task in this prep is to implement each of the unimplemented Tree methods in this file. The starter code has a recursive template that includes the "size-one" case; you may or may not choose to use this in your final implementations. """ from __future__ import annotations from typing import Any, List, Optional class Tree: """A recursive tree...
Given: int[][] matrix = new int[5][5] Using the statement above, write a nested loop to set...
Given: int[][] matrix = new int[5][5] Using the statement above, write a nested loop to set matrix as follows: [0] [1] [2] [3] [4] [0] 1 0 0 0 0 [1] 0 2 0 0 0 [2] 0 0 3 0 0 [3] 0 0 0 4 0 [4] 0 0 0 0 5
In Python: Sublist of list A is defined as a list whose elements are all from...
In Python: Sublist of list A is defined as a list whose elements are all from list A. For example, suppose list A = [0, 1, 2, 3, 4, 5, 6], its has many sublists and one of them is [0, 1, 3] because elements 0, 1 and 3 are all contained in list A. Define a function named returnComplement that accepts two integer lists as the parameter (one of the list is the sublist of the other). Suppose names...
create a function that takes a dictionary and returns a list of int. The list should...
create a function that takes a dictionary and returns a list of int. The list should appear in decreasing order based on the sum of number in each dictionary. def total_num(dict1): #Code here input = {1: {'una': 5, 'dos': 7, 'tres': 9, 'quar' : 11}, 2: {'dos':2, 'quar':3}, 3:{'una': 3, 'tres': 5}, 4:{'cin': 6}, 5:{'tres': 7 , 'cin': 8}} output = [1,5,3,4,2] 1: 38 2: 5 3: 8 4: 6 5: 15 1>5>3>4>2
Implement the following functions in the given code: def random_suit_number(): def get_card_suit_string_from_number(n): def random_value_number(): def get_card_value_string_from_number(n):...
Implement the following functions in the given code: def random_suit_number(): def get_card_suit_string_from_number(n): def random_value_number(): def get_card_value_string_from_number(n): def is_leap_year(year): def get_letter_grade_version_1(x): def get_letter_grade_version_2(x): def get_letter_grade_version_3(x): Pay careful attention to the three different versions of the number-grade-to-letter-grade functions. Their behaviors are subtly different. Use the function descriptions provided in the code to replace the pass keyword with the necessary code. Remember: Parameters contain values that are passed in by the caller. You will need to make use of the parameters that each...