--- id: 6a0dcd03ee4e68698080ef6b title: "Challenge 307: Zoning Regulations" challengeType: 29 dashedName: 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]]`. ```js ({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]]`. ```js ({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 `[]`. ```js ({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]]`. ```js ({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]]`. ```js ({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-- ```py def get_zone_violations(grid): return grid ``` # --solutions-- ```py 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 ```