简体   繁体   English

使用Python递归检查数字是否可以被其他数字平均除

[英]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. 我认为第一个功能是正确的。

  • There's no reason to make zero special. 没有理由使零特别。
  • every number divisible by six is should result in true (and zero is divisible by six), and similarly for nine and twenty. 每个可被6整除的数字都应为true(零可被6整除),类似地,其结果为9和20。
  • If the question is simply "can this result of zero pieces be achieved when the only packs I can buy are six, nine and twenty" then the answer is still yes (just don't buy anything); 如果问题很简单:“当我只能买六,九和二十包时,能否实现零件的结果”,答案仍然是肯定的(就是什么也不要买)。 that's the closest to an actual use case for this function I can think of at the moment. 这是我目前可以想到的最接近此功能的实际用例。
  • The simpler implementation of the first function suggests a greater elegance. 第一个函数的更简单实现表明更高的优雅度。
  • If you state the problem more arithmetically, it's most easily stated as "given n, do there exist natural #'si, j, k, st n = 6i + 9j + 20k" and in that formulation the answer is unequivocally true for n=0. 如果您用数学方式更清楚地说明问题,则最容易将其表述为“给定n,是否存在自然的#'si,j,k,st n = 6i + 9j + 20k”,在这种表述中,对于n = 0。
  • If you extend the above to integer i, j, k, then you should give true also for -6, +6, -9, -15, +15, and so also zero. 如果将以上内容扩展为整数i,j,k,则对于-6,+ 6,-9,-15,+ 15,也应为true,因此也为零。 If you return false for zero then lots of nice pretty properties (if f(n) true then f(n) +/- 6 true) also break, which speaks back to my third point. 如果为零返回false,那么很多漂亮的漂亮属性(如果f(n)为true,则f(n)+/- 6为true)也会中断,这可以说出我的第三点。

我的回答是第二个功能更正确,因为从技术上讲,使用6、9和/或20的包装不能购买初始数字0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM