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
- Base case: What does the tiniest pattern look like? (0 stars, 0 rows.)
- Helper function: Can one job (print a line) be its own small recursive function?
- Order: Draw 3 rows on paper. Do you build top-to-bottom or bottom-to-top?
- 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 callsprint_triangle(1)), which callsprint_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 lineBase case: 0 stars means nothing to print. Recursive: print one star, then print n-1 stars recursively.
Related Page
🔗Related Content
- 💻
Phase 3 - Practice Problems
Practice Phase 3 concepts with hands-on coding problems
- 📝
Phase 3 - Quiz
Test your Phase 3 understanding with assessment questions
- ➡️
Phase 4 - Get Started
Continue to Phase 4 after mastering Phase 3
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum