sc0rched
sc0rched

Reputation: 21

Writing a "Chessboard" function in Python that prints out requested length of binary

I have an assignment for my Python course that I'm struggling with.

We are supposed to make a function that prints out binary as follows:

If the input is:

chessboard(3)

It should print out:

101
010
101

And so forth..

Its a "simple" program but I'm really new to coding.

I can produce a while loop that writes out the correct length and amount of lines but I'm struggling to produce variation between the lines.

This is what I have come up with so far:

def chessboard(n):
height = n
length = n
while height > 0:
    while length > 0:
        print("1", end="")
        length -= 1
        if length > 0:
            print("0", end="")
            length -= 1
    height -= 1
    if length == 0:
        break
    else:
        print()
        length = n

With the input:

chessboard(3)

It prints out:

101
101
101

Could someone help me figure out how I could start every other line with zero instead of one?

Upvotes: 1

Views: 6835

Answers (8)

jerelev
jerelev

Reputation: 1

def chessboard(n):
    for i in range(n):
        if i % 2 == 0:
            start_num = 0
            second_num = 1
        else:
            start_num = 1
            second_num = 0
        line=[]
        for j in range(n):
            if j % 2 == 0:
                line.append(start_num)
            else:
                line.append(second_num)
        print(line)


chessboard(8)

Answer:

[0, 1, 0, 1, 0, 1, 0, 1]
[1, 0, 1, 0, 1, 0, 1, 0]
[0, 1, 0, 1, 0, 1, 0, 1]
[1, 0, 1, 0, 1, 0, 1, 0]
[0, 1, 0, 1, 0, 1, 0, 1]
[1, 0, 1, 0, 1, 0, 1, 0]
[0, 1, 0, 1, 0, 1, 0, 1]
[1, 0, 1, 0, 1, 0, 1, 0]

Upvotes: 0

parvus
parvus

Reputation: 6026

def chessboard(n: int):
    row: str = ('10' * (n//2 + 1))[:n]
    for _ in range(n):
        print(row)
        row = row[1:] + row[0]

chessboard(3)

Upvotes: 1

tew tadle
tew tadle

Reputation: 1

My solution contains a few more lines than neccessary but it works:

def chessboard(length):
    linecount = 0
    widthcount = 0
    while linecount < length:
        while linecount % 2 == 0:
            if widthcount == length:
                break
            if widthcount < length - 1:
                print(1, end="")
            else:
                print(1)
            widthcount += 1
            if widthcount == length:
                break
            if widthcount < length - 1:
                print(0, end="")
            else: 
                print(0)
            widthcount += 1
        while linecount % 2 != 0:
            if widthcount == length:
                break
            if widthcount < length - 1:
                print(0, end="")
            else: 
                print(0)
            widthcount += 1
            if widthcount == length:
                break
            if widthcount < length - 1:
                print(1, end="")
            else:
                print(1)
            widthcount += 1
        linecount += 1
        widthcount = 0
    linecount = 0

Upvotes: 0

Leo
Leo

Reputation: 1

I've used only conditional statements and loops as per the course curriculum, It is working fine but let me know if I can do anything to improve it.


def chessboard(num):
    h = 0
    while h <= (num//2):
        if num%2 == 0:
            if h==num//2:
                print("")
            elif h!= num//2:
                print("10"*(num//2))
                print("01"*(num//2))
        elif num%2 != 0:
            if h == num//2:
               print(("10"*(num//2))+ "1")
            elif h!= num//2:
                print(("10"*(num//2))+ "1")
                print(("01"*(num//2))+ "0")
        h+=1

Upvotes: 0

nrgx
nrgx

Reputation: 387

def chessboard(n):
    board = []
    for i in range(n):
        row = []
        for j in range(n):
            row.append('1' if (i+j)%2 == 0 else '0')
        board.append(row)
    return '\n'.join(''.join(row) for row in board)

print(chessboard(3))

Output

101
010
101

Upvotes: 0

RookieMistake
RookieMistake

Reputation: 1

def chessboard(x):
    i = 0
    while i < x:
        if i % 2 == 0:
            row = "10"*x
        else:
            row = "01"*x
        print(row[0:x])
        i += 1

Upvotes: 0

Tiia Makinen
Tiia Makinen

Reputation: 1

I am working on the same kind of assignment, but as we have only covered conditional statements and while loops so far, following the same logic, here is my solution:

def chessboard(size):

 output_1 = ''
 output_2 = ''
 i = 1
 j = 1

 while j <= size:

   while i <= size:
        
        if i % 2 == 0:
            output_1 += '1'
            output_2 += '0'
            i += 1
        else:
            output_1 += '0'
            output_2 += '1'
            i += 1

    if j % 2 == 0:
        print(output_1)
        j += 1
    else:
        print(output_2)
        j += 1

chessboard(5)

returns:

10101
01010
10101
01010
10101

Upvotes: 0

Gaston
Gaston

Reputation: 225

As I understand it, it is simple :

print("stackoverflow")

def chessboard(n):
    finalSentence1 = ""
    finalSentence2 = ""
    for i in range(n): #we add 0 and 1 as much as we have n
        if i%2 == 0: #
            finalSentence1 += "1"
            finalSentence2 += "0"
        else:
            finalSentence1 += "0"
            finalSentence2 += "1"


    for i in range(n): #we print as much as we have n
        if i%2 == 0:
            print(finalSentence1)
        else:
            print(finalSentence2)



chessboard(3)

returns :

stackoverflow
101
010
101

Upvotes: 1

Related Questions