fromfuncutilsimportproc_seqdefcollatz(n):"""The Collatz function
>>> collatz(0)
0
>>> collatz(3)
10
>>> collatz(8)
4
>>> collatz(-8)
-4
"""ifn%2==0:returnn//2# // instead of / for an integer result
else:return3*n+1defcollatz_sequence(n):"""Return the Collatz sequence seeded by n as a list.
>>> collatz_sequence(1)
[1]
>>> collatz_sequence(8)
[8, 4, 2, 1]
"""returnproc_seq(n,collatz,lambdax:x>1,lambdastate,x:state+[x],[])+[1]defcollatz_length(n):"""The length of the Collatz sequence seeded by n (n and 1 are included)
>>> collatz_length(8)
4
>>> collatz_length(3)
8
"""returnproc_seq(n,collatz,lambdax:x>1,lambdastate,x:state+1,1)