Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solving CodingBat brick making puzzle in Python

Tags:

python

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?

like image 940
Benjamin Bakhshi Avatar asked Dec 07 '22 10:12

Benjamin Bakhshi


1 Answers

Here is a shorter solution.

def make_bricks(small, big, goal):
    return (goal%5)<=small and (goal-(big*5))<=small
like image 134
Hassan Raza Avatar answered Jan 01 '23 05:01

Hassan Raza