I'm completing this CodingBat problem:
We want to make a row of bricks that is goal inches long. We have a number of small bricks (1 inch each) and big bricks (5 inches each). Return True if it is possible to make the goal by choosing from the given bricks. This is a little harder than it looks and can be done without any loops.
make_bricks(3, 1, 8) → True make_bricks(3, 1, 9) → False make_bricks(3, 2, 10) → True
I wrote:
if small + big * 5 >= goal: return True
else: return False
My errors are: make_bricks(3, 2, 9)
→ False
(I put true because (2*5)+3=13 which is greater than 9.
make_bricks(2, 1000000, 100003) → False
make_bricks(1, 4, 12) → False
I found this answer than passed those tests:
if goal > small + big * 5:
return False
else:
return goal % 5 <= small
I dont really get why, can someone please explain?
Here is a shorter solution.
def make_bricks(small, big, goal):
return (goal%5)<=small and (goal-(big*5))<=small
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With