Skip to content

Commit b1c9167

Browse files
committed
test: add parameterized tests
1 parent 64e4c0e commit b1c9167

4 files changed

Lines changed: 44 additions & 10 deletions

File tree

poetry.lock

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ readme = "README.md"
88

99
[tool.poetry.dependencies]
1010
python = "^3.11"
11+
parameterized = "^0.9.0"
1112

1213
[tool.poetry.group.dev.dependencies]
1314
ruff = "^0.3.5"

pystrings/anagram/group_anagrams/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def group_anagrams(strs: List[str]) -> List[List[str]]:
3535
3636
Let’s see how we can implement the above algorithm:
3737
38-
- For each string, compute a 6-element list. Each element in this list represents the frequency of an English letter
38+
- For each string, compute a 26-element list. Each element in this list represents the frequency of an English letter
3939
in the corresponding string. This frequency count will be represented as a tuple. For example, "abbccc" will be
4040
represented as (1, 2, 3, 0, 0, ..., 0). This mapping will generate identical lists for strings that are anagrams.
4141
@@ -59,7 +59,7 @@ def group_anagrams(strs: List[str]) -> List[List[str]]:
5959
# traversing the list of strings takes O(n) time where n is the number of strings in this list
6060
for word in strs:
6161
count = [0] * 26
62-
for char in word:
62+
for char in word.lower():
6363
index = ord(char) - ord('a')
6464
count[index] += 1
6565

pystrings/anagram/group_anagrams/test_group_anagrams.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,33 @@
11
import unittest
2-
from . import group_anagrams
2+
from parameterized import parameterized
3+
from . import group_anagrams, group_anagrams_naive
34

45

56
class GroupAnagramsTestCase(unittest.TestCase):
7+
@parameterized.expand([
8+
("test_1", ["eat", "beat", "neat", "tea"], [["eat", "tea"], ["beat"], ["neat"]]),
9+
("test_2", ["duel", "dule", "speed", "spede", "deul", "cars"], [["duel", "dule", "deul"], ["speed", "spede"], ["cars"]]),
10+
("test_3", ["eat","tea","tan","ate","nat","bat"], [["eat","tea","ate"],["tan","nat"],["bat"]]),
11+
("test_4", ["word","sword","drow","rowd","iced","dice"], [["word","drow","rowd"],["sword"],["iced","dice"]]),
12+
("test_5", ["eat","drink","sleep","repeat"], [["eat"],["drink"],["sleep"],["repeat"]]),
13+
("test_6", ["hello","ohlle","dark"], [["hello","ohlle"],["dark"]])
14+
])
15+
def test1(self, _, strs, expected):
16+
actual = group_anagrams(strs)
17+
self.assertEqual(actual, expected)
18+
19+
@parameterized.expand([
20+
("test_1", ["eat", "beat", "neat", "tea"], [["eat", "tea"], ["beat"], ["neat"]]),
21+
("test_2", ["duel", "dule", "speed", "spede", "deul", "cars"], [["duel", "dule", "deul"], ["speed", "spede"], ["cars"]]),
22+
("test_3", ["eat","tea","tan","ate","nat","bat"], [["eat","tea","ate"],["tan","nat"],["bat"]]),
23+
("test_4", ["word","sword","drow","rowd","iced","dice"], [["word","drow","rowd"],["sword"],["iced","dice"]]),
24+
("test_5", ["eat","drink","sleep","repeat"], [["eat"],["drink"],["sleep"],["repeat"]]),
25+
("test_6", ["hello","ohlle","dark"], [["hello","ohlle"],["dark"]])
26+
])
27+
def test2(self, _, strs, expected):
28+
actual = group_anagrams_naive(strs)
29+
self.assertEqual(actual, expected)
30+
631
def test_1(self):
732
strs = ["eat", "beat", "neat", "tea"]
833
expected = [["eat", "tea"], ["beat"], ["neat"]]
@@ -39,12 +64,6 @@ def test_6(self):
3964
actual = group_anagrams(strs)
4065
self.assertEqual(expected, actual)
4166

42-
def test_7(self):
43-
strs = ["eat","beat","neat","tea"]
44-
expected = [["eat","tea"],["beat"],["neat"]]
45-
actual = group_anagrams(strs)
46-
self.assertEqual(expected, actual)
47-
4867

4968
if __name__ == '__main__':
5069
unittest.main()

0 commit comments

Comments
 (0)