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. Python做web开发,推荐几个能立马上手的小项目

    Python这门优美的语言是非常适合web开发的,基于Python的Django框架简单便捷且很强大. 那么作为新手该如何上手这门语言?一切不敲代码的学编程手段都是扯淡,今天就推荐一些适合新手练手的P ...

  2. .gitignore总结

    git进行管理时,.gitignore是必不可少的,可以指定不需要提交到仓库的资源.最好在git init之后就创建 .gitignore文件,这是个好习惯,常用的配置及说明如下:

  3. Django 模板相关

    Django 模板相关 视图函数只是直接返回文本,而在实际生产环境中其实很少这样用,因为实际的页面大多是带有样式的HTML代码,这可以让浏览器渲染出非常漂亮的页面.目前市面上有非常多的模板系统,其中最 ...

  4. redmine设置user projects时无法delete的处理方法

    对于user,当要在管理员界面处理其projects权限时,发现部分项目只有edit按钮,而部分项目还有一个delete按钮. “delete”,直接点击按钮即可删除对应project权限,表明该pr ...

  5. Referrer Policy 介绍

    发布于 署名 4.0 国际 (CC BY 4.0) 原文链接:https://caixw.io/posts/2017/referrer-policy.html 当用户在浏览器上点击一个链接时,会产生一 ...

  6. sql查询其他服务器数据库表

    exec sp_addlinkedserver 'abc', '', 'SQLOLEDB', '192.168.49.34' exec sp_addlinkedsrvlogin ' go --查询 s ...

  7. 解决Oracle死锁问题,及产生的原因

    文章来源:http://www.cnblogs.com/leijh/archive/2012/10/15/2724165.html 最近老是发现应该执行操作数据库的代码时发现执行不了,查了一下发现是数 ...

  8. TensorFlow object detection API应用

    前一篇讲述了TensorFlow object detection API的安装与配置,现在我们尝试用这个API搭建自己的目标检测模型. 一.准备数据集 本篇旨在人脸识别,在百度图片上下载了120张张 ...

  9. slf4j 作用及logback概述

    为什么要使用slf4j 现实场景: 我们自己的系统中使用了logback这个日志系统 我们的系统使用了A.jar,A.jar中使用的日志系统为log4j 我们的系统又使用了B.jar,B.jar中使用 ...

  10. mongorestore 一次踩雷

    1.在做mongodb备份后,研发突然有个需求说先看一下昨天备份里面的数据,进行一下核实.因为那部分数据今天已经删除,由于使用---gzip.--archive做的备份,所以必须导入到同名的数据库里面 ...