Exercise 9.1.6: Checkerboard, v1 Spoint
In the next three exercises, we will work towards creating a program that
stores numbers corresponding to checkers pieces on a board! Our ultimate
goal here is to make a grid that stores 1's and O's, like below, such that a 1
represents a checker piece and a O represents a blank square:
ed to us
metho
loops
6 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1
GOOOOOOO
OOOOOOOO
1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0
WO
Note that the middle two rows are blank!
For this exercise, you should just make a grid that has 1's on the top 3 rows
and the bottom 3 rows. this is coding and on codehs, if anyone has done 9.1.6, please give me the answers, thank you ​

Respuesta :

Answer:

In order to get following pattern, we have to use numpy package.

Following code with work perfectly fine and will print the pattern.

Python code starts as below

*********************************************

# Python program to print 8 X 8 alternative 1 and 0's. 3rd and 4th row with all 0's

# checkerboard pattern using numpy

# We need Following pattern

# 0 1 0 1 0 1 0 1

# 1 0 1 0 1 0 1 0

# 0 1 0 1 0 1 0 1

# 0 0 0 0 0 0 0 0

# 0 0 0 0 0 0 0 0

# 1 0 1 0 1 0 1 0

# 0 1 0 1 0 1 0 1

# 1 0 1 0 1 0 1 0

import numpy as np

# function to print Checkerboard pattern

def printcheckboard(n):

       print(" Customized Checkerboard pattern:")

       # create a n * n matrix  

       x = np.zeros((n, n), dtype = int)

       y = np.zeros((n, n), dtype = int)

       # fill with 1 the alternate rows and columns

       x[1::2, ::2] = 1

       x[::2, 1::2] = 1

      # fill with 0 the alternate rows and columns

       y[1::2, ::2] = 0

       y[::2, 1::2] = 0

       # print the pattern  for first 3 rows

       for i in range(0,3):

               for j in range(n):

                       print(x[i][j], end =" ")

               print()

       # print the pattern   for next two rows with all 0's

       for k in range(3,5):

               for l in range(n):

                       print(y[k][l], end =" ")

               print()

        # print the pattern  for last 3 rows with alternative 1 and 0.        

       for i in range(5,8):

               for j in range(n):

                       print(x[i][j], end =" ")

               print()

# Calling the function code

n = 8

printcheckboard(n)

**************************************

End of the Python Code.

Explanation:

In this you have to use Python3.7 and numpy should be installed as well in order to execute the code successfully.

2 D matrix will be created using Python numpy library and checkboard pattern is generated using array slicing.

Here n=8 and it will generate the checkerboard pattern of alternative 0 and 1. However, we need row 4th and 5th as all 0. So we have initialized two arrays matrix as x and y.

Comments in the code is self explanatory.

PS: Please make sure code is edited in IDE so that tabs or space issues can be taken care.

The checkerboard is an illustration of loops and iterations.

Loops are used to carry out repetitive and iterative operations

The checkerboard program in Python where comments are used to explain each line is as follows

#This imports numpy

import numpy as np

#This creates a 8 by 8 matrix

row = np.zeros((8, 8), dtype = int)

col = np.zeros((8, 8), dtype = int)

# This fills the alternate rows and columns with 1

row[1::2, ::2] = 1

row[::2, 1::2] = 1

# This fills the alternate rows and columns with 0

col[1::2, ::2] = 0

col[::2, 1::2] = 0

# The following loop prints the first three rows

for i in range(0,3):

   for j in range(8):

       print(row[i][j], end =" ")

   print()

# The following loop prints the middle 0s

for i in range(3,5):

   for j in range(8):

       print(col[i][j], end =" ")

   print()

# The following loop prints the last three rows

for i in range(5,8):

   for j in range(8):

       print(row[i][j], end =" ")

   print()

Read more about loops at:

https://brainly.com/question/14943773

ACCESS MORE