Python Program to Print Zigzag Pattern
for loops and a variable to track the current row, moving up and down to create the zigzag shape.Examples
How to Think About It
Algorithm
Code
def print_zigzag(n, length): pattern = [[' ' for _ in range(length)] for _ in range(n)] row = 0 direction = 1 for col in range(length): pattern[row][col] = '*' if row == n - 1: direction = -1 elif row == 0: direction = 1 row += direction for r in pattern: print(''.join(r)) print_zigzag(3, 9)
Dry Run
Let's trace the example with n=3 and length=9 through the code
Initialize pattern
pattern is a 3x9 grid filled with spaces
Start at row=0, direction=1
Place '*' at (0,0)
Move down to row=1
Place '*' at (1,1)
Move down to row=2
Place '*' at (2,2), change direction to -1
Move up to row=1
Place '*' at (1,3)
Move up to row=0
Place '*' at (0,4), change direction to 1
Move down to row=1
Place '*' at (1,5)
Move down to row=2
Place '*' at (2,6), change direction to -1
Move up to row=1
Place '*' at (1,7)
Move up to row=0
Place '*' at (0,8)
| Column | Row | Direction | Pattern Position Marked |
|---|---|---|---|
| 0 | 0 | 1 | (0,0) |
| 1 | 1 | 1 | (1,1) |
| 2 | 2 | 1 | (2,2) |
| 3 | 1 | -1 | (1,3) |
| 4 | 0 | -1 | (0,4) |
| 5 | 1 | 1 | (1,5) |
| 6 | 2 | 1 | (2,6) |
| 7 | 1 | -1 | (1,7) |
| 8 | 0 | -1 | (0,8) |
Why This Works
Step 1: Pattern grid creation
We create a 2D list filled with spaces to hold the zigzag pattern, so we can place stars at correct positions.
Step 2: Row and direction control
We use a variable to track the current row and a direction flag to move down or up through rows, creating the zigzag movement.
Step 3: Placing stars
For each column, we place a star at the current row, then update the row based on direction to form the zigzag shape.
Step 4: Printing the pattern
Finally, we print each row as a string, showing the stars and spaces in the zigzag pattern.
Alternative Approaches
def print_zigzag_alt(n, length): row = 0 direction = 1 rows = ['' for _ in range(n)] for col in range(length): for r in range(n): if r == row: rows[r] += '*' else: rows[r] += ' ' if row == n - 1: direction = -1 elif row == 0: direction = 1 row += direction for line in rows: print(line) print_zigzag_alt(3, 9)
def print_zigzag_formula(n, length): cycle = 2 * (n - 1) for r in range(n): line = '' for i in range(length): if i % cycle == r or i % cycle == cycle - r: line += '*' else: line += ' ' print(line) print_zigzag_formula(3, 9)
Complexity: O(n * length) time, O(n * length) space
Time Complexity
The program loops through each column and each row to place stars or spaces, resulting in O(n * length) time.
Space Complexity
It uses a 2D list or multiple strings to store the pattern, requiring O(n * length) space.
Which Approach is Fastest?
The formula-based approach is fastest as it avoids extra state tracking, but the loop with direction flag is easier to understand.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Direction flag with 2D list | O(n * length) | O(n * length) | Clear logic and easy to modify |
| String concatenation per row | O(n * length) | O(n * length) | Simple code for beginners |
| Mathematical formula | O(n * length) | O(n * length) | Efficient and concise for fixed patterns |