递增 while 循环在某些测试用例中不起作用
Incrementing while loop isn't working in some test cases
我必须定义一个函数,其中:
Starting with a positive integer original
, keep multiplying original
by n
and calculate the sum of all multiples generated including
original
until the sum is no longer smaller than total
. Return the
minimum number of multiplications needed to reach at value at or above
the given total.
例如:
multiply_until_total_reached (1,5,2)
1*2=2, (1+2)<5, 2*2=4, (1+2+4)>5, 需要2次乘法
multiply_until_total_reached (1,15,2)
1*2=2, (1+2)<15, 2*2=4, (1+2+4)<15, 4*2=8, (1+2+4+8) =15, 3次乘法
我当前的代码可以正常工作,但在某些情况下返回值相差 1。在 1,1038,2 的情况下,我需要 9 次乘法而不是 10 次,但在 1,15,2 的情况下,我得到正确的 (3) 次乘法。
这是我的代码:
def multiply_until_total_reached(original, total, n):
if total < original:
return 0
elif total > original:
sumofdigits = 0 #declares var to keep track of sum of digits to compare to total
timesofmult = 0 #track how many multiplication operations
while sumofdigits <= total + 1:
multnum = original * n
sumofdigits = multnum + original
original = multnum
sumofdigits = sumofdigits + multnum
timesofmult = timesofmult + 1
return timesofmult
是什么导致它关闭?
试试这个,更小更整洁。解释在评论里..
def multiply_until_total_reached(original, total, n):
sum = original #Initialize sum to original
mult_no = 0
while sum < total: #Will auto return 0 if original>=total
sum += original*n #Add original * n
original = original*n #Update the new original
mult_no += 1 #Increase multiplications by 1
return mult_no
print multiply_until_total_reached(1,5,2)
print multiply_until_total_reached(1,15,2)
print multiply_until_total_reached(1,1038,2)
#Output
#2
#3
#10
您的问题是您在每次循环迭代中都重新分配 sumofdigits
。您只需在每次迭代 (sumofdigits += multnum
) 中将 multnum
添加到 sumofdigits
。此外,您的循环条件需要固定为 sumofdigits < total
因为您必须 "Return the minimum number of multiplications needed to reach at value or above the given total."
由于您的代码的解决方案已经发布,并且您接受替代解决方案,请允许我提出以下建议,这充分利用了 Python 的 > 3.2 accumulate()
功能:
from itertools import accumulate, count
def multiply_until_total_reached(original, total, n):
for i, result in enumerate(accumulate(original*n**c for c in count())):
if result >= total: return i
assert multiply_until_total_reached(1,5,2) == 2
assert multiply_until_total_reached(1,15,2) == 3
assert multiply_until_total_reached(1,1038,2) == 10
我必须定义一个函数,其中:
Starting with a positive integer
original
, keep multiplyingoriginal
byn
and calculate the sum of all multiples generated includingoriginal
until the sum is no longer smaller thantotal
. Return the minimum number of multiplications needed to reach at value at or above the given total.
例如:
multiply_until_total_reached (1,5,2)
1*2=2, (1+2)<5, 2*2=4, (1+2+4)>5, 需要2次乘法
multiply_until_total_reached (1,15,2)
1*2=2, (1+2)<15, 2*2=4, (1+2+4)<15, 4*2=8, (1+2+4+8) =15, 3次乘法
我当前的代码可以正常工作,但在某些情况下返回值相差 1。在 1,1038,2 的情况下,我需要 9 次乘法而不是 10 次,但在 1,15,2 的情况下,我得到正确的 (3) 次乘法。
这是我的代码:
def multiply_until_total_reached(original, total, n):
if total < original:
return 0
elif total > original:
sumofdigits = 0 #declares var to keep track of sum of digits to compare to total
timesofmult = 0 #track how many multiplication operations
while sumofdigits <= total + 1:
multnum = original * n
sumofdigits = multnum + original
original = multnum
sumofdigits = sumofdigits + multnum
timesofmult = timesofmult + 1
return timesofmult
是什么导致它关闭?
试试这个,更小更整洁。解释在评论里..
def multiply_until_total_reached(original, total, n):
sum = original #Initialize sum to original
mult_no = 0
while sum < total: #Will auto return 0 if original>=total
sum += original*n #Add original * n
original = original*n #Update the new original
mult_no += 1 #Increase multiplications by 1
return mult_no
print multiply_until_total_reached(1,5,2)
print multiply_until_total_reached(1,15,2)
print multiply_until_total_reached(1,1038,2)
#Output
#2
#3
#10
您的问题是您在每次循环迭代中都重新分配 sumofdigits
。您只需在每次迭代 (sumofdigits += multnum
) 中将 multnum
添加到 sumofdigits
。此外,您的循环条件需要固定为 sumofdigits < total
因为您必须 "Return the minimum number of multiplications needed to reach at value or above the given total."
由于您的代码的解决方案已经发布,并且您接受替代解决方案,请允许我提出以下建议,这充分利用了 Python 的 > 3.2 accumulate()
功能:
from itertools import accumulate, count
def multiply_until_total_reached(original, total, n):
for i, result in enumerate(accumulate(original*n**c for c in count())):
if result >= total: return i
assert multiply_until_total_reached(1,5,2) == 2
assert multiply_until_total_reached(1,15,2) == 3
assert multiply_until_total_reached(1,1038,2) == 10