Home > Enterprise >  The code is for returning the outlying even or odd number, everything works except for my numbers ar
The code is for returning the outlying even or odd number, everything works except for my numbers ar

Time:08-30

example of a problem would be [2,4,6,8,9] the code should return 9, instead it returns [9]

def find_outlier(integers):
    even, odd = 0, 0
    outlier = []

    for num in integers:
        if num % 2 == 0:
            even  = 1
        else:
            odd  =1
    if even > odd:
        for num in integers:
            if num % 2 != 0:
                outlier.append(num)
                return outlier
    else:
        if odd > even:
            for num in integers:
                if num % 2 == 0:
                    outlier.append(num)
                    return outlier

CodePudding user response:

You are returning the list and printing the list. That's the reason you can returning the num instead of the outlier list

def find_outlier(integers):
    even, odd = 0, 0
    outlier = []

    for num in integers:
        if num % 2 == 0:
            even  = 1
        else:
            odd  =1
    if even > odd:
        for num in integers:
            if num % 2 != 0:
                outlier.append(num)
                return num
    else:
        if odd > even:
            for num in integers:
                if num % 2 == 0:
                    outlier.append(num)
                    return num
print(find_outlier([2,4,6,8,9]))

But still your code is fully mess. It's not gonna return more than one odd or even. I exactly don't know what you wanted to mean by this code. But this shortened code may help

def CheckEvenOrOdd(integers):
    even, odd= 0,0
    Evenlist=[]
    OddList=[]
    for num in integers:
        if num % 2 == 0:
            Evenlist.append(num)
            even  = 1
        else:
            OddList.append(num)
            odd  =1
    if even > odd:
        return OddList
    else:
        return Evenlist

print(CheckEvenOrOdd([2,4,6,8,9,11]))

It's gonna return list so there is no problem with that

CodePudding user response:

Disclaimer - it's not a direct answer to OP's question, rather to expand it and make it more scalable to include multiple outliers by using Pythonic function from more_itertools.

It uses more_itertools partition. It returns a 2-tuple of iterables derived from the input iterable. The first yields the items that have pred(item) == False. The second yields the items that have pred(item) == True.

from more_itertools import partition   

def find_outliers(iterable):
    ''' find one or more outlier numbers in the iterable list'''
    is_odd = lambda x: x & 1
    odds, evens = partition(is_odd, iterable)

    #  can do the comparison of len(list(odds)) vs. len(list(evens)) ...
    #  ....leave as your exercise..... since you already did it. 

    return list(odds), list(evens)

>>> L = [1, 2, 3, 4, 5, 6, 7, 9]
>>> find_outliers(L)
# ([2, 4, 6], [1, 3, 5, 7, 9])
>>> find_outliers(nums)
# ([2, 4, 6, 8], [9])
  • Related