A2Z Lesson

Log in
← Back to the A2Z sheet

Step 01 Beginner Problems — Logic Building (Patterns)

Easy and Medium

Medium

Pattern problems look trivial and teach more about loops than any other beginner topic: they force you to control two nested loops precisely and to see a picture as numbers. This lesson covers the approach for the easy and medium band — the shapes built from a single rule per row: rectangles, triangles, pyramids, diamonds and the simpler number and letter patterns (roughly Patterns 1–18 of the Patterns subtopic).

The four golden rules

Every pattern program in this band follows the same contract:

Rule 1: the outer loop runs once per row — if the picture has n rows, the outer loop runs n times. Rule 2: the inner loop runs once per column in that row — its count is a function of the row number. Rule 3: print the symbol inside the inner loop, without a newline. Rule 4: print exactly one newline after each row.

When a pattern goes wrong, it is almost always one of these four being violated — the newline inside the inner loop is the classic beginner bug. Your entire job reduces to one question: for row i, how many spaces and how many symbols?

The observation method

Never start coding from the picture. First, tabulate. Take the star pyramid for n = 4:

row i | spaces | stars 0 | 3 | 1 1 | 2 | 3 2 | 1 | 5 3 | 0 | 7

Now find formulas in i: spaces = n - i - 1, stars = 2*i + 1. Those two formulas ARE the program; the loops are clerical work. If the table has no obvious formula, split the row into segments (spaces, symbols, spaces) and tabulate each segment separately — every easy/medium pattern yields to this.

Worked example: the star pyramid

Translating the table directly — one loop prints the spaces, one prints the stars, then a newline. This three-inner-loop shape (spaces, stars, newline) is the template for every centred pattern.

def star_pyramid(n): for i in range(n): print(' ' * (n - i - 1), end='') print('*' * (2 * i + 1)) star_pyramid(4) # * # *** # ***** # *******

The easy/medium families

Triangles (Patterns 2–6): the inner count is i+1 or n-i — one formula, growing or shrinking. Pyramids and diamonds (7–10): a diamond is a pyramid followed by its inverse, so write both halves or reuse one function twice. Number triangles (3, 4, 13): the printed value changes — per-row counter, per-column counter, or a running counter that survives across rows (Floyd's triangle). Letter triangles (14–16): print chr(ord('A') + j) — letters are just numbers with a costume on.

Medium patterns add one twist: two different segments per row that must be tracked independently. The observation table handles them unchanged — add a column per segment.

Key points

  • Four rules: outer loop = rows, inner loop = columns as a function of the row, symbols without newline, one newline per row.
  • Never code from the picture — tabulate spaces/symbols per row and find the formula in i first.
  • Centred patterns use three segments per row: spaces, symbols, newline; diamonds are pyramid + inverted pyramid.
  • Number and letter patterns only change what is printed: a counter, chr(ord('A') + j), or a running counter across rows.
  • When stuck, add a column to the table per segment — every easy/medium pattern decomposes this way.

Suggested questions

Pattern 1 — Rectangular Star Pattern The baseline: constant inner count. If this is not automatic, stay here a while.
Pattern 7 — Star Pyramid The worked example from this lesson — reproduce it from the observation table, not from memory.
Pattern 9 — Diamond Star Pattern Your first composite: pyramid plus inverted pyramid in one program.
Pattern 13 — Increasing Number Triangle Pattern The running-counter pattern (Floyd's triangle) — the counter must survive across rows.

Source note: this lesson follows the structure of Striver's public A2Z DSA course (takeUforward). Each problem links to its original platform, and all problems and content belong to their respective owners — STUDY-BUDDY does not claim ownership of the sheet or its problems, and no permission beyond public linking is claimed.