Skip to content

Commit bd5aae8

Browse files
authored
Implementing suggestions
1 parent 3fe9825 commit bd5aae8

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

matrix/matrix_equalization.py

+21-13
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,47 @@
1-
import sys
1+
from sys import maxsize
22

33

4-
def array_equalization(vector: list[int], k: int) -> int:
4+
def array_equalization(vector: list[int], step_size: int) -> int:
55
"""
6-
76
This algorithm equalizes all elements of the input vector
87
to a common value, by making the minimal number of
9-
"updates" under the constraint of a step size (k)
10-
11-
details: https://www.geeksforgeeks.org/equalize-array-using-array-elements/
8+
"updates" under the constraint of a step size (step_size).
129
1310
>>> array_equalization([1, 1, 6, 2, 4, 6, 5, 1, 7, 2, 2, 1, 7, 2, 2], 4)
1411
4
1512
>>> array_equalization([22, 81, 88, 71, 22, 81, 632, 81, 81, 22, 92], 2)
1613
5
17-
>>> array_equalization([-22, 81, -88, 71, 22, 81, 632, 81, -81, -22, 92], 2)
18-
5
1914
>>> array_equalization([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 5)
2015
0
21-
>>> array_equalization([sys.maxsize for c in range(10)], 3)
22-
0
2316
>>> array_equalization([22, 22, 22, 33, 33, 33], 2)
2417
2
25-
18+
>>> array_equalization([1, 2, 3], 0)
19+
Traceback (most recent call last):
20+
ValueError: Step size must be positive and non-zero.
21+
>>> array_equalization([1, 2, 3], -1)
22+
Traceback (most recent call last):
23+
ValueError: Step size must be positive and non-zero.
24+
>>> array_equalization([1, 2, 3], 0.5)
25+
Traceback (most recent call last):
26+
ValueError: Step size must be an integer.
27+
>>> array_equalization([1, 2, 3], maxsize)
28+
1
2629
"""
30+
if step_size <= 0:
31+
raise ValueError("Step size must be positive and non-zero.")
32+
if not isinstance(step_size, int):
33+
raise ValueError("Step size must be an integer.")
34+
2735
unique_elements = set(vector)
28-
min_updates = sys.maxsize
36+
min_updates = maxsize
2937

3038
for element in unique_elements:
3139
elem_index = 0
3240
updates = 0
3341
while elem_index < len(vector):
3442
if vector[elem_index] != element:
3543
updates += 1
36-
elem_index += k
44+
elem_index += step_size
3745
else:
3846
elem_index += 1
3947
min_updates = min(min_updates, updates)

0 commit comments

Comments
 (0)