[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的话,则继续将各位数进行横向相 ...
随机推荐
- [React] React.PureComponent
React.PureComponent is similar to React.Component. The difference between them is that React.Compone ...
- iOS 9 适配,我咋还没遇到这么多坑呢呀
iOS 9 适配,我咋还没遇到这么多坑呢呀 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 ...
- 13.MongoDB 连接命令格式
转自:https://www.linuxidc.com/Linux/2016-03/129456.htm 使用用户 admin 使用密码 123456 连接到本地的 MongoDB 服务上.输出结果如 ...
- Python TurtleWorld configuration and simple test
TurtleWorld provides a set of functions for drawing lines by steering turtles around the screen. You ...
- BZOJ 3130 二分+网络流
思路: 不被题目忽悠就是思路 二分一下max 判一下等不等于最大流 搞定 7 9 1 1 2 3 1 3 3 2 3 3 3 4 2 3 5 2 3 6 1 4 7 2 5 7 2 6 7 2 这里有 ...
- POJ 3173 模拟
按照题意模拟就好-- //By SiriusRen #include <cstdio> #include <algorithm> using namespace std; in ...
- webpack入门与笔记
为什要使用WebPack 现今的很多网页其实可以看做是功能丰富的应用,它们拥有着复杂的JavaScript代码和一大堆依赖包.为了简化开发的复杂度,前端社区涌现出了很多好的实践方法 模块化,让我们可以 ...
- AOC 电视机T3212M 进入 工厂模式方法,修改开机启动方式
原启动方式: 通电,再按遥控 器上 “开机” 希望改成: 通电直接打开电视 方法: 1. 按遥控器上的 menu 1147 进入 工厂模式 2. 选择 7 General Settin ...
- vue 父子组件传值:props和$emit
<!--子组件页面--> <template> <div class="hello"> <!-- 添加一个input输入框 添加keypr ...
- Nginx的编译与安装
nginx.org 下载最新版本[选择 stable 稳定版]. 安装步骤: 1.cd /usr/local/src/ 2.wget http://nginx.org/download/nginx-1 ...