Skip to content Skip to sidebar Skip to footer

Find The Parity Outlier Python

def find_outlier(lstOfints): for integer in lstOfints: #print(integer) if integer % 2 ==1: integer elif integer % 2 == 0: integer return integer I keep

Solution 1:

This seems to work for me:

def find_outlier(lstOfints):
    if lstOfints[0] % 2 == 1 and lstOfints[1] % 2 == 1:
        forintegerin lstOfints[2::]:
            ifinteger % 2 == 0:
                returnintegerelif lstOfints[0] % 2 == 0 and lstOfints[1] % 2 == 0:
        forintegerin lstOfints[2::]:
            ifinteger % 2 == 1:
                returnintegerelse:
        if lstOfints[0] % 2 == lstOfints[2] % 2:
            return lstOfints[1]
        else:
            return lstOfints[0]

Firstly, I am seeing if this list is either all odds, or all evens, bar the outlier. Then if it is even, it goes through the list looking for an odd number, the same for if the list is all odd. However, if the outlier is in the first two numbers, then it checks to see whether the outlier is the first number or the second number by comparing the modulos to the modulo of the third number in the list.

Solution 2:

A pythonic answer might be something like that:

deffind_outlier(lst):
  mods = [n % 2for n in lst]
  return lst[mods.index(0)] ifsum(mods[:3]) > 1else lst[mods.index(1)]

print(find_outlier([5, 31, 57, 45, 9, 17, 8, 37, 123, 67])) # --> 8print(find_outlier([4, 30, 56, 44, 8, 16, 7, 36, 122, 66])) # --> 7

Explanation: first create the list of all n % 2 for all n in the provided list. Then you simply have to check the sum of the first 3 elements of this new list to know whether the outlier is odd or even. Then find index of the outlier remainder (either 0 or 1) and return the corresponding integer from the first list...

Solution 3:

There are many things to change in your code, so I will try to change it at steps to get it working at the end.

Run the function from the question

Well, I tested your code here and I see why it is confusing. I have this script:

def find_outlier(lstOfints):
  forintegerin lstOfints:
    #print(integer)ifinteger % 2 ==1:
      integerelifinteger % 2 == 0:
      integerreturnintegerprint(find_outlier([1,3,4,5]))
print(find_outlier([1,2,3,4]))

...and if I run it, we get this:

$ python disparity.py
54

It is just printing the last number! Why so?

Why is it always printing the last number from the list?

Well, because you just return at the end of the function. You expect the function to return from inside the if statement, but you did not used the return command. It is easy to solve:

def find_outlier(lstOfints):
  forintegerin lstOfints:
    ifinteger % 2 ==1:
      returnintegerelifinteger % 2 == 0:
      returnintegerreturnintegerprint(find_outlier([1,3,4,5]))
print(find_outlier([0,2,3,4]))

So now we run it and get

$ python disparity.py
10

Now it is just returning the first element!

Why is it now always printing the first number from the list?

It prints the first element because your if statements are complementary. Given any element, regardless of its parity, it modulo 2 will be either 0 or 1. If it is zero, the first if returns; if it is 1, the second returns! Since it is true to all elements, it is true to the first one, so the function ends at the very first iteration.

This is not the algorithm you want. What you want is to first know which parity is the outlier, and then look for the outlier number.

How to discover which parity is an outlier if I don't know which number is the outlier?

Since the list has at least three elements, that is easy: we consider two scenarios.

  • The first scenario is, the outlier is one of the first three elements. In this case, we will have one outlier parity and two "normal" parities. An array containing the numbers 1 4 5 3 7 11 and 9. Notice that all numbers are odd except for 4, and that 4 is one of the first three values in the array.
  • The second scenario is, the outlier is not one of the first three elements. In this case, we will have three equal parities, and the outlier will be the opposite one. An array containing the numbers 1 3 5 7 11 4 and 9. Notice that all numbers are odd except for 4, and that 4 is *not* one of the first three values in the array.

So, if we count the number of even numbers and odd numbers in the first three values, we know that the parity with less numbers is the outlier one.

  • If there are more even numbers than odd numbers (either two even and one odd, or three even and no odd), then we know the outlier is odd.
  • If there are more odd numbers than even numbers, we know the outlier is even.

Now, how to do it in code?

To do that, I will write pieces of code below that, at then end, put together, will give us the answer. First I create two variables to count the even and odd numbers (with 0 at first).

even_numbers = 0odd_numbers = 0

Then, I see if the first element is even. If so, I increment the counter of even numbers:

iflistOfints[0]%2==0:even_numbers=even_numbers+1else:odd_numbers=odd_numbers+1

Then I do the same thing with the second and third elements:

iflistOfints[1]%2==0:even_numbers=even_numbers+1else:odd_numbers=odd_numbers+1iflistOfints[2]%2==0:even_numbers=even_numbers+1else:odd_numbers=odd_numbers+1

If we have more even numbers than odd numbers, we know the outlier parity is odd (that is, the rest of the division of the outlier to 2 is 1); if we have more odd numbers than even numbers, the outlier parity will be even (that is, the rest will be zero)

ifeven_numbers>odd_numbers:outlier_parity=1else:outlier_parity=0

So, now I know if the outlier number is even or odd, I agree. But I want to know what is the number!

Now that we do know the parity of the outlier, we just need to look for it:

forintegerin lstOfints:
    ifinteger % 2 == outlier_parity:
      returninteger

Bundling all together, this is our new function:

def find_outlier(lstOfints):
  even_numbers = 0
  odd_numbers = 0

  if lstOfints[0] % 2 == 0:
      even_numbers = even_numbers + 1
  else:
      odd_numbers = odd_numbers + 1

  if lstOfints[1] % 2 == 0:
      even_numbers = even_numbers + 1
  else:
      odd_numbers = odd_numbers + 1

  if lstOfints[2] % 2 == 0:
      even_numbers = even_numbers + 1
  else:
      odd_numbers = odd_numbers + 1

  if even_numbers > odd_numbers:
      outlier_parity = 1
  else:
      outlier_parity = 0


  forintegerin lstOfints:
    ifinteger % 2 == outlier_parity:
      returnintegerprint(find_outlier([1,3,4,5]))
print(find_outlier([0,2,3,4]))

Does it work? Let's test:

$ python disparity.py
43

Oh, it worked! Not bad, right? :)

Some considerations

This code here isn't the most elegant, but my purpose here is mostly that you understand what is going on. It could be done with a waaaaaay shorter, more readable function, but since you are still looking for basic concepts, I did a more detailed, explainable function, which is kind of cumbersome as a consequence.

Solution 4:

my solution is:

deffind_outlier(integers):
    odd = []
    even = []
    for i in integers:
        if i% 2 != 0:
            odd.append(i)
        elif i% 2 == 0:
            even.append(i)
    iflen(odd) < len(even):
        return odd[0]
    else:
        return even[0]

Post a Comment for "Find The Parity Outlier Python"