[英]Additive and multiplicative persistence in python
i am having some trouble with my code.我的代码有问题。 I have to find the additive and multiplicative persistence of a number.
我必须找到一个数字的加法和乘法持久性。 So far I can find the persistence just one time, but it has to keep looping in order for the answer to be less than 9. Here are the results:
到目前为止,我只能找到一次持久性,但它必须不断循环才能使答案小于 9。以下是结果:
type a number thats greater than 9: 1234
additive persistence result: 10
multiplicative persistence result: 240
Press enter to exit
However, this is wrong because it should break down 10 and 240. It is supposed to do 1+0=1 and 2*4*0=0.然而,这是错误的,因为它应该分解 10 和 240。它应该做 1+0=1 和 2*4*0=0。 I know i probably need a while loop to do this, it's just that i don't know how.
我知道我可能需要一个 while 循环来做到这一点,只是我不知道如何。 This is for my CS class and even my teacher really didn't know how to do it.
这是给我的 CS 课的,连我的老师都不知道该怎么做。 here is my code:
这是我的代码:
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()
I'll get you started on the first one:我会让你开始第一个:
while len(a) > 1:
sum_1 = 0
for element in a:
sum_1+=int(element)
a = str(sum_1)
This is a good use case for the reduce()
function:这是
reduce()
函数的一个很好的用例:
def persistence(num, op):
while True:
digits = list(map(int, str(num)))
if len(digits) == 1:
return digits[0]
num = reduce(op, digits)
You can call this with op
being either operator.add
or operator.mul
:您可以将
op
设为operator.add
或operator.mul
来调用它:
>>> persistence(1234, operator.add)
1
>>> persistence(1234, operator.mul)
8
How about you try these code:你试试这些代码怎么样:
#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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.