Krantikari
Krantikari

Reputation: 41

How do I print a checkerboard pattern in Python?

I am trying to print a checkerboard patter in Python which takes 3 input, L, W and N. For example, if L = 3, W = 5, N = 2, I should get,

XXXXX     XXXXX     
XXXXX     XXXXX     
XXXXX     XXXXX     
     XXXXX     XXXXX
     XXXXX     XXXXX
     XXXXX     XXXXX
XXXXX     XXXXX     
XXXXX     XXXXX     
XXXXX     XXXXX     
     XXXXX     XXXXX
     XXXXX     XXXXX
     XXXXX     XXXXX
def cb(L, W, N):
    for row in range(0, 2*N):
        for smallrow in range(0, L):
            for col in range(0, 2*N):
                for smallcol in range(0, W):
                    if row % 2 == col % 2:
                        print('x')
                    else:
                        print(' ')
            print('\n')

I expected the output but it is printing out a single column of 'x's.

Upvotes: 4

Views: 2688

Answers (4)

Andrej Kesely
Andrej Kesely

Reputation: 195613

Itertools version of checker generating, using chain and tee:

from itertools import tee, chain

def checker(L, W, N):
    c1 = chain(*tee(chain('X' * W, ' ' * W), N), '\n')
    c2 = chain(*tee(chain(' ' * W, 'X' * W), N), '\n')

    for v in chain(*tee(chain(chain(*tee(c1, L)), chain(*tee(c2, L))), N)):
        print(v, end='')

checker(3, 5, 2)

Prints:

XXXXX     XXXXX     
XXXXX     XXXXX     
XXXXX     XXXXX     
     XXXXX     XXXXX
     XXXXX     XXXXX
     XXXXX     XXXXX
XXXXX     XXXXX     
XXXXX     XXXXX     
XXXXX     XXXXX     
     XXXXX     XXXXX
     XXXXX     XXXXX
     XXXXX     XXXXX

Upvotes: 1

Djaouad
Djaouad

Reputation: 22794

You can shorten your code a lot and make it more efficient by building the two rows that make up the entire checkerboard only once, concatenate them to make a pair of rows, and then print the pair N times (making use of string-string addition and string-number multiplication):

def cb(L, W, N):
    r1 = (('X' * W + ' ' * W) * N + '\n') * L
    r2 = ((' ' * W + 'X' * W) * N + '\n') * L
    print((r1 + r2) * N)

cb(3, 5, 2)

Output:

XXXXX     XXXXX     
XXXXX     XXXXX     
XXXXX     XXXXX     
     XXXXX     XXXXX
     XXXXX     XXXXX
     XXXXX     XXXXX
XXXXX     XXXXX     
XXXXX     XXXXX     
XXXXX     XXXXX     
     XXXXX     XXXXX
     XXXXX     XXXXX
     XXXXX     XXXXX

Upvotes: 0

Seraph Wedd
Seraph Wedd

Reputation: 864

To do it efficiently, you need to only call a print statement once. Another point of efficiency improvement is the for loops that was called several times. A more efficient way to do this is by doing string addition. For example:

def cb(l, w, n):
    full = ''
    for i in range(n):
        full += l*(('x'*w+' '*w) * n + '\n')
        full += l*((' '*w+'x'*w) * n + '\n')
    print(full)

cb(3, 5, 2)

This should output a similar checkered pattern.

Upvotes: 1

eumiro
eumiro

Reputation: 213115

The problem is with your prints. Its usage is:

print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

so it prints automatically a newline after your 'x' and ' '.

Do

print('x', end='')

instead.

The same, at the end print('\n') prints two newlines. Use print() instead.

Upvotes: 3

Related Questions