python中的加法和乘法持久性
我的代码有点问题。我必须找到一个数的加法和乘法持久性。到目前为止,我只能找到一次持久性,但它必须不断循环,以使答案小于9。 结果如下:python中的加法和乘法持久性,python,math,persistence,Python,Math,Persistence,我的代码有点问题。我必须找到一个数的加法和乘法持久性。到目前为止,我只能找到一次持久性,但它必须不断循环,以使答案小于9。 结果如下: type a number thats greater than 9: 1234 additive persistence result: 10 multiplicative persistence result: 240 Press enter to exit 然而,这是错误的,因为它应该分解为10和240。它应该做1+0=1和2*4*0=0。我知道我可
type a number thats greater than 9: 1234
additive persistence result: 10
multiplicative persistence result: 240
Press enter to exit
然而,这是错误的,因为它应该分解为10和240。它应该做1+0=1和2*4*0=0。我知道我可能需要一个循环来完成这个,只是我不知道怎么做。这是给我的CS课的,甚至我的老师都不知道怎么做。
这是我的密码:
a=raw_input("type a number thats greater than 9: ")
sum_1=0
for element in a:
sum_1+=int(element)
print "additive persistence result: ",sum_1
for element in a:
sum_1*=int(element)
print "multiplicative persistence result: ",sum_1
print"Press enter to exit"
raw_input()
我会让你从第一个开始:
while len(a) > 1:
sum_1 = 0
for element in a:
sum_1+=int(element)
a = str(sum_1)
我会让你从第一个开始:
while len(a) > 1:
sum_1 = 0
for element in a:
sum_1+=int(element)
a = str(sum_1)
这是
reduce()
函数的一个很好的用例:
def persistence(num, op):
while True:
digits = list(map(int, str(num)))
if len(digits) == 1:
return digits[0]
num = reduce(op, digits)
您可以使用op
作为operator.add
或operator.mul来调用它:
>>> persistence(1234, operator.add)
1
>>> persistence(1234, operator.mul)
8
这是reduce()
函数的一个很好的用例:
def persistence(num, op):
while True:
digits = list(map(int, str(num)))
if len(digits) == 1:
return digits[0]
num = reduce(op, digits)
您可以使用op
作为operator.add
或operator.mul来调用它:
>>> persistence(1234, operator.add)
1
>>> persistence(1234, operator.mul)
8
试试下面这些代码怎么样:
#Multiplicative Persistence
from functools import reduce # omit on Python 2
import operator
#Q_num is used for asking the input of the number
Q_num = int(input('What is the input number: '))
#D_num is used to seperated the number from Q_num and put them in the list
D_num = ([int(A_num)for A_num in str(Q_num)])
print(D_num)
#Multiply is used to convert function to D_num(numbers that already being seperated)
Multiply = reduce(operator.__mul__, D_num)
print(Multiply)
#D_mulF is used to seperated the number from Multiply and put them in the list
D_mulF = ([int(B_num)for B_num in str(Multiply)])
print(D_mulF)
#Multiplier is used to convert function to D_mulF(numbers that already being seperated)
Multiplier = reduce(operator.__mul__, D_mulF)
print(Multiplier)
#Additive Persistence
#Q_num is used for asking the input of the number
Q_num = int(input('What is the input number: '))
#D_num is used to seperated the number from Q_num and put them in the list
D_num = ([int(A_num)for A_num in str(Q_num)])
print(D_num)
#D_sum is used to convert the sum() function to D_num(numbers that already being seperated)
D_sum = (sum(D_num)) # Add all of the number in the list
print(D_sum)
#Answer is used to seperated the number from D_sum and put them in the list
Answer = ([int(A_num2)for A_num2 in str(D_sum)])
print(Answer)
#F_ans is used to convert function to Answer(numbers that already being seperated)
F_ans = (sum(Answer))
print(F_ans)
试试下面这些代码怎么样:
#Multiplicative Persistence
from functools import reduce # omit on Python 2
import operator
#Q_num is used for asking the input of the number
Q_num = int(input('What is the input number: '))
#D_num is used to seperated the number from Q_num and put them in the list
D_num = ([int(A_num)for A_num in str(Q_num)])
print(D_num)
#Multiply is used to convert function to D_num(numbers that already being seperated)
Multiply = reduce(operator.__mul__, D_num)
print(Multiply)
#D_mulF is used to seperated the number from Multiply and put them in the list
D_mulF = ([int(B_num)for B_num in str(Multiply)])
print(D_mulF)
#Multiplier is used to convert function to D_mulF(numbers that already being seperated)
Multiplier = reduce(operator.__mul__, D_mulF)
print(Multiplier)
#Additive Persistence
#Q_num is used for asking the input of the number
Q_num = int(input('What is the input number: '))
#D_num is used to seperated the number from Q_num and put them in the list
D_num = ([int(A_num)for A_num in str(Q_num)])
print(D_num)
#D_sum is used to convert the sum() function to D_num(numbers that already being seperated)
D_sum = (sum(D_num)) # Add all of the number in the list
print(D_sum)
#Answer is used to seperated the number from D_sum and put them in the list
Answer = ([int(A_num2)for A_num2 in str(D_sum)])
print(Answer)
#F_ans is used to convert function to Answer(numbers that already being seperated)
F_ans = (sum(Answer))
print(F_ans)