**Bonus Challenges**

\n",
">\n",
">3. What other values for the total areas of the five rectangles are possible?\n",
">\n",
">4. Which sets of rectangles may be assembled to form a square?\n",
"\n",
"To me, these are interesting questions because, first, I have a (slight) personal connection to Solomon Golomb (my former colleague at USC) and to Nelson Blachman (the father of my colleague Nancy Blachman), who presented the problem to Antonik, and second, I find it interesting that the problems span the range from mathematical to computational. I wonder how the way I look at the problems compares to others who have a different mix of these two disciplines."
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"1. How many different sets of five rectangles are possible?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is a basic [combinatorics](http://en.wikipedia.org/wiki/Combinatorics) or counting problem. I will present *three* methods to count the sets. (Hopefully they will give the same answer.) The example set given in the problem was\n",
"\n",
"> {1 × 3, 2 × 4, 5 × 7, 6 × 8, 9 × 10}\n",
" \n",
"and in general it would be\n",
"\n",
"> {A × B, C × D, E × F, G × H, I × J}\n",
"\n",
"The question is: how many distinct ways can we assign values to the variables A through J?\n",
" \n",
"**Method 1: Count all permutations and divide by repetitions:** There are 10 variables to be filled, so there are 10! = 3628800 permutations. But if we fill the first two variables with 1 × 3, that is the same rectangle as 3 × 1. So divide 10! by 2^{5} to account for the fact that each of 5 rectangles can appear 2 ways. Similarly, if we fill A and B with 1 × 3, that yields the same set as if we filled C and D with 1 × 3. So divide again by 5! (the number of permutations of 5 things) to account for this.\n",
"That gives us 10! / 2^{5} / 5! = 945. (It is always gratifying when this \"count and divide\" method comes out to a whole number.)\n",
"\n",
"**Method 2: Count without repetitions**: The example set is presented in [lexicographical order](http://en.wikipedia.org/wiki/Lexicographical_order): in each rectangle the smaller component is listed first, and in the set, the rectangles with smaller first components are listed first. An alternate to \"count and divide\" is to count directly how many sets there are in lexicographical order. We'll work from left to right. How many choices are there for variable A? Only one: A must always be 1, because we agreed that the smallest number comes first. Then, given A, there are 9 remaining choices for B. For C, given A and B, there is again only one choice: C must be the smallest of the remaining 8 numbers (it will be 3 if the first rectangle was 1 × 2; otherwise it will be 2, but either way there is only one choice). That leaves 7 choices for D, 5 for F, 3 for H and 1 for J. And 9 × 7 × 5 × 3 × 1 = 945. (It is always gratifying when two methods give the same answer.)\n",
" \n",
"**Method 3: Write a program to enumerate the sets:** We'll represent the 1 × 3 rectangle as the tuple `(1, 3)` and the example set of rectangles as the set\n",
"\n",
"> {(1, 3), (2, 4), (5, 7), (6, 8), (9, 10)}\n",
"\n",
"To generate all the sets of rectangles, we'll follow method 2: given a set of sides, first make all possible rectangles `(A, B)` where `A` is the shortest side, and `B` ranges over all the other sides. For each of `(A, B)` rectangle, consider all possible `(C, D)` rectangles, and so on. This is easy to write out if we know there will be exactly 5 rectangles in a set:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def sets_of_rectangles(sides):\n",
" \"Given exactly 10 sides, yield all sets of rectangles that can be made from them.\"\n",
" A = min(sides)\n",
" for (A, B) in each_rectangle(sides):\n",
" for (C, D) in each_rectangle(sides - {A, B}):\n",
" for (E, F) in each_rectangle(sides - {A, B, C, D}):\n",
" for (G, H) in each_rectangle(sides - {A, B, C, D, E, F}):\n",
" for (I, J) in each_rectangle(sides - {A, B, C, D, E, F, G, H}):\n",
" yield {(A, B), (C, D), (E, F), (G, H), (I, J)}\n",
" \n",
"def each_rectangle(sides):\n",
" \"Yield all (A, B) pairs, where A is minimum of sides, and B ranges over all other sides.\"\n",
" A = min(sides)\n",
" for B in (sides - {A}):\n",
" yield (A, B) "
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 100
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"sides = set(range(1, 11))\n",
"sets = list(sets_of_rectangles(sides))\n",
"len(sets)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 101,
"text": [
"945"
]
}
],
"prompt_number": 101
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(Gratifying that once again we get the same answer.) But I don't like the fact that the code only works for exactly 10 sides, and it violates the \"[don't repeat yourself](http://en.wikipedia.org/wiki/Don't_repeat_yourself)\" maxim. So let's convert the nested-iterated algorithm into a *recursive* algorithm that chooses the first rectangle (in the same way as before), but then recursively solves for the remaining sides and unions the set of rectangles from the remaining sides with the first rectangle. (Note we have to also correctly handle the case when there are no sides; then there is one way to make a set of rectangles: the empty set.)"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def sets_of_rectangles(sides):\n",
" \"Given a set of sides, yield all sets of rectangles that can be made from sides.\"\n",
" if sides:\n",
" A = min(sides)\n",
" for B in (sides - {A}):\n",
" for rest in sets_of_rectangles(sides - {A, B}):\n",
" yield {(A, B)}.union(rest)\n",
" else:\n",
" yield set()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 102
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# For example, with 6 sides:\n",
"list(sets_of_rectangles({1, 2, 3, 4, 5, 6}))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 103,
"text": [
"[{(1, 2), (3, 4), (5, 6)},\n",
" {(1, 2), (3, 5), (4, 6)},\n",
" {(1, 2), (3, 6), (4, 5)},\n",
" {(1, 3), (2, 4), (5, 6)},\n",
" {(1, 3), (2, 5), (4, 6)},\n",
" {(1, 3), (2, 6), (4, 5)},\n",
" {(1, 4), (2, 3), (5, 6)},\n",
" {(1, 4), (2, 5), (3, 6)},\n",
" {(1, 4), (2, 6), (3, 5)},\n",
" {(1, 5), (2, 3), (4, 6)},\n",
" {(1, 5), (2, 4), (3, 6)},\n",
" {(1, 5), (2, 6), (3, 4)},\n",
" {(1, 6), (2, 3), (4, 5)},\n",
" {(1, 6), (2, 4), (3, 5)},\n",
" {(1, 6), (2, 5), (3, 4)}]"
]
}
],
"prompt_number": 103
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Reaffirm the answer with 10 sides\n",
"sets = list(sets_of_rectangles(sides))\n",
"len(sets)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 104,
"text": [
"945"
]
}
],
"prompt_number": 104
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I don't want to print all 945 sets, but let's peek at every 100th one:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"sets[::100]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 105,
"text": [
"[{(1, 2), (3, 4), (5, 8), (6, 9), (7, 10)},\n",
" {(1, 2), (3, 10), (4, 6), (5, 9), (7, 8)},\n",
" {(1, 3), (2, 10), (4, 9), (5, 7), (6, 8)},\n",
" {(1, 4), (2, 10), (3, 8), (5, 9), (6, 7)},\n",
" {(1, 5), (2, 9), (3, 6), (4, 10), (7, 8)},\n",
" {(1, 6), (2, 9), (3, 10), (4, 7), (5, 8)},\n",
" {(1, 7), (2, 9), (3, 8), (4, 10), (5, 6)},\n",
" {(1, 8), (2, 7), (3, 5), (4, 10), (6, 9)},\n",
" {(1, 9), (2, 7), (3, 10), (4, 6), (5, 8)},\n",
" {(1, 10), (2, 7), (3, 8), (4, 9), (5, 6)}]"
]
}
],
"prompt_number": 105
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"2. What are the maximum and minimum values for the total areas of the five rectangles?"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"source": [
"I think I know this one, but I'm not completely sure. I know that a rectangle with a fixed perimeter has maximum area when it is a square. My guess is that the maximum total area occurs when each rectangle is *almost* square: a 9 × 10 rectangle; 7 × 8; and so on. So that would give us a maximum area of:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"(1 * 2) + (3 * 4) + (5 * 6) + (7 * 8) + (9 * 10)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 106,
"text": [
"190"
]
}
],
"prompt_number": 106
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And the minimum area should be when the rectangles deviate the most from squares: a 1 × 10, and so on:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"(1 * 10) + (2 * 9) + (3 * 8) + (4 * 7) + (5 * 6)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 107,
"text": [
"110"
]
}
],
"prompt_number": 107
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Since I am not sure, I will double check by computing the set of all possible areas and then asking for the min and max of the set:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def area(rectangle): (w, h) = rectangle; return w * h\n",
"\n",
"def total_area(rectangles): return sum(area(r) for r in rectangles)\n",
"\n",
"areas = {total_area(s) for s in sets}"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 108
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"max(areas)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 109,
"text": [
"190"
]
}
],
"prompt_number": 109
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"min(areas)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 110,
"text": [
"110"
]
}
],
"prompt_number": 110
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This verifies that the maximum and minimum are what I thought they were. But I still don't think I completely understand the situation. For example, suppose there are *N* sides that are not necessarily consecutive integers. Will the maximum total area always be formed by combining the two biggest sides, and so on?"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"3. What other values for the total areas of the five rectangles are possible?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I have no idea how to figure this out mathematically from first principles, but it is easy to compute with the code we already have:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print sorted(areas) "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 177, 178, 179, 180, 181, 182, 183, 184, 186, 187, 190]\n"
]
}
],
"prompt_number": 111
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Is there a more succint way to describe these values?"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"set(range(110, 191)) - areas"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 112,
"text": [
"{176, 185, 188, 189}"
]
}
],
"prompt_number": 112
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Yes: All the integers between 110 and 190 inclusive, except 176, 185, 188, and 189."
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"4. Which sets of rectangles may be assembled to form a square?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The only way I can think about this is to write a program; I don't see any way to work it out by hand. I do know that the total area will have to be a perfect square, and that in the range of areas (110 to 190) the only perfect squares are 11^{2} = 121, 12^{2} = 144, and 13^{2} = 169. We can find the rectangle sets that have a perfect square as their total area:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"[s for s in sets if total_area(s) in {121, 144, 169}]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 113,
"text": [
"[{(1, 2), (3, 5), (4, 9), (6, 10), (7, 8)},\n",
" {(1, 2), (3, 7), (4, 9), (5, 6), (8, 10)},\n",
" {(1, 2), (3, 7), (4, 6), (5, 10), (8, 9)},\n",
" {(1, 2), (3, 8), (4, 5), (6, 10), (7, 9)},\n",
" {(1, 3), (2, 5), (4, 8), (6, 9), (7, 10)},\n",
" {(1, 3), (2, 7), (4, 8), (5, 6), (9, 10)},\n",
" {(1, 3), (2, 7), (4, 5), (6, 10), (8, 9)},\n",
" {(1, 3), (2, 9), (4, 10), (5, 7), (6, 8)},\n",
" {(1, 3), (2, 10), (4, 8), (5, 7), (6, 9)},\n",
" {(1, 3), (2, 10), (4, 7), (5, 9), (6, 8)},\n",
" {(1, 4), (2, 5), (3, 7), (6, 9), (8, 10)},\n",
" {(1, 5), (2, 3), (4, 9), (6, 7), (8, 10)},\n",
" {(1, 5), (2, 4), (3, 8), (6, 7), (9, 10)},\n",
" {(1, 5), (2, 6), (3, 8), (4, 10), (7, 9)},\n",
" {(1, 5), (2, 7), (3, 4), (6, 8), (9, 10)},\n",
" {(1, 6), (2, 3), (4, 8), (5, 7), (9, 10)},\n",
" {(1, 6), (2, 9), (3, 10), (4, 8), (5, 7)},\n",
" {(1, 6), (2, 10), (3, 8), (4, 9), (5, 7)},\n",
" {(1, 6), (2, 10), (3, 9), (4, 7), (5, 8)},\n",
" {(1, 7), (2, 4), (3, 8), (5, 9), (6, 10)},\n",
" {(1, 7), (2, 9), (3, 5), (4, 6), (8, 10)},\n",
" {(1, 7), (2, 10), (3, 6), (4, 9), (5, 8)},\n",
" {(1, 8), (2, 6), (3, 10), (4, 9), (5, 7)},\n",
" {(1, 8), (2, 7), (3, 10), (4, 6), (5, 9)},\n",
" {(1, 8), (2, 9), (3, 7), (4, 6), (5, 10)},\n",
" {(1, 8), (2, 10), (3, 5), (4, 9), (6, 7)},\n",
" {(1, 9), (2, 5), (3, 7), (4, 6), (8, 10)},\n",
" {(1, 9), (2, 6), (3, 5), (4, 7), (8, 10)},\n",
" {(1, 9), (2, 7), (3, 8), (4, 6), (5, 10)},\n",
" {(1, 9), (2, 7), (3, 10), (4, 5), (6, 8)},\n",
" {(1, 9), (2, 7), (3, 6), (4, 10), (5, 8)},\n",
" {(1, 9), (2, 8), (3, 6), (4, 7), (5, 10)},\n",
" {(1, 10), (2, 4), (3, 5), (6, 8), (7, 9)},\n",
" {(1, 10), (2, 5), (3, 9), (4, 8), (6, 7)},\n",
" {(1, 10), (2, 8), (3, 7), (4, 5), (6, 9)}]"
]
}
],
"prompt_number": 113
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"len(_)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 114,
"text": [
"35"
]
}
],
"prompt_number": 114
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So 35 out of 945 rectangle sets *might* be assembled into a square; we don't know yet. Also I would like to see *how* the rectangles are packed into the square, not just *which* sets can be packed, so I'll need a visual display of rectangles packed into a square. To start, I'll represent a *Grid* as a two-dimensional array: a list of rows, each of which is a list of cells, with the idea that each cell will be covered by a rectangle. We'll initialize each cell to be empty:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"empty = (0, 0)\n",
"\n",
"def Grid(width, height):\n",
" return [[empty for col in range(width)] for row in range(height)]\n",
"\n",
"def Square(size): return Grid(size, size)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 115
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"Square(5)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 116,
"text": [
"[[(0, 0), (0, 0), (0, 0), (0, 0), (0, 0)],\n",
" [(0, 0), (0, 0), (0, 0), (0, 0), (0, 0)],\n",
" [(0, 0), (0, 0), (0, 0), (0, 0), (0, 0)],\n",
" [(0, 0), (0, 0), (0, 0), (0, 0), (0, 0)],\n",
" [(0, 0), (0, 0), (0, 0), (0, 0), (0, 0)]]"
]
}
],
"prompt_number": 116
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's make a function to place a rectangle onto a grid. `place_rectangle_at` places a rectangle of width *w* and height *h* onto a grid at position (x_{0}, y_{0}). If the rectangle overlaps a non-empty cell, or goes off the grid, we return `None` to indicate that this is not a legal placement. If the rectangle does fit, we return a new grid with any old rectangles plus the new one (we do not modify the old grid):"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def place_rectangle_at(rect, grid, pos):\n",
" \"\"\"Place the rectangle of size (w, h) onto grid at position (x0, y0).\n",
" Return a new grid, or None if the rectangle cannot be placed.\"\"\"\n",
" (w, h) = rect\n",
" (x0, y0) = pos\n",
" newgrid = map(list, grid) # make a copy\n",
" try:\n",
" for x in range(x0, x0+w):\n",
" for y in range(y0, y0+h):\n",
" if newgrid[y][x] is not empty:\n",
" return None \n",
" newgrid[y][x] = rect\n",
" return newgrid\n",
" except IndexError: # went off the grid\n",
" return None"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 117
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Place a 3 x 4 rectangle on a square grid, 2 cells over and 1 down:\n",
"place_rectangle_at((3, 4), Square(5), (2, 1))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 118,
"text": [
"[[(0, 0), (0, 0), (0, 0), (0, 0), (0, 0)],\n",
" [(0, 0), (0, 0), (3, 4), (3, 4), (3, 4)],\n",
" [(0, 0), (0, 0), (3, 4), (3, 4), (3, 4)],\n",
" [(0, 0), (0, 0), (3, 4), (3, 4), (3, 4)],\n",
" [(0, 0), (0, 0), (3, 4), (3, 4), (3, 4)]]"
]
}
],
"prompt_number": 118
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Place two rectangles on a grid\n",
"grid2 = place_rectangle_at((3, 4), Square(5), (2, 1))\n",
"grid3 = place_rectangle_at((2, 5), grid2, (0, 0))\n",
"grid3"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 119,
"text": [
"[[(2, 5), (2, 5), (0, 0), (0, 0), (0, 0)],\n",
" [(2, 5), (2, 5), (3, 4), (3, 4), (3, 4)],\n",
" [(2, 5), (2, 5), (3, 4), (3, 4), (3, 4)],\n",
" [(2, 5), (2, 5), (3, 4), (3, 4), (3, 4)],\n",
" [(2, 5), (2, 5), (3, 4), (3, 4), (3, 4)]]"
]
}
],
"prompt_number": 119
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Now try to place a third rectangle that does not fit; should fail\n",
"print place_rectangle_at((6, 1), grid3, (0, 2))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"None\n"
]
}
],
"prompt_number": 120
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we need a strategy for packing a set of rectangles onto a grid. I know that many variants of [bin packing problems](http://en.wikipedia.org/wiki/Bin_packing_problem) are NP-hard, but we only have 5 rectangles, so it should be easy: just exhaustively try each rectangle in each possible position in both possible orientations (horizontal and vertical). But placing rectangles is commutative, so we can do this two ways:\n",
"\n",
"> Way 1: Considering the rectangles in a fixed order, try every possible position for each rectangle.\n",
"\n",
"or\n",
"\n",
"> Way 2: Considering the positions in a fixed order, try every possible rectangle for each position.\n",
"\n",
"In Way 1, we could pre-sort the rectangles (say, biggest first). Then we try to put the biggest rectangle in all possible positions on the grid, and for each position that fits, try putting the second biggest rectangle in all remaining positions, and so on. \n",
"\n",
"In Way 2, we consider the positions in some fixed order; say top-to-bottom, left-to right. Take the first empty position (the upper left corner). Try putting each of the rectangles there, and for each one that fits, try the next empty position, and consider all possible rectangles to go there, and so on.\n",
"\n",
"Which way is better? I'm not sure—let us calculate. As a wild guess, assume there are on average about 10 ways to place a rectangle on a grid. Then Way 1 will look at 10^{5} = 100,000 combinations. For Way 2, there are only 5! = 120 permutations of rectangles, and each rectangle can go either horizontaly or verticaly, so that's 5! × 2^{5} = 3840. Since 3840 < 100,000, I'll go with Way 2. Here is a more precise description:\n",
"\n",
"> Way 2: To **pack** a set of rectangles onto a grid, find the first empty cell on the grid. Try in turn to place each rectangle (in either orientation) at that position. For each one that fits, try to recursively **pack** the remaining rectangles, and return the resulting grid if one of these recursive calls succeeds. If none succeeds, return None. *Details:* If there are no rectangles to pack, the solution is the original grid. If the grid is illegal, we can't place anything on it, so return None. The order we choose for the first empty cell is arbitrary; we will go top-to-bottom, left-to-right."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def pack(rectangles, grid):\n",
" \"\"\"Find a way to pack all rectangles onto grid and return the packed grid,\n",
" or return None if not possible.\"\"\"\n",
" if not rectangles:\n",
" return grid # Placing no rectangles give you the original grid\n",
" elif not grid:\n",
" return None # Can't put rectangles on an illegal grid\n",
" else:\n",
" pos = first_empty_cell(grid)\n",
" for (w, h) in rectangles: # for each rectangle\n",
" for rect in [(w, h), (h, w)]: # in either orientation\n",
" grid2 = place_rectangle_at(rect, grid, pos)\n",
" solution = pack(rectangles - {(w, h)}, grid2)\n",
" if solution:\n",
" return solution\n",
" return None\n",
" \n",
"def first_empty_cell(grid):\n",
" \"The uppermost, leftmost empty cell position on grid.\"\n",
" for (y, row) in enumerate(grid):\n",
" for (x, cell) in enumerate(row):\n",
" if cell is empty:\n",
" return (x, y)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 121
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's try a simple example that I know will fit:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"pack({(1, 3), (2, 5), (3, 4)}, Square(5))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 122,
"text": [
"[[(2, 5), (2, 5), (3, 1), (3, 1), (3, 1)],\n",
" [(2, 5), (2, 5), (3, 4), (3, 4), (3, 4)],\n",
" [(2, 5), (2, 5), (3, 4), (3, 4), (3, 4)],\n",
" [(2, 5), (2, 5), (3, 4), (3, 4), (3, 4)],\n",
" [(2, 5), (2, 5), (3, 4), (3, 4), (3, 4)]]"
]
}
],
"prompt_number": 122
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Colored Rectangles"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The grid output does show how the rectangles are packed into the square, but the output is hard to read. It would be nicer to have a graphical display of colored rectangles. IPython has an add-on module called [ipythonblocks](http://ipythonblocks.org/) that does this. (If you are running this notebook in your copy of IPython notebook, you may need to do the shell command `\"pip install ipythonblocks\"` or `\"easy-install ipythonblocks\"`.) I will define the function `show` which takes a grid and displays it as a matrix of colored rectangles. "
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import ipythonblocks\n",
"\n",
"def show(grid):\n",
" \"Convert a python grid into an ipythonblocks BlockGrid and show it.\"\n",
" (w, h) = (len(grid[0]), len(grid))\n",
" bg = ipythonblocks.BlockGrid(w, h)\n",
" for x in range(w):\n",
" for y in range(h):\n",
" bg[y,x] = color(grid[y][x])\n",
" bg.show()\n",
" \n",
"def color(rect):\n",
" \"Determine a color for a rectangle.\"\n",
" # Base the color on the length of the sides of the rectangle\n",
" # Choose R,G,B values to give rectangles different colors\n",
" light = 240 # R,G,B = 240,240,240 is a light grey\n",
" short, long = sorted(rect) # Find the short and long sides\n",
" R, G, B = light-24*short, light-24*long, light-(50*(long%3) + 70*(short%3))\n",
" return (R, G, B)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 123
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"show(pack({(1, 3), (2, 5), (3, 4)}, Square(5)))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"