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学习笔记16-装饰器

    装饰器(函数) 1.函数作用域 2.高阶函数 把函数作为参数传入,这样的函数称为高阶函数,函数式编程就是指这种高度抽象的编程范式. 3.闭包  闭包就是能够读取其他函数内部变量的函数. 在本质上,闭包 ...

  2. java里面main函数为什么要用static修饰

    这学期刚开java,因为之前只写过C++和Python没接触过java,有些写法挺不习惯的,今天写完一个程序,run的时候发现提示the selection can't be launched.... ...

  3. 无图形界面安装CentOS

    有些插在ATCA中的x86刀片虽然是提供了Micro HDMI显示接口的,但是可能由于厂家出于节省成本的考量,没有给板卡配备显卡,那么在无图形界面下安装系统,就成为一个运维人员应知的一件事情.这里我们 ...

  4. String.format(String format,Object... args)的用法

    String.format(String format, Object... args)方法详解 以前也看到过很多次这个用法,一直记不牢靠,今天整理一下.   我仅仅举几个例子稍做说明: String ...

  5. TFS2018环境搭建一单实例安装(适用于小型团队)

    1.服务器配置 阿里云 单核CPU,2GB的RAM,SSD硬盘,安装TFS实例 TFS2018要求SQL Server 2016 (minimum SP1)以上.其要求有以下几点: (1).安装SQL ...

  6. logstash安装与logstash-input-jdbc插件使用

    ElasticSearch的索引可以手动添加索引的,就是类似下面这样添加的 PUT /movies/movie/1 { "title": "The Godfather&q ...

  7. Android学习总结——INSTALL_FAILED_CONFLICTING_PROVIDER

    在写个小demo的时候出现了这个问题: 排除手机内存不足.以及没用安装过这个应用的问题之后,发现是android:authorities="..."出了问题,可能还有其他应用程序和 ...

  8. Python -- 多媒体编程 -- 音乐播放

    使用win32库的WMPlayer.OCX开发一个简易的音乐播放器 import sys from PyQt4 import QtGui, QtCore from win32com.client im ...

  9. 使用node和express+mongodb实现数据增删改功能

    2018即将过去,2019即将来临,前端技术不断在在更新,学的东西越来越多.我们只有不断的学习,才不能被淘汰.在前后端分离的一个时代,后端提供接口,前端调用接口,逻辑判断,每个都是独立的工作.如果自己 ...

  10. maven - 使用nexus 搭建maven私服

    1, java环境 [wenbronk@localhost nexus]$ java -version java version "1.8.0_121" Java(TM) SE R ...