Finding the Parity Outlier in a list of odd/even integers

烂漫一生 提交于 2021-02-04 21:31:26

问题


I'm trying to find and return the single even integer in a list of odd integers or the sole odd integer in a list of even integers. My code works, however, if the length of the list for odd integers is even, it returns the first number in the list instead of the even integer. Any help is appreciated. Code below:

    even = [2, 4, 6, 8, 10, 12, 14, 2091] #2091 is the outlier and len(x) = 8
    odd = [1, 3, 5, 7, 9, 11, 13, 4720] #4720 is the outlier and len(x) = 8

def find_outlier(x):
    # Determine if list is even and length of list is odd
    if sum(x) % 2 != 0 and len(x) % 2 != 0:
        for x in x:
            if x % 2 != 0:
                return x
    # Determine if list is even and length of list is even
    elif sum(x) % 2 != 0 and len(x) % 2 == 0:
        for x in x:
            if x % 2 != 0:
                return x
    # Determine if list is odd and length of list is odd
    elif sum(x) % 2 == 0 and len(x) % 2 != 0:
        for x in x:
            if x % 2 == 0:
                return x
    # Determine if list is odd and length of list is even (PROBLEM)
    elif sum(x) % 2 == 0 and len(x) % 2 == 0:
        for x in x:
            if x % 2 == 0:
                return x

print (find_outlier(even))
print (find_outlier(odd))

Below is the correct solution to the problem reformatted from Dmitri's solution:

def find_outlier(x):
    odd = [i for i in x if i % 2 != 0]
    even = [i for i in x if i % 2 == 0]
    return odd[0] if len(odd) < len(even) else even[0]

回答1:


Why do you care about the length at all? Here is much simpler solution:

def find_outliner(a):
    if a[0] % 2 != a[1] % 2:
        return a[0] if a[2] % 2 == a[1] % 2 else a[1]
    for i in a:
        if i % 2 != a[0] % 2:
            return i

How does it work?

  1. There should be at least three elements in the list, otherwise the problem does not make much sense
  2. If the first two elements have different parity, check which element is wrong based on third (if the second and the third have the same parity, the first is the wrong one, otherwise - the second is the wrong one)
  3. If the first two elements have the same parity, that means that parity of the first (and the second) element is the correct parity for list. Thus, the element with different parity is what we are looking for.



回答2:


The problem is you are summing the list trying to use that to determine whether the list is primarily odds or evens. That doesn't work because the sum of a list of evens is even, but a sum of odds will be even or odd based on the number of items in the list.

Instead, just keep track of the last even and odd in the list and whether you have seen more than one of a given type. And as soon as you have two evens or two odds you can assume the outlier is the other one.

Also, using sum() and len() in each IF clause can be incredibly inefficient because you are doing it multiple times.




回答3:


Just an alternative, collecting the evens and odds in two lists and then using the smaller list.

def find_outlier(x):
    evenodd = [], []
    for v in x:
        evenodd[v & 1].append(v)
    return min(evenodd, key=len)[0]

Demo:

>>> for x in [2, 4, 6, 8, 10, 12, 14, 2091], [1, 3, 5, 7, 9, 11, 13, 4720]:
        print(find_outlier(x))

2091
4720


来源:https://stackoverflow.com/questions/39089459/finding-the-parity-outlier-in-a-list-of-odd-even-integers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!