Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
non-recursive + recursive solutions solution in Creative category for How Deep by CDG.Axel
def how_deep(structure, res=0):
return max(res := res + (i == '(') - (i == ')') for i in str(structure))
# alternative for men who love idexing
return max(res := res + (1, -1, 0)['()'.find(i)] for i in str(structure))
# recursive solution
return 1 + max([how_deep(i) for i in structure if i*0 != 0], default=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!")
Nov. 27, 2021
Comments: