Objective
Create a Python program with a generator that produces the Collatz sequence, a generator whose length is not known beforehand.
Instructions
- Define
def collatz(n): whilenis different from 1, yieldnand change it ton // 2if it is even or to3 * n + 1if it is odd. When it finishes, yield the final 1. - Read an integer
n(at least 1) and store the sequence in a list withlist(collatz(n)). - Show the sequence on one line, separated by spaces, and on the next one
Steps: K, where K is the number of elements minus 1.
6
6 3 10 5 16 8 4 2 1
Steps: 8
Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.
Start exercise