【每日一题20220809】神秘代码

:mage:‍ 在一座古城的废墟中,一群考古学家发现了一个神秘的功能,其中有很多洞,称为getNum(n)。他们试图用一些论据来称呼它。最后他们得到了这本日记:

getNum(300) #-> returns 2
getNum(90783) #-> returns 4
getNum(123321) #-> returns 0
getNum(89282350306) #-> returns 8
getNum(3479283469) #-> returns 5

考古学家完全被这一挑战所困。他们都绝望了,但后来……你是超级棒的程序员。你能理解这个函数的奥秘并重写它吗?

题目难度:简单
题目来源:https://www.codewars.com/kata/55217af7ecb43366f8000f76

def get_num(n):
    # your code
    return 

assert get_num(300) == 2
assert get_num(90783) == 4
assert get_num(123321) == 0
assert get_num(89282350306) == 8
assert get_num(3479283469) == 5
def get_num(n):
    sum=0
    for i in str(n):
        #0,6,9中都包含1个0,sum+1
        if i in ('0','6','9'):
            sum+=1
        #8中包含2个0,sum+2
        elif i=='8':
            sum+=2
        #其他数字中不包含0,sum+0
        else:
            sum+=0
    return sum

assert get_num(300) == 2
assert get_num(90783) == 4
assert get_num(123321) == 0
assert get_num(89282350306) == 8
assert get_num(3479283469) == 5
def get_num(n):
    nums = [1, 0, 0, 0, 0, 0, 1, 0, 2, 1]
    total = 0
    for i in str(n):
        total += nums[int(i)]
    return total
2 个赞
def get_num(n):
    # your code
    result = 0
    for i in str(n):
        if i in ['0', '6', '9']:
            result += 1
        elif i == '8':
            result += 2
    return result


assert get_num(300) == 2
assert get_num(90783) == 4
assert get_num(123321) == 0
assert get_num(89282350306) == 8
assert get_num(3479283469) == 5
def get_num(n):
    return sum({'0': 1, '6': 1, '8': 2, '9': 1}.get(d, 0) for d in str(n))
def get_num(n):
    num_list=[1,0,0,0,0,0,1,0,2,1]
    s=0
    for i in str(n):
        s+=num_list[int(i)]
    return s

assert get_num(300) == 2
assert get_num(90783) == 4
assert get_num(123321) == 0
assert get_num(89282350306) == 8
assert get_num(3479283469) == 5