-1

I'm new to python, getting an index error on line 6. what's the deal? trying to create a simple recursion pattern to solve a problem.

Problem// For example, suppose the numbers list were [1, 3, 0, 1]. 0 redirects to 1, who redirects to 3, who redirects back to 1. There is a loop of two : 1, 3. Thus the answer would be 2. Note that even though you started with 0, it is not part of the loop.

some test cases my function must output

Test cases

Inputs: (int list) numbers = [1, 0] Output: (int) 2

Inputs: (int list) numbers = [1, 2, 1] Output: (int) 2

def answer(numbers,depth=0):
    if depth > 0:
        if depth > numbers:
            return false
        for m in range(0,depth):
            if numbers[m] == numbers[depth]:
                return depth+1;
    return answer(numbers, depth+1)
2
  • Can you write it iteratively? Commented Sep 12, 2015 at 1:51
  • im sorry i'm not sure,what this is asking, is the for loop not iterating? Commented Sep 12, 2015 at 3:04

1 Answer 1

0
range(0,depth) 

returns 0,1,..,depth-1

Also

if depth > numbers:

does not make sense: comparing integer with list

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.