[英]Checking to see if a number is evenly divisible by other numbers with recursion in Python
At the risk of receiving negative votes, I will preface this by saying this is a midterm problem for a programming class. 冒着受到反对的风险,我将在序言中说这是编程班的中期问题。 However, I have already submitted the code and passed the question.
但是,我已经提交了代码并通过了问题。 I changed the name of the function(s) so that someone can't immediately do a search and find the correct code, as that is not my purpose.
我更改了函数的名称,以使某人无法立即进行搜索并找到正确的代码,因为这不是我的目的。 I am actually trying to figure out what is actually MORE CORRECT from two pieces that I wrote.
我实际上是想从我写的两篇文章中找出真正更正确的内容。
The problem tells us that a certain fast food place sells bite-sized pieces of chicken in packs of 6, 9, and 20. It wants us to create a function that will tell if a given number of bite-sized piece of chicken can be obtained by buying different packs. 这个问题告诉我们,某个快餐店以6、9和20为一包出售一口大小的鸡肉。它希望我们创建一个函数来判断给定数量的一口大小的鸡肉是否可以通过购买不同的包装获得。 For example, 15 can be bought, because 6 + 9 is 15, but 16 cannot be bought, because no combination of the packs will equal 15. The code I submitted and was "correct" on, was:
例如,可以购买15个,因为6 + 9为15,但是不能购买16个,因为没有任何组合的组合等于15。我提交的代码“正确”为:
def isDivisible(n):
"""
n is an int
Returns True if some integer combination of 6, 9 and 20 equals n
Otherwise returns False.
"""
a, b, c = 20, 9, 6
if n == 0:
return True
elif n < 0:
return False
elif isDivisible(n - a) or isDivisible(n - b) or isDivisible(n - c):
return True
else:
return False
However, I got to thinking, if the initial number is 0, it will return True. 但是,我想,如果初始数字为0,它将返回True。 Would an initial number of 0 be considered "buying that amount using 6, 9, and/or 20"?
初始数字0是否被认为是“使用6、9和/或20购买该数量”? I cannot view the test cases the grader used, so I don't know if the grader checked 0 as a test case and decided that True was an acceptable answer or not.
我无法查看评分者使用的测试用例,所以我不知道评分者是否将0选作测试用例并确定True是可接受的答案。 I also can't just enter the new code, because it is a midterm.
我也不能只输入新的代码,因为这是中期的。 I decided to create a second piece of code that would handle an initial case of 0, and assuming 0 is actually False:
我决定创建第二个代码来处理初始情况0,并假设0实际上为False:
def isDivisible(n):
"""
n is an int
Returns True if some integer combination of 6, 9 and 20 equals n
Otherwise returns False.
"""
a, b, c = 20, 9, 6
if n == 0:
return False
else:
def helperDivisible(n):
if n == 0:
return True
elif n < 0:
return False
elif helperDivisible(n - a) or helperDivisible(n - b) or helperDivisible(n - c):
return True
else:
return False
return helperDivisible(n)
As you can see, my second function had to use a "helper" function in order to work. 如您所见,我的第二个功能必须使用“助手”功能才能工作。 My overall question, though, is which function do you think would provide the correct answer, if the grader had tested for 0 as an initial input?
但是,我的总体问题是,如果分频器已将0作为初始输入进行测试,那么您认为哪个函数会提供正确的答案?
I would argue that the first function is correct. 我认为第一个功能是正确的。
我的回答是第二个功能更正确,因为从技术上讲,使用6、9和/或20的包装不能购买初始数字0。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.