Skip to content

Commit 436c2b4

Browse files
author
Gonzalo Diaz
committed
[REFACTOR] clean code: variable naming
1 parent 2916af8 commit 436c2b4

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

src/hackerrank/interview_preparation_kit/graphs/roads_and_libraries.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ class RoadsAndLibraries:
77
_paths = []
88
_unions = 0
99

10-
def __init__(self, n: int, cities: list[list[int]]):
10+
def __init__(self, q_paths: int, cities: list[list[int]]):
1111

12-
self._paths = [-1 for _ in range(n + 1)]
12+
self._paths = [-1 for _ in range(q_paths + 1)]
1313

1414
for path in cities:
1515
city_a, city_b = path[0], path[1]
@@ -19,26 +19,30 @@ def __init__(self, n: int, cities: list[list[int]]):
1919
def find(self, a: int):
2020
return a if self._paths[a] < 0 else self.find(self._paths[a])
2121

22-
def unite(self, a: int, b: int) -> bool:
23-
a = self.find(a)
24-
b = self.find(b)
25-
if a == b:
22+
def unite(self, _point_a: int, _point_b: int) -> bool:
23+
point_a = self.find(_point_a)
24+
point_b = self.find(_point_b)
25+
if point_a == point_b:
2626
return False
27-
if self._paths[a] > self._paths[b]:
28-
a, b = b, a
29-
self._paths[a] += self._paths[b]
30-
self._paths[b] = a
27+
if self._paths[point_a] > self._paths[point_b]:
28+
point_a, point_b = point_b, point_a
29+
self._paths[point_a] += self._paths[point_b]
30+
self._paths[point_b] = point_a
3131
return True
3232

3333
def get_unions(self) -> int:
3434
return self._unions
3535

3636

37-
def roads_and_libraries(n: int, c_lib: int, c_road: int, cities: list[list[int]]) -> int:
37+
def roads_and_libraries(
38+
q_paths: int,
39+
c_lib: int,
40+
c_road: int,
41+
cities: list[list[int]]) -> int:
3842

39-
ral = RoadsAndLibraries(n, cities)
43+
ral = RoadsAndLibraries(q_paths, cities)
4044
unions = ral.get_unions()
4145

42-
ans1 = c_lib * n
43-
ans2 = c_road * unions + c_lib * (n - unions)
46+
ans1 = c_lib * q_paths
47+
ans2 = c_road * unions + c_lib * (q_paths - unions)
4448
return min(ans1, ans2)

0 commit comments

Comments
 (0)