Sum of digits in a string

前端 未结 8 2033
天涯浪人
天涯浪人 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:23

    Just a variation to @oscar's answer, if we need the sum to be single digit,

    def sum_digits(digit):
        s = sum(int(x) for x in str(digit) if x.isdigit())
        if len(str(s)) > 1:
            return sum_digits(s)
        else:
            return s
    

提交回复
热议问题