How do I get this even/odd picker to work in python 2.7?

x = []

def find_outlier(integers):
    count = 0
    oddcount = 0
    evencount = 0
    #loop through length of integers
    for n in integers:
        #count should equal length of list by the end, because it goes up for every iteration through integers list
        count += 1
        #below counting number of odd and even integers with oddcount and evencount
        if n % 2 != 0:
            oddcount += 1
            x[0] = n
        if n % 2 == 0:
            eventcount += 1
            x[0] = n
    #loop is over.  If count memory is retained from loop, then it should go through the below conditional nest.  
    #Only one conditional should trigger, since the list is meant to contain only 1 even or one odd
    if len(integers) == count:
        if oddcount == 1:
            return x[0]
        if evencount == 1:
            return x[0]

The instructions are

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

Here is a translation of your code…

>>> def find_outlier(x):
        if len(x) < 3: return False
	y = list(filter(lambda i: i % 2 == 0, x))
	z = list(filter(lambda i: i % 2 != 0, x))
	return len(y) == 1 and y[0] or len(z) == 1 and z[0] or False

>>> a = list(range(0, 20, 2))
>>> find_outlier(a)
False
>>> a[6] = 7
>>> find_outlier(a)
7
>>> b = list(range(1, 20, 2))
>>> find_outlier(b)
False
>>> b[7] = 6
>>> find_outlier(b)
6
>>> 
1 Like

The code I wrote doesn’t run in codewars though, and I’m confused as to why

In this phase we need not keep a count, just build two lists. The one with length 1 is our outlier. If it is empty, then there is not outlier, so return False.

It’s safe to say we can call integers, s for sample list.

>>> def is_outlier(s):
    if len(s) < 3: return
    odd = []
    even = []
    for n in s:
        if n % 2:
            odd.append(n)
        else:
            even.append(n)
    return len(odd) == 1 and odd[0] or len(even) == 1 and even[0] or False

>>> a = list(range(0, 20, 2))
>>> is_outlier(a)
False
>>> a[6] = 7
>>> is_outlier(a)
7
>>> 

Notice we don’t need to keep a count.

Only three elements need to be checked to determine whether the outlier is even or odd:

    outlier_is_even = len([n for n in integers[:3] if n % 2 == 0]) <= 1

Edited on March 28, 2019 to add the following:

After the above, execute a loop to find the outlier, without keeping a count or building additional lists.

My question with this is how would we find the outliers that I have positioned at index 6, above?

def find_outlier(integers):
    # check first three elements
    outlier_is_even = len([n for n in integers[:3] if n % 2 == 0]) <= 1
    # find the outlier
    for n in integers:
        if (n % 2 == 0) == outlier_is_even:
            return n
    
a = list(range(0, 20, 2))
a[6] = 7
print(find_outlier(a))

Output:

7
1 Like

Ah, I see. outlier_is_even will be True when none or one even number is found in the first three. I hadn’t considered that there would be loop iterating over the list that would return the outlier.

1 Like

Sorry if this is a late post to this thread, but I have a side question.

Is

if (n % 2 == 0) == outlier_is_even

the same as

if (n % 2 == 0) && n == outlier_is_even 

???

That’s just confused me for awhile.

The syntax of that line is invalid.

Oh, you mean you can’t have logical operators in conditionals in python?

Please take a moment to consider what logical operators are and where/how they are commonly used.

Also consider what it is you were asking and how @appylpye’s response relates to that, it very much answers the question by pointing out that they are different.

If you think the two lines are equivalent then arguing/reasoning for this is probably going to allow you to answer that all by yourself. Do you know what each thing there does? Well, then you should have no problem saying whether they’re the same, or if you don’t, then go find out what the part you don’t know what it does, does, there’s no point trying to reason about the whole thing at once if there’s something in it that you don’t understand.

In general you’ll want to throw out ideas that you can’t argue for, and argue for everything that you ever use. Stick to solid ground, cut out any guessing.

For example, where does the question of whether those two lines are equivalent even come from? Can you argue for why they would be the same?

python.org is a good place to go regularly for information on Python.

Check out Boolean Operations — and , or , not. However, do we really need any of those operators to perform the appropriate test here?