【每日一题20220301】奇数偶数

:woman_mage: 给定一个数字N,请编写一个函数,将他们伪装成两个数字,分别用NENO表示,返回结果是元组(NE, NO)。其中NE表示筛选出的所有偶数,而NO则表示筛选出来的所有奇数。如果筛选结果为空则默认填充为0

示例:
输入:126453,输出:(264, 153)
输入:4628,输出:(4628, 0)

题目难度:简单
题目来源:CodeWars: Even and Odd !

def solution(N: int) -> (int, int):
    # your code here

assert solution(126453) == (264, 153)
assert solution(3012) == (2, 31)
assert solution(4628) == (4628, 0)
def solution(N: int) -> (int, int):
    N1, N2 = [], []
    for i in str(N):
        if int(i) & 1 == 0:
            N1.append(i)
        else:
            N2.append(i)
    if not N2: N2 = ['0']
    if not N1: N1 = ['0']
    dict1 = (int(''.join(i for i in N1)), int(''.join(i for i in N2)))
    return dict1
1 Like
def solution(N: int) -> (int, int):
    odd_l = []
    even_l = []
    for i in str(N):
        if int(i) % 2 ==0:
            even_l.append(i)
        else:
            odd_l.append(i)
    if len(odd_l) == 0 : odd_l = ['0']
    if len(even_l) == 0: even_l = ['0']
    odd_n = "".join(odd_l)
    even_n = "".join(even_l)
    return int(even_n),int(odd_n)
def solution(N: int) -> (int, int):
    NE = []
    NO = []
    [NE.append(i) if int(i)%2 == 0 else NO.append(i) for i in str(N)]
    NE.append("0") if NE==[] else NE
    NO.append("0") if NO==[] else NO
    return tuple((int("".join(NE)),int("".join(NO))))

assert solution(126453) == (264, 153)
assert solution(3012) == (2, 31)
assert solution(4628) == (4628, 0)
def solution(N: int) -> (int, int):
    num_a = ""
    num_b = ""
    for i in str(N):
        if int(i) % 2 == 0 and int(i) != 0:
            num_a = num_a + str(i)
        elif int(i) % 2 == 1:
            num_b = num_b + str(i)
        else:
            continue

    if len(num_a) == 0:
        num_a = "0"
    if len(num_b) == 0:
        num_b = "0"

    return (int(num_a), int(num_b))

assert solution(126453) == (264, 153)
assert solution(3012) == (2, 31)
assert solution(4628) == (4628, 0)
def solution(N: int) -> (int, int):
    s = str(N)
    NE = []
    NO = []
    for i in s:
        if int(i) % 2 == 0:
            NE.append(i)
        else:
            NO.append(i)
    if len(NE) == 0:
        NE.append('0')
    if len(NO) == 0:
        NO.append('0')
    return (int(''.join(NE)), int(''.join(NO)))


assert solution(126453) == (264, 153)
assert solution(3012) == (2, 31)
assert solution(4628) == (4628, 0)
    def solution(N: int) -> (int, int):
        a = 0
        b = 0
        for x in str(N):
            if int(x) % 2 == 0:
                a = a * 10 + int(x)
            else:
                b = b * 10 + int(x)
        return int(a),int(b)
    assert solution(126453) == (264, 153)
    assert solution(3012) == (2, 31)
    assert solution(4628) == (4628, 0)
1 Like
def solution(n: int) -> (int, int):
    # your code here
    ne = ""
    no = ""
    n_str = str(n)
    for i in n_str:
        if int(i) % 2 == 0:
            ne += ne.join(i)
        else:
            no += no.join(i)
    if ne == "" and no != "":
        return 0, int(no)
    elif ne != "" and no == "":
        return int(ne), 0
    else:
        return int(ne), int(no)


assert solution(126453) == (264, 153)
assert solution(3012) == (2, 31)
assert solution(4628) == (4628, 0)
def solution(N: int) -> (int, int):
    # 0301 奇数偶数
    NE = ''
    NO = ''
    for i in str(N):
        if int(i) % 2 == 1:
            NO += NO.join(i)
        else:
            NE += NE.join(i)
    if NE == '':
        NE = 0
    if NO == '':
        NO = 0
    print(NE, NO)
    return (int(NE), int(NO))

assert solution(126453) == (264, 153)
assert solution(3012) == (2, 31)
assert solution(4628) == (4628, 0)

def solution(N: int) -> (int, int):
    NE, NO = [], []
    for i in str(N):
        if int(i) % 2 == 0:
            NO.append(i)
        else:
            NE.append(i)
    if not NO: NO = ['0']
    if not NE: NE = ['0']
    Result = (int("".join(list(i for i in NO))), int("".join(list(i for i in NE))))
    return Result


assert solution(126453) == (264, 153)
assert solution(3012) == (2, 31)
assert solution(4628) == (4628, 0)
def solution(n: int) -> (int, int):
    ne = no = 0
    i = j = 0
    while n != 0:
        x = n % 10
        n = n // 10
        if x % 2 == 0:
            ne += x * 10 ** i
            i += 1
        else:
            no += x * 10 ** j
            j += 1
    return ne, no


assert solution(126453) == (264, 153)
assert solution(3012) == (2, 31)
assert solution(4628) == (4628, 0)
def solution(N: int) -> (int, int):
    s = []
    n = []
    for i in range(len(str(N))):
        if str(N)[i] in '2468':
            s.append(str(N)[i])
        elif str(N)[i] in '13579':
            n.append(str(N)[i])
    return int(''.join(s)) if s else 0, int(''.join(n)) if n else 0


assert solution(126453) == (264, 153)
assert solution(3012) == (2, 31)
assert solution(4628) == (4628, 0)
def solution(N: int) -> (int, int):
    # your code here
    NE = ''
    NO = ''
    str_N = str(N)
    for i in str_N:
        if int(i)%2 == 0 :
            NE = NE+i
        elif int(i)%2 == 1:
            NO = NO+i
    if NE == '':
        NE = 0
    if NO == '':
        NO = 0
    return (int(NE),int(NO))
assert solution(126453) == (264, 153)
assert solution(3012) == (2, 31)
assert solution(4628) == (4628, 0)
def solution(N: int) -> (int, int):
     even_num = 0
     odd_num = 0
    for i in str(N):
        if int(i) % 2 == 0:
            even_num = even_num * 10 + int(i)
        else:
            odd_num = odd_num * 10 + int(i)
    return even_num,odd_num
方法二、
def solution(N: int) -> (int, int):
    even_num = []
    odd_num = []
    for i in str(N):
        if int(i) % 2 == 0:
            even_num.append(i)
        else:
            odd_num.append(i)
    if len(even_num) == 0: even_num='0'
    if len(odd_num) == 0: odd_num = '0'
    return (int(''.join(even_num)), int(''.join(odd_num)))
def solution(N: int) -> (int, int):
    NE=[]
    NO=[]
    for i in range(len(str(N))):
        if int(str(N)[i])%2==0:
            NE.append(str(N)[i])
        else:
            NO.append(str(N)[i])
    if not NE:
        NE.append('0')
    if not NO:
        NO.append('0')
    return (int(''.join(NE)),int(''.join(NO)))


assert solution(126453) == (264, 153)
assert solution(3012) == (2, 31)
assert solution(4628) == (4628, 0)