[codewars_python]Sum of Digits / Digital Root
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的更多相关文章
- Sum of Digits / Digital Root
Sum of Digits / Digital Root In this kata, you must create a digital root function. A digital root i ...
- digital root问题
问题阐述会是这样的: Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- 快速切题 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 ...
- Codeforces Beta Round #10 C. Digital Root 数学
C. Digital Root 题目连接: http://www.codeforces.com/contest/10/problem/C Description Not long ago Billy ...
- Digital Root 的推导
背景 在LeetCode上遇到这道题:Add Digits 大意是给一个数,把它各位数字相加得到一个数,如果这个数小于10就返回,不然继续 addDigits(这个相加得到的数). 题目很简单,但是如 ...
- codeforces 10C Digital Root(非原创)
Not long ago Billy came across such a problem, where there were given three natural numbers A, B and ...
- 数字根(digital root)
来源:LeetCode 258 Add Dights Question:Given a non-negative integer num , repeatedly add all its digi ...
- 【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+..+ ...
- 树根 Digital root
数根 (又称数字根Digital root)是自然数的一种性质.换句话说.每一个自然数都有一个数根.数根是将一正整数的各个位数相加(即横向相加),若加完后的值大于等于10的话,则继续将各位数进行横向相 ...
随机推荐
- less03 混合
less //基本混合 .font_hn{ color: red; font-family: microsoft yahei, "黑体", Arial, Simsun, " ...
- 青蛙的约会 poj 1061
青蛙的约会 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 86640 Accepted: 15232 Descripti ...
- jquery easyui a标记方法传值问题
今天开发一个功能,其操作的按钮式是一个点击的a标记,并且是动态生成的,其点击方法需要传参数具体代码如下 {field:'time',title:'日程时间',width:200,align:'cent ...
- springMVC接受前台传值
今天,用ajax向springMVC的控制器传参数,是一个json对象.({"test":"test","test1":"test ...
- c# IndexOf()用法
IndexOf()用法 查找字符串中字符或者字符串首次出现的位置,返回的是索引值; str1.indexOf('字');//查找“字”在字符串中首次出现的索引值 str1.indexOf(" ...
- exsi从磁盘中加载虚拟机
e
- 什么是Monad?
为了理解什么是Monad,最好需要了解什么是Monoid.这两篇互为姐妹篇,因为Monad的定义是:A monad is just a monoid in the category of endofu ...
- vue组件递归的一些理解
自己做个小项目练手,需要用到组件递归,网上查了一些资料,每个代码片段都认识,但是连起来,就一团浆糊. 既然人傻就多思考吧.不明白的点有以下: 1.组件怎么自己调用自己,函数的递归是就是在functio ...
- 与 JSON 相关的一些操作 (项目中用到一部分,后续逐渐完善)
1.JSON 打印 console.log(JSON.stringify(data, null, 4)); 2.JSON 格式判断 var isjson = typeof(obj) == " ...
- 记intel杯比赛中各种bug与debug【其五】:朴素贝叶斯分类器的实现和针对性的优化
咱这个项目最主要的就是这个了 贝叶斯分类器用于做可以统计概率的二元分类 典型的例子就是垃圾邮件过滤 理论基础 对于贝叶斯算法,这里附上两个链接,便于理解: 朴素贝叶斯分类器的应用-阮一峰的网络日志 基 ...