Day 13 - Logical Thinking

Pattern Printing & Problem Solving Exercises

Why Pattern Printing?

Pattern printing exercises help develop logical thinking and problem-solving skills. They teach you how to break down complex problems into smaller, manageable steps.

These exercises improve your understanding of loops, conditional statements, and algorithmic thinking - essential skills for any programmer!

Pattern Printing Exercises

Try to solve these pattern printing problems. Solutions are provided below each problem.

1 Right-Angled Triangle

Print a right-angled triangle pattern with 5 rows:

*
**
***
****
*****
Solution:
rows = 5
for i in range(1, rows + 1):
    print('*' * i)

2 Inverted Right-Angled Triangle

Print an inverted right-angled triangle pattern with 5 rows:

*****
****
***
**
*
Solution:
rows = 5
for i in range(rows, 0, -1):
    print('*' * i)

3 Pyramid Pattern

Print a pyramid pattern with 4 rows:

   *
  ***
 *****
*******
Solution:
rows = 4
for i in range(1, rows + 1):
    spaces = ' ' * (rows - i)
    stars = '*' * (2 * i - 1)
    print(spaces + stars)

4 Number Triangle

Print a number triangle with 5 rows:

1
12
123
1234
12345
Solution:
rows = 5
for i in range(1, rows + 1):
    for j in range(1, i + 1):
        print(j, end='')
    print()

5 Diamond Pattern

Print a diamond pattern with 5 rows in the top half:

  *
 ***
*****
 ***
  *
Solution:
n = 5
# Top half
for i in range(1, n+1, 2):
    spaces = ' ' * ((n - i) // 2)
    print(spaces + '*' * i)

# Bottom half
for i in range(n-2, 0, -2):
    spaces = ' ' * ((n - i) // 2)
    print(spaces + '*' * i)

6 Hollow Square

Print a hollow square pattern with side length 5:

*****
*   *
*   *
*   *
*****
Solution:
size = 5
for i in range(size):
    for j in range(size):
        if i == 0 or i == size-1 or j == 0 or j == size-1:
            print('*', end='')
        else:
            print(' ', end='')
    print()

7 Alphabet Pyramid

Print an alphabet pyramid with 5 rows:

A
AB
ABC
ABCD
ABCDE
Solution:
rows = 5
for i in range(1, rows + 1):
    for j in range(i):
        print(chr(65 + j), end='')
    print()

8 Number Pyramid

Print a number pyramid with 5 rows:

1
22
333
4444
55555
Solution:
rows = 5
for i in range(1, rows + 1):
    print(str(i) * i)

9 Floyd's Triangle

Print Floyd's Triangle with 5 rows:

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Solution:
rows = 5
number = 1
for i in range(1, rows + 1):
    for j in range(1, i + 1):
        print(number, end=' ')
        number += 1
    print()

10 Pascal's Triangle

Print the first 5 rows of Pascal's Triangle:

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Solution:
rows = 5
for i in range(rows):
    # Print leading spaces
    print(' ' * (rows - i), end='')
    
    # Calculate and print values
    value = 1
    for j in range(i + 1):
        print(value, end=' ')
        value = value * (i - j) // (j + 1)
    print()

11 Cross Pattern

Print a cross pattern with size 5:

*    *
 *  *
  *
 *  *
*    *
Solution:
size = 5
for i in range(size):
    for j in range(size):
        if i == j or i + j == size - 1:
            print('*', end='')
        else:
            print(' ', end='')
    print()

12 Binary Number Triangle

Print a binary number triangle with 5 rows:

1
01
101
0101
10101
Solution:
rows = 5
for i in range(1, rows + 1):
    start = 1 if i % 2 == 1 else 0
    for j in range(i):
        print(start, end='')
        start = 1 - start  # Flip between 0 and 1
    print()

13 Hollow Diamond

Print a hollow diamond pattern with 5 rows in the top half:

  *
 * *
*   *
 * *
  *
Solution:
n = 5
# Top half
for i in range(1, n+1):
    spaces = ' ' * (n - i)
    if i == 1:
        print(spaces + '*')
    else:
        print(spaces + '*' + ' ' * (2*i-3) + '*')

# Bottom half
for i in range(n-1, 0, -1):
    spaces = ' ' * (n - i)
    if i == 1:
        print(spaces + '*')
    else:
        print(spaces + '*' + ' ' * (2*i-3) + '*')

14 Spiral Number Pattern

Print a spiral number pattern with size 4:

1  2  3  4
12 13 14 5
11 16 15 6
10 9  8  7
Solution:
n = 4
matrix = [[0]*n for _ in range(n)]
directions = [(0,1), (1,0), (0,-1), (-1,0)]
x, y, d = 0, 0, 0

for i in range(1, n*n+1):
    matrix[x][y] = i
    nx, ny = x + directions[d][0], y + directions[d][1]
    
    if 0 <= nx < n and 0 <= ny < n and matrix[nx][ny] == 0:
        x, y = nx, ny
    else:
        d = (d + 1) % 4
        x, y = x + directions[d][0], y + directions[d][1]

for row in matrix:
    for num in row:
        print(f"{num:2}", end=' ')
    print()

15 Zigzag Pattern

Print a zigzag pattern with height 4 and width 8:

*     *     *
 *   *   *   *
  * *    * *
   *       *
Solution:
height = 4
width = 8

for i in range(height):
    line = [' '] * width
    pos = i
    step = 2 * (height - 1)
    
    while pos < width:
        line[pos] = '*'
        pos += step
        if step != 2 * (height - 1):
            step = 2 * (height - 1) - step
    
    print(''.join(line))