I'm working on one easy question (Leetcode 1275. Find Winner on a Tic Tac Toe Game) and am wondering how I can make the code even cleaner.
class Solution:
def tictactoe(self, moves: List[List[int]]) -> str:
from collections import Counter
def is_win(pos: List[List[int]]) -> bool:
pos = set(tuple(i) for i in pos)
d1, d2 = set([(0, 0), (1, 1), (2, 2)]), set([(0, 2), (1, 1), (2, 0)])
x = Counter(i for i, _ in pos)
y = Counter(i for _, i in pos)
d1 -= set(pos)
d2 -= set(pos)
return (3 in x.values()) or (3 in y.values()) or not d1 or not d2
a, b = moves[::2], moves[1::2]
A, B = is_win(a), is_win(b)
if A:
return 'A'
elif B:
return 'B'
elif not A and not B and (len(moves) == 9):
return 'Draw'
return 'Pending'
tictactoe
with sample value formoves
? \$\endgroup\$ – RomanPerekhrest Dec 9 '19 at 9:43List[int]
instead of the more obviousTuple[int, int]
for(row, col)
is definitely confusing. Leetcode should have defined better types here. \$\endgroup\$ – Roland Illig Dec 9 '19 at 10:19