-2
\$\begingroup\$

Problem 4

As per the given link, this exercise is part of recursion lab.

Printer A prints a random x copies 50 ≤ x ≤ 60, and Printer B prints a random y copies 130 ≤ y ≤ 140. Can the two printers be used in a way that guarantees that the number of copies is between some lower and upper bound?

Hint: Try using a helper function.

def sum_range(lower, upper):
    """
    >>> sum_range(45, 60) # Printer A prints within this range
    ;
    ... # the TAs would be satisfied with any number it prints
    ...
    True
    >>> sum_range(40, 55) # Printer A can print some number 56-60
    ... # copies, which is not within the TA acceptable range
    ...
    False
    >>> sum_range(170, 201) # Printer A + Printer B will print
    ... # somewhere between 180 and 200 copies total
    ...
    True
    """

Solution

def sum_range(given_min, given_max):
    """
    >>> sum_range(45, 60) # Printer A prints within this range;
    True
    >>> sum_range(40, 55) # Printer A can print some number 56-60
    False
    >>> sum_range(170, 201) # Printer A + Printer B will print somewhere between 180 and 200 copies total
    True
    """
    def helper(print_min, print_max):
        if print_min >= given_min and print_max <= given_max:
            return True
        if print_min and (print_min < given_min or print_max > given_max): 
            return False
        return helper(50 + print_min, 60 + print_max) or helper(130 + print_min, 140 + print_max) or helper(50 + 130 + print_min, 60 + 140 + print_max)
    return helper(0, 0)

Tested this solution, it works.

Can this solution still be improved?

Does it make sense to memoiz this solution? Is their a chance of having similar nodes in a tree that has redundant computation?

\$\endgroup\$
5
  • 1
    \$\begingroup\$ What about using Printer A twice, etc? \$\endgroup\$ – o11c Jul 14 '15 at 7:16
  • \$\begingroup\$ @o11c Considering that this problem is part of a problem set where the preceding question was about linear combinations, I believe that it the point of the problem is to consider using Printer A and Printer B multiple times. Hence the suggestion in the problem to use recursion. I do not believe that this code solves the problem. \$\endgroup\$ – 200_success Jul 14 '15 at 7:30
  • \$\begingroup\$ Have you considered looking at the class instructor's solutions for these kinds of questions? \$\endgroup\$ – 200_success Jul 14 '15 at 7:34
  • \$\begingroup\$ @200_success the solution seems correct. \$\endgroup\$ – xyz Jul 14 '15 at 11:21
  • \$\begingroup\$ @o11c you are right, I modified the solution. \$\endgroup\$ – overexchange Jul 15 '15 at 2:10
2
\$\begingroup\$

No idea about the recursion part, but a few things to mention.

This

def helper(given_lower, given_upper, avlbl_lower, avlbl_upper):
    if given_lower <= avlbl_lower and given_upper >= avlbl_upper:
        return True
    return False  

can be simplified by returning the condition like

def helper(given_lower, given_upper, avlbl_lower, avlbl_upper):
    return given_lower <= avlbl_lower and given_upper >= avlbl_upper  

Instead of using result1..result3 I would use 2 if statements and return early if one of these method calls returns true. There is no need to call each one if the first returns true.

Like so

if helper(lower, upper, printerA[0], printerA[-1]):
    return true
if helper(lower, upper, printerB[0], printerB[-1]):
    return true

return helper(lower, upper, printerA[0] + printerB[0], printerA[-1] + printerB[-1])
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.