Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

The problem is as follows:

  • Design a function that generates the number of steps in the Collatz Conjecture
  • For more information
  • Essentially, for a given number, the sequence begins reducing the number until the value becomes 1. I wish to calculate the the amount of steps in the sequence for a given n (in other word, the number of terms produced by the sequence before the value 1 is produced)

Examples:

collatz_steps(1) => 0
collatz_steps(6) => 8

My attempt:

def collatz_steps(n):
    L = []
    if n == 1:
        return len[L] + 1
    if n%2 == 0:
        L.append(n)
        collatz_steps(n/2)
    else:
        L.append(n)
        collatz_steps((3*n)+1)
share|improve this question

closed as off-topic by Josay, BeetDemGuise, Dave Yarwood, ckuhn203, Janne Karila Jun 12 at 17:50

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. Such questions may be suitable for Stack Overflow or Programmers. After the question has been edited to contain working code, we will consider reopening it." – Josay, BeetDemGuise, Dave Yarwood, ckuhn203, Janne Karila
If this question can be reworded to fit the rules in the help center, please edit the question.

1  
Welcome to CodeReview.SE! It is a place to review working code. Unfortunately, I have the feeling that your code does not work at the moment (for a start, it doesn't return anything). –  Josay Jun 12 at 16:52
    
Understood, my apologies ... Will delete and repost with working code –  Hassan Jun 12 at 21:56
add comment

1 Answer

You could solve this with recursion, methinks. However, your current implementation is buggy:

  1. len[L] will throw a TypeError
  2. collatz_steps(1) returns 1 on my machine
  3. collatz_steps(6) returns nothing on my machine

The biggest problem with your solution is that you assume that L is the same each recursive call. However, on each step you go down, L gets reallocated as a new list. Because of this, the most len(L) will be is 1.

Another problem is that once you call collatz_steps recursively, you don't return that value. The because you only return a result when n==1 your final value will never propagate back up.


Instead of using recursion, I would suggest using a simple while loop to get the same result:

def collatz_steps(num):
    iterations = 0

    while num != 1:
        iterations += 1

        if num % 2 == 0:
            num /= 2
        else:
            num = 3*num + 1

    return iterations

# Prints:
#    0
#    8
print(collatz_steps(1))
print(collatz_steps(6))

You could even make the if statement on one line if you wanted:

def collatz_steps(num):
    iterations = 0

    while num != 1:
        iterations += 1
        num = 3*num + 1 if num % 2 else n/2

    return iterations

As some general style feedback:

  1. Don't use single letter variable names. They offer little-to-no help defining what they hold. Always err on the side of being too verbose when naming variables.
  2. Don't use all uppercase letters unless the variable is supposed to be constant. Convention is the only way we can define constants in Python as there is no syntactic way of doing so.

If you really wanted to use recursion, here is a recursive version of collatz_steps:

def collatz_steps(num, iterations=0):
    if num == 1:
        return iterations

    iterations += 1
    return collatz_steps(3*num + 1, iterations) if num % 2
                                                else collatz_steps(n/2, iterations) 
share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.