CodingBat code practice

## MakeBricks Solution Code

Video

MakeBricks is a favorite problem I created for CodingBat, probably because it has more depth than it appears at first. Here I'll walk through one way to solve it.

See also: MakeBricks Introduction , Mod Introduction

Here's the live MakeBricks and the slightly more difficult MakeChocolate problems:
makeBricks  makeChocolate

(in python: make_bricks  make_chocolate )

• Lots of ways to solve it -- anything without iteration is ok
• For some problems, there is a very short solution -- not always!
• Use "mod" % 5 to figure needed small bricks (vs. loops)
• Think about cases where the goal is not possible .. easier

## Fail #1: not enough bricks

• Is the goal bigger than all the big and small bricks we have?
• Easy to express: goal vs. big*5 + small

## Fail #2: not enough small bricks

• Do we not have enough small bricks to get the last bit of length
• Use `goal % 5`

## Divide and Conquer

Sometimes a problem appears to be a baffling tangle. Often, if you slow down, you can break it down to 2 or 3 cases, and then solve them one at a time. "Divide and Conquer" as Caesar said, and that's what's going on here, focussing on one aspect of the problem at a time.

CodingBat.com code practice. Copyright 2013 Nick Parlante.