## DEV Community

John Johnson Okah

Posted on

# Pattern Program: A Creative Intersection of Logic and Design

Pattern programming is like drawing with code, where you can make all sorts of interesting shapes and designs with a set of instructions. It is one of the earliest intersections of art and logic in programming.

## What is Pattern Program?

A pattern program is a piece of code that generates a specific textual pattern in the form of shapes, numerical sequences, or even complex designs. Pattern programs are commonly used to practice problem-solving skills that have to do with logic and control structures. It is an accessible way for beginners to learn programming concepts while also producing visually interesting results.

In pattern programming, a rule is defined on how each element is arranged to make a recognizable pattern.
For example, creating rows and columns of a rectangle by printing a pattern element in a nested loop. Usually, the first loop defines the length of the shape (in rows), then a new line (\n), and the second loop defines the width of the shape (in columns).

Rectangle pattern:

``````*******
*******
*******
``````

Program pattern for the rectangle:

``````for i in range(3): # rows
for j in range(7): # columns
print("*", end="") # pattern element *
print() # new line
``````

## Types of Pattern Programs

Pattern programs often comes in these three (3) patterns:

1. Shape Pattern: A shape pattern program involves creating visual designs using a character symbol to form various geometric shapes, such as triangles, squares, retangles, and more.
2. Number Pattern: A number pattern program involves using numbers to create sequences or arrangements that follow a specific rule or logic. A typical example is the pascal triangle.
3. Letter Pattern: A letter pattern program involves using letters of the alphabet to create interesting arrangements or shapes.

## Examples of Pattern Programs

Here are some examples of pattern program in python:

### 1. Diamond Pattern

``````def generate_diamond_pattern(rows):
# Upper half of the diamond
for i in range(1, rows + 1):
print(" " * (rows - i) + "* " * i)

# Lower half of the diamond (excluding the center row for odd row numbers)
for i in range(rows - 1, 0, -1):
print(" " * (rows - i) + "* " * i)

# Example: Generate a diamond pattern with 5 rows
rows = 5
generate_diamond_pattern(rows)
``````

output:

``````    *
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
``````

### 2. Alphabetic Half Pyramid Pattern

``````def generate_alphabetic_half_pyramid(rows):
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(chr(ord('A') + j - 1), end=" ")
print()

# Example: Generate an alphabetic half pyramid pattern with 5 rows
rows = 5
generate_alphabetic_half_pyramid(rows)
``````

output:

``````A
A B
A B C
A B C D
A B C D E
``````

### 3. Pascal Triangle

``````def generate_pascals_triangle_pyramid(rows):
triangle = []
for i in range(rows):
row = [1] * (i + 1)
for j in range(1, i):
row[j] = triangle[i - 1][j - 1] + triangle[i - 1][j]
triangle.append(row)

max_width = len(' '.join(map(str, triangle[-1])))

for row in triangle:
formatted_row = ' '.join(map(str, row))
print(formatted_row.center(max_width))

# Example: Generate Pascal's Triangle with 5 rows in pyramid pattern
rows = 5
generate_pascals_triangle_pyramid(rows)
``````

output:

``````    1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
``````

### 4. Chessboard Pattern

``````def generate_chessboard(rows, cols):
for i in range(rows):
for j in range(cols):
# Check if the sum of row and column indices is even or odd
if (i + j) % 2 == 0:
print("■", end=" ")
else:
print("□", end=" ")
print()

# Example: Generate a chessboard pattern with 8 rows and 8 columns
rows = 8
cols = 8
generate_chessboard(rows, cols)
``````

output:

``````■ □ ■ □ ■ □ ■ □
□ ■ □ ■ □ ■ □ ■
■ □ ■ □ ■ □ ■ □
□ ■ □ ■ □ ■ □ ■
■ □ ■ □ ■ □ ■ □
□ ■ □ ■ □ ■ □ ■
■ □ ■ □ ■ □ ■ □
□ ■ □ ■ □ ■ □ ■
``````

### 5. Doormat Pattern

``````def print_welcome_pattern(M, N):
pattern = ".|."
welcome_message = "WELCOME"

# Print the top part of the pattern
for i in range(M // 2):
line = (pattern * (2 * i + 1)).center(N, '-')
print(line)

# Print the welcome message
print(welcome_message.center(N, '-'))

# Print the bottom part of the pattern
for i in range(M // 2 - 1, -1, -1):
line = (pattern * (2 * i + 1)).center(N, '-')
print(line)

# Example usage for a 11 x 33 pattern
print_welcome_pattern(11, 33)
``````

output:

``````---------------.|.---------------
------------.|..|..|.------------
---------.|..|..|..|..|.---------
------.|..|..|..|..|..|..|.------
---.|..|..|..|..|..|..|..|..|.---
-------------WELCOME-------------
---.|..|..|..|..|..|..|..|..|.---
------.|..|..|..|..|..|..|.------
---------.|..|..|..|..|.---------
------------.|..|..|.------------
---------------.|.---------------
``````

## Conclusion

As we've seen with triangles, diamonds, and more, there's an artistic side to coding that often goes unnoticed. It's about finding beauty in the logical. Both newbies and seasoned coders can hone their creative, artistic, and programming skills while playing with Pattern Programs. The beauty of pattern programming lies in its simplicity and the endless possibilities it offers.