Sum of digits in a string

前端 未结 8 2049
天涯浪人
天涯浪人 2020-12-06 12:35

if i just read my sum_digits function here, it makes sense in my head but it seems to be producing wrong results. Any tip?

def is_a_digit(s):
\         


        
8条回答
  •  悲哀的现实
    2020-12-06 13:24

    Another way of using built in functions, is using the reduce function:

    >>> numeric = lambda x: int(x) if x.isdigit() else 0
    >>> reduce(lambda x, y: x + numeric(y), 'hihello153john', 0)
    9
    

提交回复
热议问题