Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- arr = [1, 1, [1], [[1, 1, [1, 1]]], [1, [1, 1], 1]]
- def f(x):
- if isinstance(x,(float,int)):
- return x
- else:
- return sum(f(z) for z in x)
- def g(x):
- brr=[x]
- s=0
- while brr:
- if isinstance(u:=brr.pop(),(float,int)):
- s+=u
- else:
- brr.extend(u)
- return s
- print(f(arr))
- print(g(arr))
- from timeit import *
- for u in [f,g,g,f,f,g,g,f]:
- print(f'{u.__name__}: {timeit(lambda:u(arr),number=100000)}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement