Skip to content

Commit f84ebac

Browse files
committed
chore: minor linting fixes
1 parent 7c0698a commit f84ebac

File tree

4 files changed

+46
-184
lines changed

4 files changed

+46
-184
lines changed

algorithms/dynamic_programming/word_break/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ it has the properties needed for conversion to dynamic programming.
135135
- **Optimal substructure**: Given an input string ,s, that we want to break up into dictionary words, we find the first
136136
word that matches a word from the dictionary, and then repeat the process for the remaining, shorter input string.
137137
This means that, to solve the problem for input `q`, we need to solve the same problem for `p`, where `p` is at
138-
least one character shorter than`q`. Therefore, this problem obeys the optimal substructure property.
138+
least one character shorter than `q`. Therefore, this problem obeys the optimal substructure property.
139139

140140
- **Overlapping subproblems**: The algorithm solves the same subproblems repeatedly. Consider input string “ancookbook”
141141
and the dictionary [“an”, “book”, “cook”, “cookbook”]. The following is the partial call tree for the naive recursive

algorithms/dynamic_programming/word_break/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def word_break_trie(s: str, word_dict: List[str]) -> List[str]:
3737
# iterate from start_idx to the end of the string
3838
for end_idx in range(start_idx, len(s)):
3939
char = s[end_idx]
40-
index = ord(char) - ord("a")
40+
index = ord(char.lower()) - ord("a")
4141

4242
# check if the current character exists in the trie
4343
if not current_node.children[index]:
@@ -84,7 +84,7 @@ def word_break_dp_tabulation(s: str, word_dict: List[str]) -> List[str]:
8484
List of valid sentences
8585
"""
8686
# Initializing the dp table of size s.length + 1
87-
dp = [[]] * (len(s) + 1)
87+
dp = [[] for _ in range(len(s) + 1)]
8888
# Setting the base case
8989
dp[0] = [""]
9090

@@ -246,7 +246,7 @@ def backtrack(
246246

247247
# Iterate over possible end indices
248248
for end_index in range(start_index + 1, len(sentence) + 1):
249-
word = s[start_index:end_index]
249+
word = sentence[start_index:end_index]
250250
# If the word is in the set, proceed with backtracking
251251
if word in words_set:
252252
current_sentence.append(word)

algorithms/dynamic_programming/word_break/test_word_break.py

Lines changed: 40 additions & 180 deletions
Original file line numberDiff line numberDiff line change
@@ -9,86 +9,51 @@
99
word_break_dp_memoization,
1010
)
1111

12+
WORD_BREAK_TEST_DATA = [
13+
(
14+
"magiclly",
15+
["ag", "al", "icl", "mag", "magic", "ly", "lly"],
16+
["mag icl ly", "magic lly"],
17+
),
18+
(
19+
"raincoats",
20+
["rain", "oats", "coat", "s", "rains", "oat", "coats", "c"],
21+
["rain c oats", "rain c oat s", "rain coats", "rain coat s"],
22+
),
23+
(
24+
"highway",
25+
["crash", "cream", "high", "highway", "low", "way"],
26+
["highway", "high way"],
27+
),
28+
("robocat", ["rob", "cat", "robo", "bo", "b"], ["robo cat"]),
29+
(
30+
"cocomomo",
31+
["co", "mo", "coco", "momo"],
32+
["co co momo", "co co mo mo", "coco momo", "coco mo mo"],
33+
),
34+
(
35+
"catsanddog",
36+
["cat", "cats", "and", "sand", "dog"],
37+
["cats and dog", "cat sand dog"],
38+
),
39+
(
40+
"pineapplepenapple",
41+
["apple", "pen", "applepen", "pine", "pineapple"],
42+
["pine apple pen apple", "pineapple pen apple", "pine applepen apple"],
43+
),
44+
("catsandog", ["cats", "dog", "sand", "and", "cat"], []),
45+
]
46+
1247

1348
class WordBreakTestCases(unittest.TestCase):
14-
@parameterized.expand(
15-
[
16-
(
17-
"magiclly",
18-
["ag", "al", "icl", "mag", "magic", "ly", "lly"],
19-
["mag icl ly", "magic lly"],
20-
),
21-
(
22-
"raincoats",
23-
["rain", "oats", "coat", "s", "rains", "oat", "coats", "c"],
24-
["rain c oats", "rain c oat s", "rain coats", "rain coat s"],
25-
),
26-
(
27-
"highway",
28-
["crash", "cream", "high", "highway", "low", "way"],
29-
["highway", "high way"],
30-
),
31-
("robocat", ["rob", "cat", "robo", "bo", "b"], ["robo cat"]),
32-
(
33-
"cocomomo",
34-
["co", "mo", "coco", "momo"],
35-
["co co momo", "co co mo mo", "coco momo", "coco mo mo"],
36-
),
37-
(
38-
"catsanddog",
39-
["cat", "cats", "and", "sand", "dog"],
40-
["cats and dog", "cat sand dog"],
41-
),
42-
(
43-
"pineapplepenapple",
44-
["apple", "pen", "applepen", "pine", "pineapple"],
45-
["pine apple pen apple", "pineapple pen apple", "pine applepen apple"],
46-
),
47-
("catsandog", ["cats", "dog", "sand", "and", "cat"], []),
48-
]
49-
)
49+
@parameterized.expand(WORD_BREAK_TEST_DATA)
5050
def test_word_break_trie(self, s: str, word_dict: List[str], expected: List[str]):
5151
actual = word_break_trie(s, word_dict)
5252
actual.sort()
5353
expected.sort()
5454
self.assertListEqual(expected, actual)
5555

56-
@parameterized.expand(
57-
[
58-
(
59-
"magiclly",
60-
["ag", "al", "icl", "mag", "magic", "ly", "lly"],
61-
["mag icl ly", "magic lly"],
62-
),
63-
(
64-
"raincoats",
65-
["rain", "oats", "coat", "s", "rains", "oat", "coats", "c"],
66-
["rain c oats", "rain c oat s", "rain coats", "rain coat s"],
67-
),
68-
(
69-
"highway",
70-
["crash", "cream", "high", "highway", "low", "way"],
71-
["highway", "high way"],
72-
),
73-
("robocat", ["rob", "cat", "robo", "bo", "b"], ["robo cat"]),
74-
(
75-
"cocomomo",
76-
["co", "mo", "coco", "momo"],
77-
["co co momo", "co co mo mo", "coco momo", "coco mo mo"],
78-
),
79-
(
80-
"catsanddog",
81-
["cat", "cats", "and", "sand", "dog"],
82-
["cats and dog", "cat sand dog"],
83-
),
84-
(
85-
"pineapplepenapple",
86-
["apple", "pen", "applepen", "pine", "pineapple"],
87-
["pine apple pen apple", "pineapple pen apple", "pine applepen apple"],
88-
),
89-
("catsandog", ["cats", "dog", "sand", "and", "cat"], []),
90-
]
91-
)
56+
@parameterized.expand(WORD_BREAK_TEST_DATA)
9257
def test_word_break_dp_tabulation(
9358
self, s: str, word_dict: List[str], expected: List[str]
9459
):
@@ -97,42 +62,7 @@ def test_word_break_dp_tabulation(
9762
expected.sort()
9863
self.assertListEqual(expected, actual)
9964

100-
@parameterized.expand(
101-
[
102-
(
103-
"magiclly",
104-
["ag", "al", "icl", "mag", "magic", "ly", "lly"],
105-
["mag icl ly", "magic lly"],
106-
),
107-
(
108-
"raincoats",
109-
["rain", "oats", "coat", "s", "rains", "oat", "coats", "c"],
110-
["rain c oats", "rain c oat s", "rain coats", "rain coat s"],
111-
),
112-
(
113-
"highway",
114-
["crash", "cream", "high", "highway", "low", "way"],
115-
["highway", "high way"],
116-
),
117-
("robocat", ["rob", "cat", "robo", "bo", "b"], ["robo cat"]),
118-
(
119-
"cocomomo",
120-
["co", "mo", "coco", "momo"],
121-
["co co momo", "co co mo mo", "coco momo", "coco mo mo"],
122-
),
123-
(
124-
"catsanddog",
125-
["cat", "cats", "and", "sand", "dog"],
126-
["cats and dog", "cat sand dog"],
127-
),
128-
(
129-
"pineapplepenapple",
130-
["apple", "pen", "applepen", "pine", "pineapple"],
131-
["pine apple pen apple", "pineapple pen apple", "pine applepen apple"],
132-
),
133-
("catsandog", ["cats", "dog", "sand", "and", "cat"], []),
134-
]
135-
)
65+
@parameterized.expand(WORD_BREAK_TEST_DATA)
13666
def test_word_break_dp_tabulation_2(
13767
self, s: str, word_dict: List[str], expected: List[str]
13868
):
@@ -141,42 +71,7 @@ def test_word_break_dp_tabulation_2(
14171
expected.sort()
14272
self.assertListEqual(expected, actual)
14373

144-
@parameterized.expand(
145-
[
146-
(
147-
"magiclly",
148-
["ag", "al", "icl", "mag", "magic", "ly", "lly"],
149-
["mag icl ly", "magic lly"],
150-
),
151-
(
152-
"raincoats",
153-
["rain", "oats", "coat", "s", "rains", "oat", "coats", "c"],
154-
["rain c oats", "rain c oat s", "rain coats", "rain coat s"],
155-
),
156-
(
157-
"highway",
158-
["crash", "cream", "high", "highway", "low", "way"],
159-
["highway", "high way"],
160-
),
161-
("robocat", ["rob", "cat", "robo", "bo", "b"], ["robo cat"]),
162-
(
163-
"cocomomo",
164-
["co", "mo", "coco", "momo"],
165-
["co co momo", "co co mo mo", "coco momo", "coco mo mo"],
166-
),
167-
(
168-
"catsanddog",
169-
["cat", "cats", "and", "sand", "dog"],
170-
["cats and dog", "cat sand dog"],
171-
),
172-
(
173-
"pineapplepenapple",
174-
["apple", "pen", "applepen", "pine", "pineapple"],
175-
["pine apple pen apple", "pineapple pen apple", "pine applepen apple"],
176-
),
177-
("catsandog", ["cats", "dog", "sand", "and", "cat"], []),
178-
]
179-
)
74+
@parameterized.expand(WORD_BREAK_TEST_DATA)
18075
def test_word_break_backtrack(
18176
self, s: str, word_dict: List[str], expected: List[str]
18277
):
@@ -185,42 +80,7 @@ def test_word_break_backtrack(
18580
expected.sort()
18681
self.assertListEqual(expected, actual)
18782

188-
@parameterized.expand(
189-
[
190-
(
191-
"magiclly",
192-
["ag", "al", "icl", "mag", "magic", "ly", "lly"],
193-
["mag icl ly", "magic lly"],
194-
),
195-
(
196-
"raincoats",
197-
["rain", "oats", "coat", "s", "rains", "oat", "coats", "c"],
198-
["rain c oats", "rain c oat s", "rain coats", "rain coat s"],
199-
),
200-
(
201-
"highway",
202-
["crash", "cream", "high", "highway", "low", "way"],
203-
["highway", "high way"],
204-
),
205-
("robocat", ["rob", "cat", "robo", "bo", "b"], ["robo cat"]),
206-
(
207-
"cocomomo",
208-
["co", "mo", "coco", "momo"],
209-
["co co momo", "co co mo mo", "coco momo", "coco mo mo"],
210-
),
211-
(
212-
"catsanddog",
213-
["cat", "cats", "and", "sand", "dog"],
214-
["cats and dog", "cat sand dog"],
215-
),
216-
(
217-
"pineapplepenapple",
218-
["apple", "pen", "applepen", "pine", "pineapple"],
219-
["pine apple pen apple", "pineapple pen apple", "pine applepen apple"],
220-
),
221-
("catsandog", ["cats", "dog", "sand", "and", "cat"], []),
222-
]
223-
)
83+
@parameterized.expand(WORD_BREAK_TEST_DATA)
22484
def test_word_break_dp_memoization(
22585
self, s: str, word_dict: List[str], expected: List[str]
22686
):

datastructures/trees/trie/alphabet_trie/alphabet_trie.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ def __init__(self):
66
self.root = AlphabetTrieNode()
77

88
def insert(self, word: str) -> None:
9+
if not word or not all('a' <= char.lower() <= 'z' for char in word):
10+
raise ValueError("Word must contain only English letters (a-z)")
911
node = self.root
1012
for char in word:
1113
index = ord(char.lower()) - ord("a")

0 commit comments

Comments
 (0)