Available Captures for Rook LeetCode solution in Python

1
class Solution:
2
def numRookCaptures(self, board: List[List[str]]) -> int:
3
# Checking for possible case to the right of Rook
4
def right_position(n, i):
5
List = i[0:n][::-1] # taking list to the right of rook
6
pIndex, bIndex = -1, -1
7
if "p" in List: # Checking if 'p' in list
8
pIndex = List.index("p")
9
if "B" in List: # Checking if 'B' in list
10
bIndex = List.index("B")
11
print(bIndex, pIndex, List)
12
if bIndex == -1 and pIndex > -1: # if list does not have 'B' and have 'p'
13
return True
14
if pIndex == -1: # if list does not have 'p'
15
return False
16
return bIndex > pIndex
17

18
def left_position(n, i):
19
List = i[n + 1 :] # taking list to the right of rook
20
pIndex, bIndex = -1, -1
21
if "p" in List:
22
pIndex = List.index("p")
23
if "B" in List:
24
bIndex = List.index("B")
25
print(bIndex, pIndex, List)
26
if bIndex == -1 and pIndex > -1:
27
return True
28
if pIndex == -1:
29
return False
30
return bIndex > pIndex
31

32
Count = 0
33
# Checking for possibilites in row
34
for i in board:
35
if "R" in i:
36
print(i)
37
n = i.index("R")
38
if left_position(n, i):
39
Count += 1
40
if right_position(n, i):
41
Count += 1
42
Col = []
43
# checking for possibilites in col
44
for i in range(0, len(board)):
45
Col.append(board[i][n]) # taking the elements from the same col of Rook
46
n = Col.index("R")
47
if left_position(n, Col):
48
Count += 1
49
if right_position(n, Col):
50
Count += 1
51
return Count

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0