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

膜拜大佬

Code Signal_练习题_digitDegree的更多相关文章

  1. Code Signal_练习题_Knapsack Light

    You found two items in a treasure chest! The first item weighs weight1 and is worth value1, and the ...

  2. Code Signal_练习题_growingPlant

    Each day a plant is growing by upSpeed meters. Each night that plant's height decreases by downSpeed ...

  3. Code Signal_练习题_arrayMaxConsecutiveSum

    Given array of integers, find the maximal possible sum of some of its k consecutive elements. Exampl ...

  4. Code Signal_练习题_differentSymbolsNaive

    Given a string, find the number of different characters in it. Example For s = "cabca", th ...

  5. Code Signal_练习题_firstDigit

    Find the leftmost digit that occurs in a given string. Example For inputString = "var_1__Int&qu ...

  6. Code Signal_练习题_extractEachKth

    Given array of integers, remove each kth element from it. Example For inputArray = [1, 2, 3, 4, 5, 6 ...

  7. Code Signal_练习题_stringsRearrangement

    Given an array of equal-length strings, check if it is possible to rearrange the strings in such a w ...

  8. Code Signal_练习题_absoluteValuesSumMinimization

    Given a sorted array of integers a, find an integer x from a such that the value of abs(a[0] - x) + ...

  9. Code Signal_练习题_depositProfit

    You have deposited a specific amount of money into your bank account. Each year your balance increas ...

随机推荐

  1. 03_python_基本数据类型

    一.基本数据类型 整数 bool 字符串: 可以保存少量数据并进行相应的操作 列表 list: 存大量数据 [] 元组 tuple: 不可改变的() 字典 dict: 保存键值对,一样可以存储大量的数 ...

  2. git使用分支与tag

    查看分支:git branch 创建分支:git branch <name> 切换分支:git checkout <name> 创建+切换分支:git checkout -b ...

  3. mongodb postgresql mysql jsonb对比

    mongodb pg mysql jsonb对比 http://erthalion.info/2017/12/21/advanced-json-benchmarks/ 使用禁用jsonb列的压缩 AL ...

  4. 实验1 C语言开发环境使用和数据类型、运算符、表达式

    ♦ 实验结论 PART 1 验证性内容 问题: 1.结尾没有加“:”时回车到下一行的时候再输入下一行的语言首字对齐方式会发生变化,可以对上一行进行检查. (这一点需要在不同软件里面试一下,在机房里的软 ...

  5. cookie、session的区别

    相信你肯定经常听说cookie和Session,那你有没有好好了解这两个的区别呢?其实,不整理之前,我也是一脸懵. 为什么需要cookie和session呢?---因为Http是无状态的,web开发中 ...

  6. 剑指offer十七姊妹篇之二叉树的创建、遍历、判断子二叉树

    1.二叉树节点类 public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public Tr ...

  7. Java生成某段时间内的随机时间

    上代码: import java.text.SimpleDateFormat; import java.util.Date; public class DateUtil { /** * 生成随机时间 ...

  8. android开发学习——day2

    简单了解了android stdio的操作方式,今天着手于探究活动(Activity) 了解了基本活动与手动创建活动的方法,了解了onCreate()方法,了解了创建和加载页面布局(layout) 新 ...

  9. Kafka消息存储原理

    kafka消息存储机制 (一)关键术语 复习一下几个基本概念,详见上面的基础知识文章. Broker:消息中间件处理结点,一个Kafka节点就是一个broker,多个broker能够组成一个Kafka ...

  10. Java-Reflection反射-获取包括父类在内的所有字段

    前言 今天Android移动端要加个新功能,所以回归Android程序员的身份.开发的过程中,发现了之前的代码写的有很多问题,真的应该把时间抽出来重构一下了. 其中有反射的一个坑,工具类某方法反射获取 ...