きったんの頭

#! /usr/bin/env python3
"""
Collatz
https://mind.kittttttan.info/py/collatz
"""
from time import time

def collatz(n=1):
    if n > 0:
        list = []
        while n > 1:
            list.append(n)
            if n & 1:
                n = 3 * n + 1
            else:
                n >>= 1
        if n == 1:
            list.append(n)
            return list
        else:
            raise ValueError("unexpected error")
    elif n < 0:
        list = []
        while n < -1:
            if n == -1 or n == -5 or n == -17:
                break
            list.append(n)
            if n & 1:
                n = 3 * n + 1
            else:
                n >>= 1
        list.append(n)
        return list
    elif n == 0:
        return [0]
    else:
        raise TypeError("input integer only")

def main():
    x = 7
    c = collatz(x)
    print("c = collatz(%s)" % x)
    print("c[0] =", c[0])
    print("len(c) =", len(c))
    print("max(c) =", max(c))
    print("min(c) =", min(c))

if __name__ == "__main__":
    main()