Code Signal_练习题_evenDigitsOnly

Check if all digits of the given integer are even.

Example

    • For n = 248622, the output should be
      evenDigitsOnly(n) = true;
    • For n = 642386, the output should be
      evenDigitsOnly(n) = false.

我的解答:

def evenDigitsOnly(n):
    for i in range(len(str(n))):
        if int(str(n)[i]) % 2 != 0:
            return False
    return True
def evenDigitsOnly(n):
    return all([int(i)%2==0 for i in str(n)])

想起来用列表推导式了...但没想起来all函数....
膜拜大佬

猜你喜欢

转载自www.cnblogs.com/YD2018/p/9470081.html
今日推荐