Skip to content

Commit 361b189

Browse files
committed
chore(algorithms, hash-table): ransom note cleanup
1 parent f40908e commit 361b189

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

algorithms/hash_table/ransom_note/__init__.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ def can_construct(ransom_note: str, magazine: str) -> bool:
2929
# Count the number of occurrences of each letter in the magazine. This will be used to keep track of the number of
3030
# letters we can use when constructing the ransom note
3131
occurrences = Counter(magazine)
32-
# Count the number of letters in the ransom note. This will be used to track how many letters are left when constructing
33-
# the ransom note from the letters in the magazine
34-
count = len(ransom_note)
3532

3633
# Iterate through each letter in the ransom note to check if it is in the magazine
3734
for letter in ransom_note:
@@ -44,11 +41,8 @@ def can_construct(ransom_note: str, magazine: str) -> bool:
4441
# if the letter is in the occurrences, we decrease the count of the occurrences of the letter and the number
4542
# of letters left to construct the ransom note
4643
occurrences[letter] -= 1
47-
count -= 1
4844

49-
# If we have no letters left, then we can construct the ransom note from the letters, else we can't so, we check to
50-
# see if the count equals 0
51-
return count == 0
45+
return True
5246

5347

5448
def can_construct_2(ransom_note: str, magazine: str) -> bool:
@@ -66,7 +60,6 @@ def can_construct_2(ransom_note: str, magazine: str) -> bool:
6660
frequency[char] = 1
6761

6862
for char in ransom_note:
69-
7063
# if the character is not in the hash map or its count is 0, return False
7164
if char not in frequency or frequency[char] == 0:
7265
return False
@@ -75,4 +68,4 @@ def can_construct_2(ransom_note: str, magazine: str) -> bool:
7568
else:
7669
frequency[char] -= 1
7770

78-
return True
71+
return True

algorithms/hash_table/ransom_note/test_ransom_note.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,32 @@
33
from algorithms.hash_table.ransom_note import can_construct, can_construct_2
44

55
RANSOM_NOTE_TEST_CASES = [
6-
("codinginterviewquestions", "aboincsdefoetingvqtniewonoregessnutins", True),
7-
("code", "coingd", False),
8-
("codinginterview", "vieewidingcodinter", True),
9-
("program", "programming", True),
10-
("me", "meme", True),
11-
("a", "b", False),
12-
("aa", "ab", False),
13-
("aa", "aab", True),
6+
(
7+
"long_note_success",
8+
"codinginterviewquestions",
9+
"aboincsdefoetingvqtniewonoregessnutins",
10+
True,
11+
),
12+
("missing_letter", "code", "coingd", False),
13+
("shuffled_letters", "codinginterview", "vieewidingcodinter", True),
14+
("subset_of_magazine", "program", "programming", True),
15+
("repeated_letters", "me", "meme", True),
16+
("single_char_mismatch", "a", "b", False),
17+
("insufficient_repeated_char", "aa", "ab", False),
18+
("sufficient_repeated_char", "aa", "aab", True),
19+
("empty_ransom_note", "", "abc", True),
20+
("empty_magazine", "a", "", False),
1421
]
1522

1623

1724
class RansomNoteTestCase(unittest.TestCase):
1825
@parameterized.expand(RANSOM_NOTE_TEST_CASES)
19-
def test_can_construct(self, ransom_note: str, magazine: str, expected: bool):
26+
def test_can_construct(self, _, ransom_note: str, magazine: str, expected: bool):
2027
actual = can_construct(ransom_note, magazine)
2128
self.assertEqual(expected, actual)
2229

2330
@parameterized.expand(RANSOM_NOTE_TEST_CASES)
24-
def test_can_construct_2(self, ransom_note: str, magazine: str, expected: bool):
31+
def test_can_construct_2(self, _, ransom_note: str, magazine: str, expected: bool):
2532
actual = can_construct_2(ransom_note, magazine)
2633
self.assertEqual(expected, actual)
2734

0 commit comments

Comments
 (0)