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
1348class 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 ):
0 commit comments