Skip to content

Commit 0a5589c

Browse files
authored
Merge pull request #650 from sir-gon/feature/2d_array
[Hacker Rank] Interview Preparation Kit: Array: 2D Array - DS. Solved ✅.
2 parents 1f72742 + 4f3c5c9 commit 0a5589c

File tree

4 files changed

+215
-0
lines changed

4 files changed

+215
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# [Arrays: 2D Array - DS](https://www.hackerrank.com/challenges/2d-array)
2+
3+
- Difficulty: ` #easy `
4+
- Category: ` #ProblemSolvingBasic `
5+
6+
Given a 6 × 6 2D Array, `arr`:
7+
8+
```text
9+
1 1 1 0 0 0
10+
0 1 0 0 0 0
11+
1 1 1 0 0 0
12+
0 0 0 0 0 0
13+
0 0 0 0 0 0
14+
0 0 0 0 0 0
15+
```
16+
17+
An hourglass in `A` is a subset of values with indices falling in this pattern
18+
in `arr`'s graphical representation:
19+
20+
```text
21+
a b c
22+
d
23+
e f g
24+
```
25+
26+
There are `16` hourglasses in `arr`.
27+
An hourglass sum is the sum of an hourglass' values.
28+
Calculate the hourglass sum for every hourglass in `arr`,
29+
then print the maximum hourglass sum. The array will always be 6 × 6.
30+
31+
## Example
32+
33+
arr =
34+
35+
```text
36+
-9 -9 -9 1 1 1
37+
0 -9 0 4 3 2
38+
-9 -9 -9 1 2 3
39+
0 0 8 6 6 0
40+
0 0 0 -2 0 0
41+
0 0 1 2 4 0
42+
```
43+
44+
The `16` hourglass sums are:
45+
46+
```text
47+
-63, -34, -9, 12,
48+
-10, 0, 28, 23,
49+
-27, -11, -2, 10,
50+
9, 17, 25, 18
51+
```
52+
53+
The highest hourglass sum is `26` from the hourglass beginning
54+
at row `1`, column `2`:
55+
56+
```text
57+
0 4 3
58+
1
59+
8 6 6
60+
````
61+
62+
**Note**: If you have already solved the Java domain's Java 2D Array challenge,
63+
you may wish to skip this challenge.
64+
65+
## Function Description
66+
67+
Complete the function hourglassSum in the editor below.
68+
69+
hourglassSum has the following parameter(s):
70+
71+
- `int arr[6][6]`: an array of integers
72+
73+
## Returns
74+
75+
- int: the maximum hourglass sum
76+
77+
## Input Format
78+
79+
Each of the `6` lines of inputs `arr[i]` contains space-separated integers `arr[i][j]`.
80+
81+
## Constraints
82+
83+
- $9 \leq arr[i][j] \leq 9$
84+
- $0 \leq i, j \leq 5$
85+
86+
## Output Format
87+
88+
Print the largest (maximum) hourglass sum found in `arr`.
89+
90+
## Sample Input
91+
92+
```text
93+
1 1 1 0 0 0
94+
0 1 0 0 0 0
95+
1 1 1 0 0 0
96+
0 0 2 4 4 0
97+
0 0 0 2 0 0
98+
0 0 1 2 4 0
99+
```
100+
101+
## Sample Output
102+
103+
```text
104+
19
105+
```
106+
107+
## Explanation
108+
109+
`arr` contains the following hourglasses:
110+
111+
```text
112+
111 110 100 000
113+
1 0 0 0
114+
111 110 100 000
115+
116+
010 100 000 000
117+
0 1 0 0
118+
002 024 244 440
119+
120+
111 110 100 000
121+
0 2 4 4
122+
000 002 020 200
123+
124+
002 024 244 440
125+
0 0 2 0
126+
001 012 124 240
127+
```
128+
129+
The hourglass with the maximum sum (`19`) is:
130+
131+
```text
132+
2 4 4
133+
2
134+
1 2 4
135+
```
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# @link Problem definition
2+
# [[docs/hackerrank/interview_preparation_kit/arrays/two_d_array.md]]
3+
4+
import logging
5+
6+
LOGGER = logging.getLogger(__name__)
7+
8+
9+
def gethourglass(arr: list[list[int]], position_x: int, position_y: int) -> list[int]:
10+
result: list[int] = []
11+
12+
# top
13+
result.append(arr[position_x - 1][position_y - 1])
14+
result.append(arr[position_x - 1][position_y])
15+
result.append(arr[position_x - 1][position_y + 1])
16+
17+
# middle
18+
result.append(arr[position_x][position_y])
19+
20+
# bottom
21+
result.append(arr[position_x + 1][position_y - 1])
22+
result.append(arr[position_x + 1][position_y])
23+
result.append(arr[position_x + 1][position_y + 1])
24+
25+
return result
26+
27+
28+
def hourglass_sum(arr: list[list[int]]) -> int | None:
29+
matrix_size: int = len(arr)
30+
31+
matrix_start_index: int = 1
32+
matrix_stop_index: int = matrix_size - 1
33+
34+
max_hourglass_sum: int | None = None
35+
36+
for i in range(matrix_start_index, matrix_stop_index):
37+
for j in range(matrix_start_index, matrix_stop_index):
38+
39+
houglass_values: list[int] = gethourglass(arr, i, j)
40+
this_hourglass_sum: int = sum(houglass_values)
41+
42+
if max_hourglass_sum is None or this_hourglass_sum > max_hourglass_sum:
43+
max_hourglass_sum = this_hourglass_sum
44+
45+
return max_hourglass_sum
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"title": "Sample Test Case 0",
4+
"input": [
5+
[1, 1, 1, 0, 0, 0],
6+
[0, 1, 0, 0, 0, 0],
7+
[1, 1, 1, 0, 0, 0],
8+
[0, 0, 2, 4, 4, 0],
9+
[0, 0, 0, 2, 0, 0],
10+
[0, 0, 1, 2, 4, 0]
11+
],
12+
"expected": 19
13+
}
14+
]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import unittest
2+
from pathlib import Path
3+
4+
from ....hackerrank.lib.loader import load_test_cases
5+
from .two_d_array import hourglass_sum
6+
7+
FILE_PATH = str(Path(__file__).resolve().parent)
8+
9+
TEST_CASES = load_test_cases(
10+
FILE_PATH + '/two_d_array.testcases.json')
11+
12+
13+
class Test2dArray(unittest.TestCase):
14+
15+
def test_hourglass_sum(self):
16+
for _, _tt in enumerate(TEST_CASES):
17+
18+
self.assertEqual(
19+
hourglass_sum(_tt['input']), _tt['expected'],
20+
f"{_} | hourglass_sum({_tt['input']}) must be "
21+
f"=> {_tt['expected']}")

0 commit comments

Comments
 (0)