Skip to content
This repository was archived by the owner on Jul 2, 2024. It is now read-only.

Commit d3936e9

Browse files
authored
Brick wall challenge (#21)
"Brick wall" is a basic-level challenge for working with data structures. The original challenge: https://leetcode.com/problems/brick-wall/ There is a rectangular brick wall in front of you with n rows of bricks. The ith row has some number of bricks each of the same height (i.e., one unit) but they can be of different widths. The total width of each row is the same. Draw a vertical line from the top to the bottom and cross the least bricks. If your line goes through the edge of a brick, then the brick is not considered as crossed. You cannot draw a line just along one of the two vertical edges of the wall, in which case the line will obviously cross no bricks. Given the 2D array wall that contains the information about the wall, return the minimum number of crossed bricks after drawing such a vertical line.
1 parent d83d849 commit d3936e9

File tree

8 files changed

+259
-73
lines changed

8 files changed

+259
-73
lines changed

poetry.lock

Lines changed: 72 additions & 72 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ packages = [
3535
{ include = "bisearch", from = "src" },
3636
{ include = "calc", from = "src" },
3737
{ include = "conv_store", from = "src" },
38+
{ include = "datasets", from = "src" },
3839
{ include = "dynamic", from = "src" },
3940
{ include = "sequences", from = "src" },
4041
{ include = "sorting", from = "src" },
4142
{ include = "primes", from = "src" },
42-
{ include = "wrw_game", from = "src" }
43+
{ include = "wrw_game", from = "src" },
4344
]
4445

4546
[tool.poetry.dependencies]

src/datasets/__init__.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
Dataset challenges
3+
==================
4+
5+
"""
6+
7+
__all__ = [
8+
"get_bricks_count",
9+
"get_position_frequency",
10+
"get_least_bricks_position",
11+
"get_least_bricks_count",
12+
]
13+
14+
from datasets.func import (get_bricks_count, get_least_bricks_count,
15+
get_least_bricks_position, get_position_frequency)

src/datasets/func.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
"""
2+
Datasets functions implementation
3+
4+
"""
5+
6+
from typing import Dict, List
7+
8+
9+
# brick wall challenge
10+
def get_bricks_count(structure: List[List[int]]) -> int:
11+
"""Return number of bricks in the wall structure
12+
13+
:param structure: represents wall structure as sequences of integers
14+
:type structure: list[list[int]]
15+
16+
:return: total number of bricks in the entire wall structure
17+
:rtype: int
18+
19+
Usage:
20+
21+
>>> get_bricks_count([[1], [1], [1]]) == 3
22+
>>> get_bricks_count([[1, 1, 1], [1, 1, 1]]) == 6
23+
>>> get_bricks_count([[2, 1, 2], [1, 1, 1, 1, 1]]) == 8
24+
25+
"""
26+
27+
return sum(len(row) for row in structure)
28+
29+
30+
def get_position_frequency(structure: List[List[int]]) -> Dict[int, int]:
31+
"""Return a matrix of position-bricks pairs for a structure
32+
33+
:param structure: represents wall structure as sequences of integers
34+
:type structure: list[list[int]]
35+
36+
:return: the position - frequency matrix
37+
:rtype: dict[int, int]
38+
39+
Usage:
40+
41+
>>> assert get_position_frequency([[1], [1], [1]]) == {}
42+
>>> assert get_position_frequency([[1, 2], [2, 1], [3]]) = {1: 1, 2: 1}
43+
>>> assert get_position_frequency([[1, 1, 1], [1, 1, 1]]) = {1: 2, 2: 2}
44+
45+
"""
46+
47+
structure_matrix = {}
48+
49+
for row in structure:
50+
row_length = 0
51+
for brick_length in row[:-1]:
52+
row_length += brick_length
53+
if row_length not in structure_matrix:
54+
structure_matrix[row_length] = 0
55+
structure_matrix[row_length] += 1
56+
57+
return structure_matrix
58+
59+
60+
def get_least_bricks_position(structure: List[List[int]]) -> int:
61+
"""Return a pointer to the weakest line in the structure
62+
63+
:param structure: represents wall structure as sequences of integers
64+
:type structure: list[list[int]]
65+
66+
:return: the distance from the left edge to the weakest line location
67+
:rtype: int
68+
69+
This function uses helper function ``get_structure_matrix`` to build
70+
the matrix of distances from the left edge of the "wall".
71+
72+
Usage:
73+
74+
>>> assert get_least_bricks_position([[1], [1], [1]]) == 0
75+
>>> assert get_least_bricks_position([[1, 1, 1], [1, 1, 1]]) == 1
76+
77+
"""
78+
79+
matrix = get_position_frequency(structure)
80+
if not matrix:
81+
return 0
82+
83+
return max(matrix, key=matrix.get) # type: ignore
84+
85+
86+
def get_least_bricks_count(structure: List[List[int]]) -> int:
87+
"""Return the least number of bricks in a vertical line
88+
89+
:param structure: represents wall structure as sequences of integers
90+
:type structure: list[list[int]]
91+
92+
:return: minimum number of bricks in a line
93+
:rtype: int
94+
95+
Usage:
96+
97+
>>> assert get_least_bricks_count([[1], [1], [1]]) == 3
98+
>>> assert get_least_bricks_count([[1, 2], [2, 1], [3], [1, 1, 1]]) == 2
99+
>>> assert get_least_bricks_count([[1, 1, 1], [1, 1, 1]]) == 0
100+
101+
"""
102+
103+
max_value: int = 0
104+
matrix = get_position_frequency(structure)
105+
for count in matrix.values():
106+
max_value = max(max_value, count)
107+
108+
return len(structure) - max_value

src/datasets/py.typed

Whitespace-only changes.

tests/datasets/__init__.py

Whitespace-only changes.

tests/datasets/conftest.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import pytest
2+
3+
4+
@pytest.fixture
5+
def small_bricks_wall():
6+
return [[1], [1], [1]]
7+
8+
9+
@pytest.fixture
10+
def bricks_wall():
11+
return [
12+
[5, 5, 3, 4, 1],
13+
[3, 2, 1, 4, 2, 1, 5],
14+
[2, 3, 1, 5, 5, 2],
15+
[3, 4, 3, 4, 3, 1],
16+
[5, 5, 3, 2, 3],
17+
]
18+
19+
20+
@pytest.fixture
21+
def matrix():
22+
return {5: 4, 10: 4, 13: 3, 17: 2, 3: 2, 6: 2, 12: 1, 2: 1, 11: 1, 16: 1,
23+
7: 1, 14: 1, 15: 1}
24+
25+
26+
@pytest.fixture
27+
def position():
28+
return 5
29+
30+
31+
@pytest.fixture
32+
def bricks_count():
33+
return 29
34+
35+
36+
@pytest.fixture
37+
def least_bricks():
38+
return 1

tests/datasets/func_test.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import datasets
2+
3+
4+
def test_brick_counter(bricks_wall, bricks_count):
5+
assert datasets.get_bricks_count(bricks_wall) == bricks_count
6+
7+
8+
def test_matrix_builder(bricks_wall, position):
9+
assert datasets.get_least_bricks_position(bricks_wall) == position
10+
11+
12+
def test_least_bricks(bricks_wall, small_bricks_wall):
13+
assert datasets.get_least_bricks_count(bricks_wall) == 1
14+
assert datasets.get_least_bricks_count(small_bricks_wall) == 3
15+
16+
17+
def test_original_case():
18+
wall = [[1, 2, 2, 1],
19+
[3, 1, 2],
20+
[1, 3, 2],
21+
[2, 4],
22+
[3, 1, 2],
23+
[1, 3, 1, 1]]
24+
assert datasets.get_least_bricks_count(wall) == 2

0 commit comments

Comments
 (0)