1

Give the position of 8 queens on the chessboard. Print YES if at least one pair of queens hit each other. If not print out NO.

so here's my code but when checking if these queens can hit ea other diagonally, python says

unsupported operand type(s) for Sub: "str" and "str"

a, b = input().split()
c, d = input().split()
e, f = input().split()
g, h = input().split()
i, j = input().split()
k, l = input().split()
m, n = input().split()
o, p = input().split()
#check cross or straight
if a==c or c==e or e==g or g==i or i==k or k==m or m==o or o==a or b==d or d==f or f==h or h==j or j==l or l==n or n==p or p==b:
    print("YES")
#check diagonally
elif a==b and c==d or c==d and e==f or e==f and g==h or g==h and i==j or i==j and k==l or k==l and a==b:
    print("YES")
elif abs(int(a-b))==abs(int(c-d)) or abs(int(c-d))==abs(int(e-f)) or abs(int(e-f))==abs(int(g-h)) or abs(int(g-h))==abs(int(i-j)) or abs(int(i-j)) == abs(int(k-l)) or abs(int(k-l))==abs(int(a-b)):
    print("YES")
else:
    print("NO")

these inputs are splited by spaces (coordination of each queen on the chess board)

example coordination

1 3
4 8
6 1
5 5
2 7
8 6
7 4
3 2
Codeer
  • 82
  • 9

2 Answers2

1

So you'll basically convert everything into a list so it will be easier to check

rows = []
cols = []
for _ in range(8):
    r, c = [int(v) for v in input().split()]
    rows.append(r)
    cols.append(c)
count = 0
for i in range(8):
    for j in range(i+1, 8):
        if rows[i] == rows[j] or cols[i]==cols[j] or abs(rows[i]-rows[j])==abs(cols[i]-cols[j]):
            count += 1
if count==0:
    print("NO")
else:
    print("YES")
    
Meno
  • 120
  • 8
0

Use abs(int(a)-int(b)) instead of abs(int(a-b))

You are trying to convert a-b into an int, but two strings cannot be substracted.

Always look at the error stack, not just the description of the error type:

TypeError                                 Traceback (most recent call last)
<ipython-input-6-23e2f43d3b2f> in <module>()
     13 elif a==b and c==d or c==d and e==f or e==f and g==h or g==h and i==j or i==j and k==l or k==l and a==b:
     14     print("YES")
---> 15 elif abs(int(a-b))==abs(int(c-d)) or abs(int(c-d))==abs(int(e-f)) or abs(int(e-f))==abs(int(g-h)) or abs(int(g-h))==abs(int(i-j)) or abs(int(i-j)) == abs(int(k-l)) or abs(int(k-l))==abs(int(a-b)):
     16     print("YES")
     17 else:

TypeError: unsupported operand type(s) for -: 'str' and 'str'

there you can see the error is happening in the line

---> 15 elif abs(int(a-b))==abs(int(c-d)) or abs(int(c-d))==abs(int(e-f)) or abs(int(e-f))==abs(int(g-h)) or abs(int(g-h))==abs(int(i-j)) or abs(int(i-j)) == abs(int(k-l)) or abs(int(k-l))==abs(int(a-b)):

where the operations involving to strings (and an operand) are the ones of the kind a-b, c-d, etc.

Bonus:

Since it make sense to use the coordinates always as numbers, you could do

def get_coords():
  x, y = input().split()
  x = int(x)
  y = int(y)
  return x, y

a, b = get_coords()
c, d = get_coords()
...

And then the rest of the script becomes a bit shorter and easier to read.

Ignatius Reilly
  • 1,594
  • 2
  • 6
  • 15
  • that's weird, I use it and the error change to unindent doesn't match any outer indent level – Codeer Jul 09 '22 at 12:03
  • Maybe you changed the indentation when editing the code? It typically happens when copy-pasting. If everything seems properly indented, check that you are using spaces, not tabs. – Ignatius Reilly Jul 09 '22 at 16:41