3.4 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 6a19b1062d1b153d8ac76d70 | Challenge 322: Connect 3 | 29 | challenge-322 |
--description--
Given a matrix of strings representing pieces on a game grid, determine if any player has three in a row.
- Each cell contains
"R","Y", or""(empty string). - Three in a row means three consecutive non-empty cells of the same type horizontally, vertically, or diagonally.
Return:
- A flat array with the winner and the coordinates of their three winning cells in the format:
["R", [0,2], [1,3], [2,4]]. Coordinates are returned top-to-bottom, then left-to-right. - An empty array if there is no winner.
--hints--
connect_three([["", "", "", ""], ["", "", "", ""], ["", "Y", "", ""], ["Y", "R", "R", "R"]]) should return ["R", [3, 1], [3, 2], [3, 3]].
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(connect_three([["", "", "", ""], ["", "", "", ""], ["", "Y", "", ""], ["Y", "R", "R", "R"]]), ["R", [3, 1], [3, 2], [3, 3]])`)
}})
connect_three([["", "", "", ""], ["", "Y", "Y", ""], ["", "Y", "R", "R"], ["", "Y", "R", "R"]]) should return ["Y", [1, 1], [2, 1], [3, 1]].
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(connect_three([["", "", "", ""], ["", "Y", "Y", ""], ["", "Y", "R", "R"], ["", "Y", "R", "R"]]), ["Y", [1, 1], [2, 1], [3, 1]])`)
}})
connect_three([["", "", "Y", "R"], ["", "Y", "R", "Y"], ["", "R", "Y", "R"], ["", "R", "Y", "R"]]) should return ["R", [0, 3], [1, 2], [2, 1]].
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(connect_three([["", "", "Y", "R"], ["", "Y", "R", "Y"], ["", "R", "Y", "R"], ["", "R", "Y", "R"]]), ["R", [0, 3], [1, 2], [2, 1]])`)
}})
connect_three([["", "Y", "", ""], ["", "Y", "Y", ""], ["", "R", "R", "Y"], ["R", "R", "Y", "R"]]) should return ["Y", [0, 1], [1, 2], [2, 3]].
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(connect_three([["", "Y", "", ""], ["", "Y", "Y", ""], ["", "R", "R", "Y"], ["R", "R", "Y", "R"]]), ["Y", [0, 1], [1, 2], [2, 3]])`)
}})
connect_three([["Y", "R", "R", "Y"], ["R", "Y", "Y", "R"], ["Y", "R", "R", "Y"], ["R", "Y", "Y", "R"]]) should return [].
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(connect_three([["Y", "R", "R", "Y"], ["R", "Y", "Y", "R"], ["Y", "R", "R", "Y"], ["R", "Y", "Y", "R"]]), [])`)
}})
--seed--
--seed-contents--
def connect_three(matrix):
return matrix
--solutions--
def connect_three(matrix):
rows = len(matrix)
cols = len(matrix[0])
directions = [[0, 1], [1, 0], [1, 1], [1, -1]]
for r in range(rows):
for c in range(cols):
piece = matrix[r][c]
if not piece:
continue
for dr, dc in directions:
cells = [[r, c]]
for i in range(1, 3):
nr = r + dr * i
nc = c + dc * i
if nr < 0 or nr >= rows or nc < 0 or nc >= cols:
break
if matrix[nr][nc] != piece:
break
cells.append([nr, nc])
if len(cells) == 3:
cells.sort(key=lambda x: (x[0], x[1]))
return [piece] + cells
return []