Learning

Pattern Printing Problems

Pattern Printing Problems

Core Concept: Nested Loops

Patterns are built with two loops:

  • Outer loop → controls ROWS
  • Inner loop → controls COLUMNS (what prints on each row)
python
# Basic square pattern n = 5 for i in range(n): # Rows for j in range(n): # Columns print('*', end=' ') print() # New line after each row # * * * * * # * * * * * # * * * * * # * * * * * # * * * * *

Right-Angled Triangle

python
n = 5 for i in range(1, n + 1): for j in range(i): print('*', end=' ') print() # * # * * # * * * # * * * * # * * * * * # Using string multiplication (simpler!) for i in range(1, n + 1): print('* ' * i)

Inverted Triangle

python
n = 5 for i in range(n, 0, -1): print('* ' * i) # * * * * * # * * * * # * * * # * * # *

Pyramid (Full Triangle)

python
n = 5 for i in range(1, n + 1): spaces = ' ' * (n - i) stars = '* ' * i print(spaces + stars) # * # * * # * * * # * * * * # * * * * *

Diamond Pattern

python
n = 5 # Upper half for i in range(1, n + 1): print(' ' * (n - i) + '* ' * i) # Lower half for i in range(n - 1, 0, -1): print(' ' * (n - i) + '* ' * i)

Number Patterns

python
# Floyd's Triangle n = 5 num = 1 for i in range(1, n + 1): for j in range(i): print(f'{num:3}', end='') num += 1 print() # 1 # 2 3 # 4 5 6 # 7 8 9 10 # 11 12 13 14 15 # Number triangle n = 5 for i in range(1, n + 1): for j in range(1, i + 1): print(j, end=' ') print() # 1 # 1 2 # 1 2 3 # 1 2 3 4 # 1 2 3 4 5 # Binary triangle for i in range(1, n + 1): for j in range(i): print(j % 2, end=' ') print() # 0 # 0 1 # 0 1 0 # 0 1 0 1 # 0 1 0 1 0

Character Patterns

python
# Alphabet triangle n = 5 for i in range(1, n + 1): ch = 65 # ASCII for 'A' for j in range(i): print(chr(ch), end=' ') ch += 1 print() # A # A B # A B C # A B C D # A B C D E # Repeated character triangle for i in range(1, n + 1): print((chr(64 + i) + ' ') * i) # A # B B # C C C # D D D D # E E E E E

Hollow Patterns

python
# Hollow square n = 5 for i in range(n): for j in range(n): if i == 0 or i == n-1 or j == 0 or j == n-1: print('*', end=' ') else: print(' ', end=' ') print() # * * * * * # * * # * * # * * # * * * * *

Key Formula Summary

| Pattern | Spaces | Stars | Formula | |---------|--------|-------|---------| | Right triangle | 0 | i | * * i | | Inverted | 0 | n-i+1 | * * (n-i+1) | | Pyramid | n-i | i | ' '*(n-i) + '* '*i | | Diamond | abs(n-i) | n-abs(n-i) | Varies |

Key Rules
  • •Outer loop = rows, inner loop = columns — always think in this 2D grid
  • •String multiplication ('* ' * i) is cleaner than nested loops for simple patterns
  • •For pyramid: spaces decrease (n-i) while stars increase (i) — they must sum to n
  • •Hollow patterns: print star ONLY on first/last row OR first/last column
  • •Use chr(65) for 'A', chr(97) for 'a' — increment for next character
  • •Use f'{num:width}' or end=' ' to maintain alignment in number patterns
Your Task

Create a function `print_pattern(n, type)` that prints different patterns based on type: 'triangle' (right-angled), 'pyramid', 'diamond', 'hollow_square'. Each function should use nested loops (not just string multiplication). Also create `number_triangle(n)` that prints Floyd's triangle with proper spacing alignment.

EditorPython · JSX
PreviewUpdates on Run Tests
Loading preview…
Tests
Should define print_pattern function
Should use nested for loops
Should handle 'pyramid' type
Should handle 'hollow_square' with boundary checks
Should define number_triangle for Floyd's triangle
Should use number formatting for alignment