Code Signal_练习题_digitDegree

时间:2022-08-31 08:02:43

Let's define digit degree of some positive integer as the number of times we need to replace this number with the sum of its digits until we get to a one digit number.

Given an integer, find its digit degree.

Example

    • For n = 5, the output should be
      digitDegree(n) = 0;
    • For n = 100, the output should be
      digitDegree(n) = 1.
      1 + 0 + 0 = 1.
    • For n = 91, the output should be
      digitDegree(n) = 2.
      9 + 1 = 10 -> 1 + 0 = 1.

我的解答:

def digitDegree(n):
s = 0
count = 1
if len(str(n)) == 1:
return 0
for i in str(n):
s += int(i)
if len(str(s)) == 1:
return 1
while s >= 10:
c = 0
for i in str(s):
c = c + int(i)
s = c
return count + 1
def digitDegree(n):
d=0
while n>=10:
n=sum([int(i) for i in str(n)])
d+=1
return d

膜拜大佬