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. 【spring cloud】服务启动后正常,但是无法上线,一直处于down状态

    spring cloud eureka 如果出现某个应用实例 down(1), 说明 spring admin 健康检测没有通过导致 eureka 注册中心不会把这个实例从列表中删除掉. 这样所有使用 ...

  2. nginx-https错误

    连接 ssl.acfun.tv 时发生错误. SSL 接收到一个超出最大准许长度的记录. (错误码: ssl_error_rx_record_too_long) 最后发现,是因为nginx里的配置包含 ...

  3. 【ElasticSearch】:Mapping相关

    Mapping 类似数据库中的表结构定义,主要作用如下: 定义Index下的字段名(Field Name). 定义字段类型,例如数值型.字符串型.布尔型等. 定义倒排索引相关配置,比如是否索引.记录p ...

  4. karma 启动提示PhantomJS not found on PATH

    Karma 介绍:是由AngularJS团队开发的测试执行过程管理实用工具,帮助开发人员在不同的浏览器中执行测试. 一般搭配PhantomJS作为浏览器启动器.PhantomJS是一个非主流的Webk ...

  5. 监控 Redis 服务方案

    RedisLive easy_install pip wget https://bootstrap.pypa.io/get-pip.py --no-check-certificate python g ...

  6. java字节码文件

    查看字节码文件: javap  -verbose  HellloWorld.class

  7. 导出txt文件

    <?php Header( "Content-type: application/octet-stream "); Header( "Accept-Ranges: ...

  8. Vue笔记:在项目中使用 SCSS

    背景概述 1. CSS预处理器 css预处理器定义了一种新的编程语言,编译后成正常的CSS文件.为CSS增加一些编程的特性,无需考虑浏览器的兼容问题,让CSS更加简洁,适应性更强,可读性更佳,更易于代 ...

  9. psql工具使用(二)

    所有psql命令都以   \   开头 一.使用psql -l查看有哪些数据库: -bash-4.2$ psql -l List of databases Name | Owner | Encodin ...

  10. linux下更改时区

    起因: 装系统时一走神把时区选错了,导致时间不正确,但是又不想重装,所以找了一下解决方法. 解决方案: 我的环境时这样的,其他的环境没试过. [root@werserver01 ~]# cat /et ...