简体   繁体   中英

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. 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:

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. Would an initial number of 0 be considered "buying that amount using 6, 9, and/or 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. 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:

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?

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.
  • 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.
  • 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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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