R
R
Rapt2014-04-14 20:12:54
Python
Rapt, 2014-04-14 20:12:54

How to create a (chessboard) matrix in Python?

Task: Set the coordinates of two chess fields and display the same color or not.
Input - 1122 Output - yes
I tried to solve it like this (in the code, do not hit: 95% of qbasik knowledge, the rest is "sugar" from python):

m = [[0] * 8] * 8
md = ['Черный','Белый']
n = 0
for i in range(8):
    for r in range(8):
        if n == 0:
            while n < 1:
                m[i][r] = md[n]
                n += 1
        else:
            while n >= 1:
                m[i][r] = md[n]
                n -= 1
v = input('Введите координаты полей')
if m[int(v[0])][int(v[1])] == m[int(v[2])][int(v[3])]:
    print('Yes')
else:
    print('No')

It turns out not a chessboard, but a "zebra" surface ... I think it could have been done much easier, but the knowledge of this PL is still so-so.

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
V
Vladlen Grachev, 2014-04-14
@Rapt

It's not about the language, it's about the algorithm. You have a complete bust here, which is not needed.

def chess(a,b):
    return ((a[0]+a[1])&1 == (b[0]+b[1])&1);
print 'yes' if chess((1,2),(0,2)) else 'no';

For the cells to be of the same color, the sums of the coordinates of the cells must be either both even or both odd. If the sum of the coordinates of one cell is even and the sum of the coordinates of the other is odd, the cells are of different colors.

T
tsarevfs, 2014-04-14
@tsarevfs

Not sure if this works, but the gist should be clear.

first_black = x1 % 2 == y1 % 2
second_black = x2 % 2 == y2 % 2

print "yes" if first_black == second_black else "no"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question