Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Recursive dive solution in Clear category for How Deep by MaxOnCeckIo
def how_deep(structure):
"""Return the depth for nested tuples."""
if not structure: # By default an empty tuple will have depth == 1
return 1
else:
if isinstance(structure, tuple): # If it's a tuple, use recursion to check the max depth
return 1 + max(how_deep(item) for item in structure)
else:
return 0
if __name__ == '__main__':
print("Example:")
print(how_deep((1, 2, 3)))
# These "asserts" are used for self-checking and not for an auto-testing
assert how_deep((1, 2, 3)) == 1
assert how_deep((1, 2, (3,))) == 2
assert how_deep((1, 2, (3, (4,)))) == 3
assert how_deep(()) == 1
assert how_deep(((),)) == 2
assert how_deep((((),),)) == 3
assert how_deep((1, (2,), (3,))) == 2
assert how_deep((1, ((),), (3,))) == 3
print("Coding complete? Click 'Check' to earn cool rewards!")
June 15, 2021
Comments: