[英]recursive sum of all the digits in a number
我被困在這個練習中。
任務:
數字根是數字中所有數字的遞歸和。 給定n,取n的位數之和。 如果該值超過一位,則繼續以這種方式減少,直到產生一位數。 這僅適用於自然數。
以下是它的工作原理:
數字根(16)
1 + 6 = 7
數字根(942)
9 + 4 + 2 = 15... 1 + 5 =6
我的方法就在這里。 關於如何正確返回正確值的任何提示? 我將不勝感激任何幫助。
def digital_root(n):
answ = 0
s = 0
x = str(n)
for i in range(0, len(x)):
s = s + int(x[i])
if len(str(s)) > 1:
digital_root(s)
elif len(str(s)) == 1:
answ = s # here is an answer
return answ # answer is 2 but this one is not returning 2, why?
return answ # here it returns answ=0, I want to return 2...
print(digital_root(493193))
你對此有什么看法:
def digital_root(n):
if n<10 :
return n
return digital_root( n%10 + digital_root( n//10 ) )
主要問題是,在進行遞歸調用時,您沒有將返回值分配給任何東西,因此對於需要多次傳遞的任何值,您總是會得到 0。
此外,在遞歸調用之后,長度應為 1,因此以下elif
不是必需的,並且會導致錯誤的返回值,因為它不會將s
分配給answ
固定版本:
def digital_root(n):
answ = 0
s = 0
x = str(n)
for i in range(0, len(x)):
s = s + int(x[i])
if len(str(s)) > 1:
s = digital_root(s)
answ = s # You could even return s directly
return answ
print(digital_root(493193))
這是我的看法。 我有使用sum
的沖動,但這幾乎感覺像是在作弊,因為你可以只使用sum([int(i) for i in str(n)])
。
def digital_root(n):
# convert to a string
as_str = str(n)
# get the value of the current first digit
value = int(as_str[0])
if len(as_str) > 1:
# add the recursive return plus the value
# for anything other than our base case.
# pass the rest of the digits into our recursive call
return digital_root(int(as_str[1:])) + value
# our base case
return value
print(digital_root(493193))
你對遞歸調用的結果什么都不做——這就是你的代碼出錯的原因。
通過索引進行迭代大多不好 - 更好地直接迭代字符串。
這是使用一些內置python 函數的更簡潔的遞歸方式:
def rec_sum(n):
sn = str(n)
# base case - return the number
if len(sn)==1:
return n
# not the base case,return whatever the recursive output returns
return rec_sum(sum(map(int,sn)))
for n in range(1,71):
print(f"{n:3}=>{rec_sum(n):3}", end = "|")
if n%7 == 0:
print()
Output:
1=> 1| 2=> 2| 3=> 3| 4=> 4| 5=> 5| 6=> 6| 7=> 7|
8=> 8| 9=> 9| 10=> 1| 11=> 2| 12=> 3| 13=> 4| 14=> 5|
15=> 6| 16=> 7| 17=> 8| 18=> 9| 19=> 1| 20=> 2| 21=> 3|
22=> 4| 23=> 5| 24=> 6| 25=> 7| 26=> 8| 27=> 9| 28=> 1|
29=> 2| 30=> 3| 31=> 4| 32=> 5| 33=> 6| 34=> 7| 35=> 8|
36=> 9| 37=> 1| 38=> 2| 39=> 3| 40=> 4| 41=> 5| 42=> 6|
43=> 7| 44=> 8| 45=> 9| 46=> 1| 47=> 2| 48=> 3| 49=> 4|
50=> 5| 51=> 6| 52=> 7| 53=> 8| 54=> 9| 55=> 1| 56=> 2|
57=> 3| 58=> 4| 59=> 5| 60=> 6| 61=> 7| 62=> 8| 63=> 9|
64=> 1| 65=> 2| 66=> 3| 67=> 4| 68=> 5| 69=> 6| 70=> 7|
部分sum(map(int,sn))
表示: map(function,iterable)
將int()
-函數應用於sn
中的所有字符(字符串是可迭代的),這是您的數字的字符串。 然后它sum()
s 並用這個總和調用它自己。
對於遞歸函數,最好從最基本的情況開始,然后逐步增加復雜性。
此外,一個有用的技巧是獲取字符串的list()
將字符串切割成字符,因此list("abc")
返回["a", "b", "c"]
。
使用它,我們得到:
def digital_root(n):
# basic scenario: n is 1 digit, ergo <10.
if n < 10:
return n
# alternative case: more than 1 digit
# cut n into digits with a list comprehension
# str(714) => "714", list(str(714)) => "['7', '1', '4']
digits = [int(c) for c in list(str(n))]
# take the digital root of the sum
return digital_root(sum(digits))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.