Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Character Patterns Recursively

Build alphabet rows with two recursive helpers. Students trace row counts, per-row character positions, and the base cases that bound both recursive paths.

AdvancedPhase 3 - Level 3: Pattern RecursionExample 10 of 10
character-patterns-recursively.py
1def print_chars(n, current=0):
2 # Base case
3 if current >= n:
4 return
5
6 # Print character
7 print(chr(65 + current), end="") # 65 is 'A'
8 # Recurse
9 print_chars(n, current + 1)
10
11def print_pattern(n, row=1):
12 # Base case
13 if row > n:
14 return
15
16 # Print row
17 print_chars(row)
18 print()
19 # Recurse
20 print_pattern(n, row + 1)
21
22# Test
23n = int(input("Enter rows: "))
24print_pattern(n)

Output

Enter rows: 4
A
AB
ABC
ABCD

In this one

  • Recurse over character codes
  • Reset per-row positions
  • Coordinate two helpers

How it works

  1. 1

    Choose a row width in print_pattern.

  2. 2

    Print chr(65 + current) until current reaches that width.

  3. 3

    End the row and request the next one.

Edge cases

What happens for n = 0?

What happens for n = 0?

The pattern helper prints no rows.

Does the character sequence wrap after Z?

Does the character sequence wrap after Z?

No. chr continues to later code points.

What's going on

print_chars converts 65 + current with chr, beginning at A. Its current counter rises until it reaches the requested row width.

print_pattern calls that helper for widths one through n and ends each line before requesting the next row. Thus three rows are A, AB, ABC. The call stack tracks both helpers. No alphabet wrap is coded after Z.

What happens for n = 0? The pattern helper prints no rows.

Does the character sequence wrap after Z? No. chr continues to later code points.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Why start current at zero?

Pick an answer to check it.

Question 2

What happens for n = 0?

Pick an answer to check it.

Questions people ask

What happens for n = 0?

The pattern helper prints no rows.

Does the character sequence wrap after Z?

No. chr continues to later code points.

Why start current at zero?

chr(65 + 0) produces A.