1

I am currently trying to build a function that finds the second divisor of a number (n) and returns the index of the second divisor WITHOUT calling the build-in index function. xs is a list and n is the number to be divided example would be: locate_second_divisor([20,3,4,2],12) yields 2

My current code

count=0
def locate_second_divisor(xs,n):
     count=0
     for num in xs:
        if n % num==0:
          count+=1
        if count==2:
         return

At return, I need to write the index of the second divisor but I can't think of how to do it without calling index.

This is not a duplicate question as I am not allowed to use ANY built in functions. I can only use append, int, float, str, and loops with booleans and operators. I cannot use enumerate like similar questions can. I need some sort of way around the built in functions.

Updated code (only fails because I have to include None if there is no second divisor)

def locate_second_divisor(xs,n): count=0 index=0 for num in xs: if (n % num)==0: count+=1 if count ==2: return index else: return None

ocean.1234
  • 129
  • 2
  • 13

1 Answers1

2

If I understand correctly, you can just track the index the same way you're tacking the count of the times your condition is met.

def locate_second_divisor(xs, n):
     count = 0
     index = 0
     for num in xs:
        count += (n % num) == 0

        if count == 2:
            return index
        else:
            index += 1 

print(locate_second_divisor([20,3,5,3,4], 12))
# 3

Fun note: Since (n % num) == 0 evaluates to True or False, which safely cast to 1 and 0, you can simplify your first if block to: count += (n % num) == 0

Paul H
  • 65,268
  • 20
  • 159
  • 136
  • Okay so I think that is a good set up, but I am still failing tests. For instance, locate_second_divisor([20,3,5,3,4],12) should return 2, but instead is giving none. @Paul H – ocean.1234 Mar 02 '17 at 01:50
  • @ocean.1234 check your indentation. it works as expected for me. Also note that your example is wrong. the second divisor is the second occurrence of 3, which is the 4th element, which is located at index 3, – Paul H Mar 02 '17 at 04:36
  • you are correct that my example is wrong. I see that. I still can't get the function to past the tests though. – ocean.1234 Mar 02 '17 at 15:17
  • Also @Paul H, it needs to include an else statement that returns none. Here is my updated code:def locate_second_divisor(xs,n): count=0 index=0 for num in xs: if (n % num)==0: count+=1 if count ==2: return index else: return None – ocean.1234 Mar 02 '17 at 15:27
  • @ocean.1234 you don't need that last `else` since `None` is returned by default. See for yourself with: `print(locate_second_divisor([20,3,5,3,4], 7))` – Paul H Mar 02 '17 at 17:17
  • you were right. I didn't need the last else statement. I removed it but my function is just returning 0 each time. I don't think it is updating count or something. – ocean.1234 Mar 02 '17 at 17:52
  • restart your interpreter, check your indentation. my implementation is working as expected for me. – Paul H Mar 02 '17 at 18:33