Instructions

In this kata, you must create a digital root function.

A digital root is the recursive sum of all the digits in a number. Given n, take the sum of the digits of n. If that value has more than one digit, continue reducing in this way until a single-digit number is produced. This is only applicable to the natural numbers.

Here's how it works:

digital_root(16)
=> 1 + 6
=> 7 digital_root(942)
=> 9 + 4 + 2
=> 15 ...
=> 1 + 5
=> 6 digital_root(132189)
=> 1 + 3 + 2 + 1 + 8 + 9
=> 24 ...
=> 2 + 4
=> 6 digital_root(493193)
=> 4 + 9 + 3 + 1 + 9 + 3
=> 29 ...
=> 2 + 9
=> 11 ...
=> 1 + 1
=> 2

My solution:

def digital_root(n):
lst = [int(x) for x in str(n)]
result = sum(lst)
if result < 10:
return result
else:
return digital_root(result)

Best solution:

def digital_root(n):
return n if n < 10 else digital_root(sum(map(int,str(n))))

[codewars_python]Sum of Digits / Digital Root的更多相关文章

  1. Sum of Digits / Digital Root

    Sum of Digits / Digital Root In this kata, you must create a digital root function. A digital root i ...

  2. digital root问题

    问题阐述会是这样的: Given a non-negative integer num, repeatedly add all its digits until the result has only ...

  3. 快速切题 sgu118. Digital Root 秦九韶公式

    118. Digital Root time limit per test: 0.25 sec. memory limit per test: 4096 KB Let f(n) be a sum of ...

  4. Codeforces Beta Round #10 C. Digital Root 数学

    C. Digital Root 题目连接: http://www.codeforces.com/contest/10/problem/C Description Not long ago Billy ...

  5. Digital Root 的推导

    背景 在LeetCode上遇到这道题:Add Digits 大意是给一个数,把它各位数字相加得到一个数,如果这个数小于10就返回,不然继续 addDigits(这个相加得到的数). 题目很简单,但是如 ...

  6. codeforces 10C Digital Root(非原创)

    Not long ago Billy came across such a problem, where there were given three natural numbers A, B and ...

  7. 数字根(digital root)

    来源:LeetCode 258  Add Dights Question:Given a non-negative integer  num , repeatedly add all its digi ...

  8. 【HDOJ】4351 Digital root

    digital root = n==0 ? 0 : n%9==0 ? 9:n%9;可以简单证明一下n = a0*n^0 + a1*n^1 + ... + ak * n^kn%9 = a0+a1+..+ ...

  9. 树根 Digital root

    数根 (又称数字根Digital root)是自然数的一种性质.换句话说.每一个自然数都有一个数根.数根是将一正整数的各个位数相加(即横向相加),若加完后的值大于等于10的话,则继续将各位数进行横向相 ...

随机推荐

  1. Android中处理Touch Icon的方案

    苹果的Touch Icon相对我们都比较熟悉,是苹果为了支持网络应用(或者说网页)添加到桌面需要的图标,有了这些Touch Icon的网页链接更加和Native应用更相像了.由于苹果设备IPod,IP ...

  2. google在线測试练习题3

    Problem The Latin alphabet contains 26 characters and telephones only have ten digits on the keypad. ...

  3. Android学习笔记(9):使用XML文件和Java代码控制UI界面

    Android推荐使用XML文件设置UI界面.然后用Java代码控制逻辑部分,这体现了MVC思想. MVC全名是Model View Controller.是模型(model)-视图(view)-控制 ...

  4. Codeforces 559B Equivalent Strings 等价串

    题意:给定两个等长串a,b.推断是否等价.等价的含义为:若长度为奇数,则必须是同样串.若长度是偶数,则将两串都均分成长度为原串一半的两个子串al,ar和bl,br,当中al和bl等价且ar和br等价, ...

  5. Configuration file schema for the .NET Framework

    https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/index Runtime Settings ...

  6. 日期格式,Popup的使用方法,RenderTransform与LayoutTransform的区别

    1.画个笑脸给大家娱乐一下: <Canvas Width="200" Height="180" VerticalAlignment="Cente ...

  7. Python: PS 滤镜--碎片特效

    本文用 Python 实现 PS 滤镜中的碎片特效,这个特效简单来说就是将图像在 上,下,左,右 四个方向做平移,然后将四个方向的平移的图像叠加起来做平均.具体的效果图可以参考之前的博客 http:/ ...

  8. OWIN是Open Web Server Interface for .NET

    http://owin.org/ Servers and Hosts Katana Nowin Suave Frameworks Jasper Nancy SignalR WebApi WebShar ...

  9. HDU 4786Fibonacci Tree(最小生成树)

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  10. PostgreSQL Replication之第七章 理解Linux高可用(3)

    7.3 高可用软件的历史 有大量的专有的和开源的高可用性软件.专有的例子有:Solaris Cluster (有时称为Sun 集群 or SunCluster), SteelEye LifeKeepe ...