How to read a pattern: rows, columns and what changes
1 Why patterns come first
A pattern problem shows a small picture made of stars, numbers or letters, and asks us to draw it for any size n. Here is our running pattern for n = 4:
*
**
***
****
The code for a pattern is short. It is an outer loop over the rows and an inner loop that builds one row. The hard part is reading the picture: how many spaces, how many stars, and which character goes where.
So patterns are nested loops with nothing else in the way. After twenty of them, writing a loop inside a loop feels automatic. On oneby7, a pattern problem returns the picture as a list of strings, one string per row, top row first.
2 Rows and columns
First, we put the picture on a grid. We number the rows from 1 at the top and call the row number i. We number the columns from 1 at the left and call the column number j. Every character now has an address: row i, column j.
The grid shows two things the text hides. The spaces are real characters, and each one takes a column. Also, every row ends at column n, so nothing comes after the last star.
Each row has two parts: some spaces, then some stars. To draw the shape, we only need to know how long each part is in row i.
3 What changes from row to row
We write one line per row and count each part. In this table, each space is shown as · so we can see it.
Row i | Spaces | Stars | The row |
|---|---|---|---|
| 1 | 3 | 1 | ···* |
| 2 | 2 | 2 | ··** |
| 3 | 1 | 3 | ·*** |
| 4 | 0 | 4 | **** |
Now we look down each column of the table. The stars go up by one per row and always equal the row number. The spaces go down by one per row, and spaces plus stars always make n. So for row i:
A formula that fits one row can still be wrong, so we check it on the first and the last row. Row 1 gives 4 − 1 = 3 spaces and 1 star. Row 4 gives 0 spaces and 4 stars. Both match the picture.
Most patterns follow the same steps:
- Number the rows from 1, and write the size n of the example next to them.
- Split each row into parts, such as spaces, then stars, then spaces again.
- Count each part in every row, in a table.
- Turn each count into a formula in i and n, and check it on the first and last row.
4 From the table to code
The outer loop runs i from 1 to n. For each row, one inner loop adds n − i spaces, and a second inner loop adds i stars. Then the finished row goes into the list.
For n = 4, it returns [" *", " **", " ***", "****"], the four rows of the table. Every language also has a shortcut that repeats a character, such as string(n - i, ' ') in C++ or " " * (n - i) in Python. It does the same work as the inner loop.
The answer holds about n²/2 characters, so the work is O(n²). Pattern problems keep n small, so the cost does not matter here.
5 What goes in each cell
The counts give the shape. The second question is what each mark is. Here the shape stays the same and only the marks change. We count the marks in a row from 1 and call that place k.
| Each mark is | Row 3 becomes |
|---|---|
| A star | ·*** |
| The row number i | ·333 |
| Its place k | ·123 |
| The k-th letter | ·ABC |
| A counter that carries on from the rows above | ·456 |
The counter starts at 1 before the outer loop and goes up by one after every mark. Rows 1 and 2 use 1, 2 and 3, so row 3 starts at 4.
For letters, we add k − 1 to the letter A: (char)('A' + k - 1) gives it.
Some problems return each row as a list of numbers instead of a string. The loops stay the same, and only what we add to the row changes.
6 Holes and bigger shapes
Some shapes have a hole in the middle. Their rows do not split into a few simple parts, so we decide each cell on its own. The inner loop visits every column of the row and asks one question: does this cell get a star? Here is a hollow triangle for n = 5:
Row i has i cells. A cell gets a star in three cases: it is in the first column, it is on the slope where j = i, or it is in the last row.
for i from 1 to n
row = ""
for j from 1 to i
if j == 1 or j == i or i == n
add "*" to row
else
add " " to row
add row to the list
Bigger shapes are often two simple shapes joined together. A shape that grows and then shrinks is two loops, one after the other: the first draws the top half and the second draws the bottom half. A shape with a left part and a right part gets one inner loop per part, each with its own count.
7 Common mistakes
Each of these gives a picture that looks almost right and gets Wrong Answer:
- Spaces after the last star. If the hollow triangle's inner loop runs to n instead of i, row 1 becomes
*····. It looks the same on screen as*, but it is a different string. - A formula off by one. With spaces = n − i + 1, the last row for n = 4 is
·****. Checking the formula on the last row catches this before we run the code. - One row string for all rows. If
rowis created before the outer loop, it never empties. Row 2 for n = 4 becomes···*··**. A new row starts inside the outer loop. - Rows from 0 with formulas from 1. With i from 0 to n − 1, stars = i gives an empty first row. The loop and the table must count rows the same way.
The smallest input, n = 1, is a good last check. Our pattern must give ["*"].
The habit to keep: before writing any loop, fill the table of rows and counts, and check every formula on the first and the last row.
Practice it
- Square of starsEasy
- Right triangle of starsEasy
- Right triangle of numbersEasy
- Triangle of repeated numbersEasy
- Inverted right triangle of starsEasy
- Inverted right triangle of numbersEasy
- Star pyramidEasy
- Inverted star pyramidEasy
- Star diamondEasy
- Half diamondEasy
- Alternating binary triangleEasy
- Number crownEasy
- Increasing number triangleEasy
- Increasing letter triangleEasy
- Reverse letter triangleEasy
- Letter rampEasy
- Letter pyramidEasy
- Letter triangle from the endEasy
- Symmetric voidMedium
- Symmetric butterflyMedium
- Hollow squareMedium
- Concentric number squaresMedium
Finished this lesson?
Mark it read and it is ticked on the roadmap and in the list.