MASTER YOUR LOGIC BUILDING:Phase 3 - Level 3: Pattern Recursion

Mastering phase 3 - level 3: pattern recursion concepts and implementation.

Phase 3 - Level 3: Pattern Recursion

Why Patterns + Recursion?

Printing a triangle of stars looks like nested layers — a 4-row triangle contains a 3-row triangle plus one extra row at the bottom. That's recursion in disguise. Instead of nested for loops, you ask: "Print a smaller triangle, then add my row."

The new skill here isn't a new rule — it's learning when to act (print) relative to the recursive call.

What You'll Learn

  • Print a line of stars with recursion
  • Build a triangle row by row using a helper function
  • The golden question: recurse first, or print first?

Pattern Preview

Line: ***** — five stars in one row

Triangle (growing down):

*
**
***
****

Each row is one star longer than the row above — like Russian dolls stacked from smallest on top to largest on bottom.

Step 1 — One Row of Stars

def print_stars(n):
    if n == 0:           # base: no stars left
        return
    print("*", end="")   # print one star
    print_stars(n - 1)   # trust: prints n-1 more stars

Trust: print_stars(3) prints one star, then trusts print_stars(2) prints two more.

Step 2 — Full Triangle (Recurse First, Then Print)

def print_triangle(n):
    if n == 0:              # base: zero rows = nothing
        return
    print_triangle(n - 1)   # trust: smaller triangle prints first
    print_stars(n)          # then this row
    print()                 # move to next line

Why recurse first? Because row 1 must appear above row 2. The smaller triangle (n-1 rows) prints completely before you add the wider bottom row.

Rule of thumb: If the pattern grows downward, usually recurse first, print second. If it shrinks downward, print first, then recurse.

Step-by-Step Thinking Process

  1. Base case: What does the tiniest pattern look like? (0 stars, 0 rows.)
  2. Helper function: Can one job (print a line) be its own small recursive function?
  3. Order: Draw 3 rows on paper. Do you build top-to-bottom or bottom-to-top?
  4. Combine: One recursive call + one local action (print a row).

Common Mistakes & Tips

  • Wrong order — printing before print_triangle(n-1) gives an upside-down triangle. Swap and see!
  • Forgetting `print()` after each row — stars run together on one line.
  • **Using print("*" * n)** is fine for practice, but learn the recursive line builder first — it transfers to harder patterns.
  • Tip: Trace only print_triangle(2): it calls print_triangle(1)), which calls print_triangle(0)) (stops), then prints 1 star, then 2 stars.

Practice Focus

Use the examples below — Print Line of N Stars is your building block; Print Triangle combines it with the "recurse first" rule. Try changing the order of the recursive call and the print_stars line to feel the difference instantly.

Hands-on Examples

Print Line of N Stars Recursively

def print_stars(n):
    # Base case
    if n == 0:
        return
    
    # Print one star and recurse
    print("*", end="")
    print_stars(n - 1)

# Test
n = int(input("Enter n: "))
print_stars(n)
print()  # New line

Base case: 0 stars means nothing to print. Recursive: print one star, then print n-1 stars recursively.