Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

3.4 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
6a0dcd03ee4e68698080ef6b Challenge 307: Zoning Regulations 29 challenge-307

--description--

Given a 2D grid (array of arrays) representing a city's building layout, return the coordinates of all buildings that are violating zoning rules.

Each cell in the grid contains one of the labels from the table below. A building is in violation if any of its (up to) 4 neighbors, horizontal or vertical, are a type it cannot be adjacent to.

Label Type Cannot be adjacent to
"i" industrial "R", "I"
"A" Agricultural "C"
"R" Residential "i", "C"
"I" Institutional "i"
"C" Commercial "R", "A"
"" (empty string) undeveloped no restrictions

Return the coordinates of all violating cells as an array of [row, col] pairs, in any order. If no violations exist, return an empty array.

--hints--

get_zone_violations([["R", "C"], ["", "C"]]) should return [[0, 0], [0, 1]].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(sorted(get_zone_violations([["R", "C"], ["", "C"]])), [[0, 0], [0, 1]])`)
}})

get_zone_violations([["", "i"], ["", "R"], ["R", "I"]]) should return [[0, 1], [1, 1]].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(sorted(get_zone_violations([["", "i"], ["", "R"], ["R", "I"]])), [[0, 1], [1, 1]])`)
}})

get_zone_violations([["A", "i", "C"], ["A", "", "C"], ["R", "R", "I"]]) should return [].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(sorted(get_zone_violations([["A", "i", "C"], ["A", "", "C"], ["R", "R", "I"]])), [])`)
}})

get_zone_violations([["R", "R", "C", "R", "R"], ["R", "I", "C", "", "A"], ["R", "R", "", "i", "A"]]) should return [[0, 1], [0, 2], [0, 3]].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(sorted(get_zone_violations([["R", "R", "C", "R", "R"], ["R", "I", "C", "", "A"], ["R", "R", "", "i", "A"]])), [[0, 1], [0, 2], [0, 3]])`)
}})

get_zone_violations([["R", "A", "A", "", "i", "i"], ["R", "I", "", "C", "i", "i"], ["R", "", "C", "C", "A", "A"], ["R", "R", "C", "I", "R", "R"]]) should return [[2, 3], [2, 4], [3, 1], [3, 2]].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(sorted(get_zone_violations([["R", "A", "A", "", "i", "i"], ["R", "I", "", "C", "i", "i"], ["R", "", "C", "C", "A", "A"], ["R", "R", "C", "I", "R", "R"]])), [[2, 3], [2, 4], [3, 1], [3, 2]])`)
}})

--seed--

--seed-contents--

def get_zone_violations(grid):

    return grid

--solutions--

def get_zone_violations(grid):
    rules = {
        "i": ["R", "I"],
        "A": ["C"],
        "R": ["i", "C"],
        "I": ["i"],
        "C": ["R", "A"],
        "": []
    }

    directions = [[-1, 0], [0, -1], [0, 1], [1, 0]]
    violations = []

    for row in range(len(grid)):
        for col in range(len(grid[row])):
            cell = grid[row][col]
            forbidden = rules[cell]

            for dr, dc in directions:
                nr = row + dr
                nc = col + dc
                if 0 <= nr < len(grid) and 0 <= nc < len(grid[row]):
                    if grid[nr][nc] in forbidden:
                        violations.append([row, col])
                        break

    return violations