I completed 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
Any comments on the following code appreciated. (I used Python 2.7)
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 = float(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]