Python program to print checkerboard pattern of nxn using numpy
×


Python program to print checkerboard pattern of nxn using numpy

1159

Introduction

Creating a checkerboard pattern is a classic exercise in programming that helps in understanding array manipulation and indexing. In this article, we'll explore how to generate an nxn checkerboard pattern using NumPy in Python, leveraging NumPy's powerful array operations.

Understanding the Checkerboard Pattern

A checkerboard pattern consists of alternating squares of two colors, typically black and white. In a 2D array, this can be represented by alternating 0s and 1s. For example, a 4x4 checkerboard pattern would look like:

0 1 0 1
1 0 1 0
0 1 0 1
1 0 1 0

Our goal is to generate such a pattern for any given n x n matrix using NumPy.

Method 1: Using Array Slicing

NumPy allows for efficient manipulation of arrays using slicing. To create a checkerboard pattern, we can initialize an n x n array of zeros and then use slicing to assign 1s to alternate positions.

import numpy as np

def create_checkerboard(n):
    # Create an n x n matrix filled with zeros
    checkerboard = np.zeros((n, n), dtype=int)
    
    # Assign 1s to alternate positions
    checkerboard[1::2, ::2] = 1
    checkerboard[::2, 1::2] = 1
    
    return checkerboard

# Example usage
n = 8
print(create_checkerboard(n))

This method efficiently creates a checkerboard pattern by directly modifying the array using slicing.

Method 2: Using Tiling

Another approach is to use NumPy's tile() function, which constructs a new array by repeating a given array. By tiling a 2x2 array of alternating 0s and 1s, we can create a larger checkerboard pattern.

import numpy as np

def create_checkerboard(n):
    # Create a 2x2 array of alternating 0s and 1s
    base = np.array([[0, 1], [1, 0]])
    
    # Tile the base array to create an n x n checkerboard pattern
    checkerboard = np.tile(base, (n // 2, n // 2))
    
    return checkerboard

# Example usage
n = 8
print(create_checkerboard(n))

This method is particularly concise and leverages NumPy's efficient array operations.

Method 3: Using Indexing

For those interested in exploring more advanced techniques, NumPy's indices() function can be used to generate an array of indices, which can then be manipulated to create a checkerboard pattern.

import numpy as np

def create_checkerboard(n):
    # Generate an array of indices
    indices = np.indices((n, n)).sum(axis=0)
    
    # Create a checkerboard pattern by applying modulo operation
    checkerboard = indices % 2
    
    return checkerboard

# Example usage
n = 8
print(create_checkerboard(n))

This method offers a more mathematical approach to generating the pattern.

Conclusion

Creating a checkerboard pattern in Python using NumPy is a great way to practice array manipulation and indexing. Depending on your preferences and the specific requirements of your project, you can choose the method that best suits your needs. Each of the methods discussed offers a unique approach to solving the problem, providing flexibility in your coding toolkit.



If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!


Best WordPress Hosting


Share:


Discount Coupons

Unlimited Video Generation

Best Platform to generate videos

Search and buy from Namecheap

Secure Domain for a Minimum Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat