【每日一题1011】奇偶比较

:woman_mage: 给定一个数字,请编写一个函数,判断其中组成数字中,比较所有的奇数和偶数的总和,如果奇数总和大于等于偶数总和则返回True,否则返回False。

示例:
输入:12,输出:False
输入:123,输出:True

题目难度:简单
题目来源:codewars

def odd_or_even(n: int) -> bool:
    pass

assert odd_or_even(12) is False
assert odd_or_even(123) is True
assert odd_or_even(112) is True
def odd_or_even(n: int) -> bool:
    odd_count = 0
    even_count = 0
    for i in str(n):
        if int(i) % 2 == 0:
            even_count += int(i)
        else:
            odd_count += int(i)
    return odd_count >= even_count


assert odd_or_even(12) is False
assert odd_or_even(123) is True
assert odd_or_even(112) is True
def odd_or_even(n: int) -> bool:
    odd = 0
    even = 0
    i = 0
    while n >= 1:
        j = n % 10
        if j % 2 == 1:
            odd += j
        else:
            even += j
        n = n // 10
    return odd >= even

assert odd_or_even(12) is False
assert odd_or_even(123) is True
assert odd_or_even(112) is True

def odd_or_even(n:int) ->bool:
    sum_odd = 0
    sum_even = 0
    shang = 1

    while shang:
        shang, yushu = divmod(n, 10)
        if yushu % 2 == 1:
            sum_odd += yushu
        else:
            sum_even += yushu
        n = shang

    print(sum_odd, sum_even)
    if sum_odd >= sum_even:
        return True
    else:
        return False

assert odd_or_even(12) is False
assert odd_or_even(123) is True
assert odd_or_even(112) is True
def odd_or_even(n: int) -> bool:
    # 定义奇数和变量,初始值为 0
    odd_sum = 0
    # 定义偶数和变量,初始值为 0
    even_sum = 0
    # 转化为字符串,遍历每一位字符
    for v in str(n):
        # 将单个字符再转化为数值
        num = int(v)
        # 判断是否为偶数
        # 可以被 2 整除的数则为偶数
        if num % 2 == 0:
            # 数值与偶数和进行累加
            even_sum += num
        else:
            # 数值与奇数和进行累加
            odd_sum += num
    # 返回奇数总和大于等于偶数总和的比较
    return odd_sum >= even_sum

assert odd_or_even(12) is False
assert odd_or_even(123) is True
assert odd_or_even(112) is True

@1042207232_5343 思路清晰,注释完美,直呼内行!

def odd_or_even(n: int) -> bool:
    evens = [int(i) for i in str(n) if int(i) % 2 == 0]
    odds = [int(i) for i in str(n) if int(i) % 2 != 0]
    return sum(odds) >= sum(evens)


assert odd_or_even(12) is False
assert odd_or_even(123) is True
assert odd_or_even(112) is True

思路:分别提取出奇数和偶数数字,各自组成一个列表,然后利用sum函数统计总和,最后比较大小。

1 个赞
def odd_or_even(n: int) -> bool:
    oddSum = sum([int(i) for i in list(str(n)) if int(i) % 2 == 0])
    evenSum = sum([int(i) for i in list(str(n)) if int(i) % 2 != 0])
    return True if evenSum>=oddSum else False

assert odd_or_even(12) is False
assert odd_or_even(123) is True
assert odd_or_even(112) is True
        public boolean oddOrEven(Integer integer) {
            return Arrays.stream(integer.toString().split("")).filter(i -> Integer.parseInt(i) % 2 != 0).count() >= ((integer.toString().length() / 2) + 1);
        }
    
        @Test
        public void testAddOrEven() {
            assert oddOrEven(123);
            assert oddOrEven(112);
            assert false == oddOrEven(12);
        }
def odd_or_even(n: int) -> bool:
    list1 = []
    list2 = []
    for i in str(n):
        if int(i) % 2 == 0:
            list1.append(int(i))
        else:
            list2.append(int(i))
    if sum(list2) >= sum(list1):
        return True
    else:
        return False


assert odd_or_even(12) is False
assert odd_or_even(123) is True
assert odd_or_even(112) is True
def odd_or_even(n: int) -> bool:
    x=0
    y=0
    for i in str(n):
        if int(i)%2==0:
            x+=int(i)
        else:
            y+=int(i)
    if y>=x:
        return True
    else:
        return False

assert odd_or_even(12) is False
assert odd_or_even(123) is True
assert odd_or_even(112) is True
def odd_or_even(n: int) -> bool:
    sum1 = sum([int(i) for i in str(n) if int(i) % 2 == 1])
    sum2 = sum([int(i) for i in str(n) if int(i) % 2 == 0])
    if sum1 >= sum2:
        return True
    else:
        return False


assert odd_or_even(12) is False
assert odd_or_even(123) is True
assert odd_or_even(112) is True
def odd_or_even(n: int) -> bool:
    odd_sum = sum([int(i) for i in str(n) if int(i) % 2 == 1])
    if odd_sum >= sum(int(j) for j in str(n)) - odd_sum:
        return True

    return False


assert odd_or_even(12) is False
assert odd_or_even(123) is True
assert odd_or_even(112) is True