1

I'm completing this Codewars exercise. Here are the instructions:

You are given an array (which will have a length of at least 3, but could be very large) containing integers. The integers in the array are either entirely odd or entirely even except for a single integer N. Write a method that takes the array as an argument and returns N.

For example:

[2, 4, 0, 100, 4, 11, 2602, 36]

Should return: 11

[160, 3, 1719, 19, 11, 13, -21]

Should return: 160

However, when I submit the following code:

def ifeven(list):
     #Determine if we are dealing with list of evens or odds
    sum = 0
    for ran in (0,1,2):
        sum += abs(list[ran])%2
    avg = sum/3
    r_avg = round(avg)
    return r_avg == 0

def find_outlier(integers):
    even =  ifeven(integers)
    new = []
    for num in integers:
        new.append(num%2)
    if even:
        loc = new.index(1)
    else:
        loc = new.index(0)
    return integers[loc]

With this test case (see exercise link):

test.assert_equals(find_outlier([1,2,3]), 2)

For some I reason I get the error 1 should equal 2 even though if I run the code on another compiler, I get 2 as the (correct) answer.

Is the issue with my code or Codewars's compiler?

zthomas.nc
  • 3,689
  • 8
  • 35
  • 49
  • 3
    Check the version of python being used. Python 2 and 3 handle the `/` operator differently. See here http://stackoverflow.com/questions/21316968/division-in-python-2-7-and-3-3 – nbryans Jun 14 '16 at 20:44
  • ^That was the issue. Thanks. – zthomas.nc Jun 14 '16 at 20:47
  • Please post this as an answer or tag as a duplicate. – Prune Jun 14 '16 at 20:51
  • 1
    Just FYI, your code is doing a lot more work than you really have to. [Here's a shorter solution](http://pastebin.com/g0p6hkiG). We can chat about it on here if you have questions. Happy coding! – Two-Bit Alchemist Jun 14 '16 at 20:53
  • @Two-BitAlchemist Yes -- I like that solution, somewhat reflects my similar approach of looking at the first three elements but in a much more concise manner. Thanks. – zthomas.nc Jun 14 '16 at 23:08

1 Answers1

2

As suggested by /u/Prune, here is the answer:

Check the version of python being used. Python 2 and 3 handle the / operator differently. See here: Division in Python 2.7. and 3.3

Community
  • 1
  • 1
nbryans
  • 1,507
  • 17
  • 24