Code Signal_练习题_digitDegree
Posted yd2018
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Code Signal_练习题_digitDegree相关的知识,希望对你有一定的参考价值。
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 bedigitDegree(n) = 0
; - For
n = 100
, the output should bedigitDegree(n) = 1
.1 + 0 + 0 = 1
. - For
n = 91
, the output should bedigitDegree(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
以上是关于Code Signal_练习题_digitDegree的主要内容,如果未能解决你的问题,请参考以下文章
Code Signal_练习题_stringsRearrangement