Skip to content

Develop #510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 26 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
10d6575
[CONFIG] [make] coverage/html opens report at the end
Jul 1, 2024
9b53363
[REFACTOR] documentation link added.
Jul 1, 2024
fcaf6d9
[BUGFIX] wrong type hinting fixed. Clean code improvement.
Jun 20, 2024
1365f91
[REFACTOR] wrong tests naming fixed.
Jun 20, 2024
a17b1f3
[REFACTOR] [Hacker Rank] Interview Preparation Kit: Greedy Algorithms…
Jun 22, 2024
550bff6
[REFACTOR] [Hacker Rank] Interview Preparation Kit: Linked Lists: Det…
Jun 22, 2024
75042cf
[REFACTOR] clean code: variable naming
Jul 1, 2024
62d96d7
[REFACTOR] clean code: variable naming
Jul 1, 2024
a7963c0
[REFACTOR] clean code: variable naming
Jul 1, 2024
74031fd
[REFACTOR] clean code: variable naming
Jul 1, 2024
c087e16
[REFACTOR] clean code: variable naming
Jul 1, 2024
e0629f4
[REFACTOR] clean code: variable naming
Jul 1, 2024
e5c24cf
[REFACTOR] clean code: variable naming
Jul 1, 2024
d087727
[REFACTOR] clean code: variable naming
Jul 1, 2024
abf61d2
[REFACTOR] clean code: variable naming
Jul 1, 2024
c8f90e1
[REFACTOR] clean code: variable naming
Jul 1, 2024
116d6fa
[REFACTOR] clean code: variable naming
Jul 1, 2024
9c875b1
[REFACTOR] clean code: variable naming
Jul 1, 2024
c04f2d7
[REFACTOR] clean code: variable naming
Jul 1, 2024
165242d
[REFACTOR] clean code: variable naming
Jul 1, 2024
a2e3bad
[REFACTOR] clean code: variable naming
Jul 1, 2024
305ecbc
[REFACTOR] clean code: mixed naming update.
Jul 1, 2024
83a7fd3
[REFACTOR] clean code: variable naming
Jul 1, 2024
3d579a1
[REFACTOR] clean code: variable naming
Jul 1, 2024
31a6b8f
[REFACTOR] clean code: variable naming
Jul 1, 2024
2672350
[REFACTOR] clean code: variable naming
Jul 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ coverage: test

coverage/html: test
${RUNTIME_TOOL} -m coverage html
open htmlcov/index.html

outdated:
${PACKAGE_TOOL} list --outdated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@
LOGGER = logging.getLogger(__name__)


def array_manipulation(n: int, queries: list[list[int]]) -> int:

result = [0] * (n + 1)
def array_manipulation(n_operations: int, queries: list[list[int]]) -> int:
result = [0] * (n_operations + 1)
maximum = 0

for [a, b, k] in queries:
for [a_start, b_end, k_value] in queries:

LOGGER.debug("start -> %s", result)
for j in range(a, b + 1):
result[j] += k
for i in range(a_start, b_end + 1):
result[i] += k_value
LOGGER.debug("result -> %s", result)

for value in result:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
LOGGER = logging.getLogger(__name__)


def array_manipulation_optimized(n: int, queries: list[list[int]]) -> int:
def array_manipulation_optimized(n_operations: int, queries: list[list[int]]) -> int:
# why adding 2?
# first slot to adjust 1-based index and
# last slot for storing accum_sum result
result = [0] * (n + 2)
result = [0] * (n_operations + 2)
maximum = 0

for [a, b, k] in queries:
for [a_start, b_end, k_value] in queries:
# Prefix
result[a] += k
result[b + 1] -= k
result[a_start] += k_value
result[b_end + 1] -= k_value

accum_sum = 0
for value in result:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ def rot_left_one(group: list[int]) -> list[int]:
return group


def rot_left(group: list[int], d: int) -> list[int]:
def rot_left(group: list[int], d_rotations: int) -> list[int]:

output = group.copy()
i = 1
while i <= d:
while i <= d_rotations:
output = rot_left_one(output)
i += 1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@


def minimum_swaps(group: list[int]) -> int:
q = [i - 1 for i in group]
indexed_group = [i - 1 for i in group]

swaps = 0
index = 0
size = len(q)
size = len(indexed_group)

while index < size:
if q[index] == index:
if indexed_group[index] == index:
index += 1
else:
temp = q[index]
q[index] = q[temp]
q[temp] = temp
temp = indexed_group[index]
indexed_group[index] = indexed_group[temp]
indexed_group[temp] = temp
swaps += 1

return swaps
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# @link Problem definition
# [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1.md]]

from collections import Counter


Expand All @@ -18,16 +21,16 @@ def count_triplets_brute_force(arr: list[int], r: int) -> int:


def count_triplets(arr, r):
a = Counter(arr)
b = Counter()
a_counter = Counter(arr)
b_counter = Counter()
triplets = 0

for i in arr:
j = i // r
k = i * r
a[i] -= 1
if b[j] and a[k] and i % r == 0:
triplets += b[j] * a[k]
b[i] += 1
a_counter[i] -= 1
if b_counter[j] and a_counter[k] and i % r == 0:
triplets += b_counter[j] * a_counter[k]
b_counter[i] += 1

return triplets
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
# @link Problem definition
# [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/ctci-ransom-note.md]]

from typing import Dict
from typing import Dict, List

__YES__ = 'Yes'
__NO__ = 'No'


def check_magazine_compute(magazine: str, note: str) -> bool:
def check_magazine_compute(magazine: List[str], note: List[str]) -> bool:
dictionary: Dict[str, int] = {}

for x in magazine:
word: str

if x in dictionary:
dictionary[x] += 1
for word in magazine:

if word in dictionary:
dictionary[word] += 1
else:
dictionary[x] = 1
dictionary[word] = 1

for x in note:
for word in note:

if x in dictionary and dictionary[x] > 0:
dictionary[x] -= 1
if word in dictionary and dictionary[word] > 0:
dictionary[word] -= 1
else:
return False

return True


def check_magazine(magazine: str, note: str) -> str:
def check_magazine(magazine: List[str], note: List[str]) -> str:
return __YES__ if check_magazine_compute(magazine, note) else __NO__
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
]


class TestSherlockAndAnagrams(unittest.TestCase):
class TestCheckMagazine(unittest.TestCase):

def test_sherlock_and_anagrams(self):
def test_check_magazine(self):

for _, _tt in enumerate(TEST_CASES):

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
]


class Test(unittest.TestCase):
class TestFreqQuery(unittest.TestCase):

def test_freq_query(self):

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
import math


def sherlock_and_anagrams(s: str) -> int:
def sherlock_and_anagrams(s_word: str) -> int:

candidates = {}
size = len(s)
size = len(s_word)

# Calculate all substrings
for i in range(0, size):
for j in range(0, size - i):
substr = s[i:size - j]
substr = s_word[i:size - j]
print(f'i: {i}, {size} size - j: {size - j} | substr: {substr}')

# Add substrings to a candidate list.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
]


class TestSherlockAndAnagrams(unittest.TestCase):
class TestTwoStrings(unittest.TestCase):

def test_sherlock_and_anagrams(self):
def test_two_strings(self):

for _, _tt in enumerate(TEST_CASES):

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,42 @@ class RoadsAndLibraries:
_paths = []
_unions = 0

def __init__(self, n: int, cities: list[list[int]]):
def __init__(self, q_paths: int, cities: list[list[int]]):

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

for path in cities:
a, b = path[0], path[1]
if self.unite(a, b):
city_a, city_b = path[0], path[1]
if self.unite(city_a, city_b):
self._unions += 1

def find(self, a: int):
return a if self._paths[a] < 0 else self.find(self._paths[a])

def unite(self, a: int, b: int) -> bool:
a = self.find(a)
b = self.find(b)
if a == b:
def unite(self, _point_a: int, _point_b: int) -> bool:
point_a = self.find(_point_a)
point_b = self.find(_point_b)
if point_a == point_b:
return False
if self._paths[a] > self._paths[b]:
a, b = b, a
self._paths[a] += self._paths[b]
self._paths[b] = a
if self._paths[point_a] > self._paths[point_b]:
point_a, point_b = point_b, point_a
self._paths[point_a] += self._paths[point_b]
self._paths[point_b] = point_a
return True

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


def roads_and_libraries(n: int, c_lib: int, c_road: int, cities: list[list[int]]) -> int:
def roads_and_libraries(
q_paths: int,
c_lib: int,
c_road: int,
cities: list[list[int]]) -> int:

ral = RoadsAndLibraries(n, cities)
ral = RoadsAndLibraries(q_paths, cities)
unions = ral.get_unions()

ans1 = c_lib * n
ans2 = c_road * unions + c_lib * (n - unions)
ans1 = c_lib * q_paths
ans2 = c_road * unions + c_lib * (q_paths - unions)
return min(ans1, ans2)
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# pylint: enable=line-too-long

# pylint: disable=too-few-public-methods
class Competition:
class Contest:
luck = 0
important = 0

Expand All @@ -17,32 +17,33 @@ def __getitem__(self, item):

def luck_balance(k, contests: list) -> int:

important_competitions: list[Competition] = []
nonimportant_competitions: list[Competition] = []
important_contests: list[Contest] = []
nonimportant_contests: list[Contest] = []

for x in contests:
luck = x[0]
important = x[1]
for contest in contests:
luck = contest[0]
important = contest[1]

if important == 1:
important_competitions.append(Competition(luck=luck, important=important))
important_contests.append(Contest(luck=luck, important=important))
else:
nonimportant_competitions.append(Competition(luck=luck, important=important))
nonimportant_contests.append(Contest(luck=luck, important=important))

important_competitions = sorted(
important_competitions,
important_contests = sorted(
important_contests,
key=lambda contest: (-contest['important'], -contest['luck'])
)

total = 0
size = len(important_competitions)
total: int = 0
size: int = len(important_contests)

for i in range(0, min(k, size)):
total += important_competitions[i].luck
total += important_contests[i].luck

for i in range(min(k, size), size):
total -= important_competitions[i].luck
total -= important_contests[i].luck

for x in nonimportant_competitions:
total += x.luck
for contest in nonimportant_contests:
total += contest.luck

return total
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@
def has_cycle(head: SinglyLinkedListNode) -> bool:
llindex: list[SinglyLinkedListNode] = []

result = False
node = head
resume = True
node: SinglyLinkedListNode | None = head

while node and resume:
while node is not None:
if node in llindex:
resume = False
result = True
return result
return True

llindex.append(node)
node = node.next

return result
return False
Loading