66 array_sum_combinations ,
77 unique_array_sum_combinations ,
88 combination_sum ,
9+ get_factors ,
10+ recursive_get_factors ,
911 find_words ,
12+ generate_abbreviations ,
13+ generate_parenthesis_v1 ,
14+ generate_parenthesis_v2 ,
15+ letter_combinations ,
1016 pattern_match ,
1117)
1218
1319import unittest
14- from algorithms .backtrack .generate_parenthesis import *
1520
1621
1722class TestAddOperator (unittest .TestCase ):
@@ -110,6 +115,53 @@ def test_combination_sum(self):
110115 self .assertEqual (combination_sum (candidates2 , target2 ), answer2 )
111116
112117
118+ class TestFactorCombinations (unittest .TestCase ):
119+
120+ def test_get_factors (self ):
121+ target1 = 32
122+ answer1 = [
123+ [2 , 16 ],
124+ [2 , 2 , 8 ],
125+ [2 , 2 , 2 , 4 ],
126+ [2 , 2 , 2 , 2 , 2 ],
127+ [2 , 4 , 4 ],
128+ [4 , 8 ]
129+ ]
130+ self .assertEqual (sorted (get_factors (target1 )), sorted (answer1 ))
131+
132+ target2 = 12
133+ answer2 = [
134+ [2 , 6 ],
135+ [2 , 2 , 3 ],
136+ [3 , 4 ]
137+ ]
138+ self .assertEqual (sorted (get_factors (target2 )), sorted (answer2 ))
139+ self .assertEqual (sorted (get_factors (1 )), [])
140+ self .assertEqual (sorted (get_factors (37 )), [])
141+
142+ def test_recursive_get_factors (self ):
143+ target1 = 32
144+ answer1 = [
145+ [2 , 16 ],
146+ [2 , 2 , 8 ],
147+ [2 , 2 , 2 , 4 ],
148+ [2 , 2 , 2 , 2 , 2 ],
149+ [2 , 4 , 4 ],
150+ [4 , 8 ]
151+ ]
152+ self .assertEqual (sorted (recursive_get_factors (target1 )), sorted (answer1 ))
153+
154+ target2 = 12
155+ answer2 = [
156+ [2 , 6 ],
157+ [2 , 2 , 3 ],
158+ [3 , 4 ]
159+ ]
160+ self .assertEqual (sorted (recursive_get_factors (target2 )), sorted (answer2 ))
161+ self .assertEqual (sorted (recursive_get_factors (1 )), [])
162+ self .assertEqual (sorted (recursive_get_factors (37 )), [])
163+
164+
113165class TestFindWords (unittest .TestCase ):
114166
115167 def test_normal (self ):
@@ -158,6 +210,21 @@ def test_repeat(self):
158210 self .assertTrue (len (find_words (board , words )) == 5 )
159211
160212
213+ class TestGenerateAbbreviations (unittest .TestCase ):
214+ def test_generate_abbreviations (self ):
215+ word1 = "word"
216+ answer1 = ['word' , 'wor1' , 'wo1d' , 'wo2' , 'w1rd' , 'w1r1' , 'w2d' , 'w3' ,
217+ '1ord' , '1or1' , '1o1d' , '1o2' , '2rd' , '2r1' , '3d' , '4' ]
218+ self .assertEqual (sorted (generate_abbreviations (word1 )), sorted (answer1 ))
219+
220+ word2 = "hello"
221+ answer2 = ['hello' , 'hell1' , 'hel1o' , 'hel2' , 'he1lo' , 'he1l1' , 'he2o' ,
222+ 'he3' , 'h1llo' , 'h1ll1' , 'h1l1o' , 'h1l2' , 'h2lo' , 'h2l1' , 'h3o' , 'h4' ,
223+ '1ello' , '1ell1' , '1el1o' , '1el2' , '1e1lo' , '1e1l1' , '1e2o' , '1e3' ,
224+ '2llo' , '2ll1' , '2l1o' , '2l2' , '3lo' , '3l1' , '4o' , '5' ]
225+ self .assertEqual (sorted (generate_abbreviations (word2 )), sorted (answer2 ))
226+
227+
161228class TestPatternMatch (unittest .TestCase ):
162229
163230 def test_pattern_match (self ):
@@ -172,6 +239,7 @@ def test_pattern_match(self):
172239 self .assertTrue (pattern_match (pattern2 , string2 ))
173240 self .assertFalse (pattern_match (pattern3 , string3 ))
174241
242+
175243class TestGenerateParenthesis (unittest .TestCase ):
176244
177245 def test_generate_parenthesis (self ):
@@ -180,7 +248,19 @@ def test_generate_parenthesis(self):
180248 self .assertEqual (generate_parenthesis_v2 (2 ), ['(())' , '()()' ])
181249 self .assertEqual (generate_parenthesis_v2 (3 ), ['((()))' , '(()())' , '(())()' , '()(())' , '()()()' ])
182250
251+
252+ class TestLetterCombinations (unittest .TestCase ):
253+
254+ def test_letter_combinations (self ):
255+ digit1 = "23"
256+ answer1 = ["ad" , "ae" , "af" , "bd" , "be" , "bf" , "cd" , "ce" , "cf" ]
257+ self .assertEqual (sorted (letter_combinations (digit1 )), sorted (answer1 ))
258+
259+ digit2 = "34"
260+ answer2 = ['dg' , 'dh' , 'di' , 'eg' , 'eh' , 'ei' , 'fg' , 'fh' , 'fi' ]
261+ self .assertEqual (sorted (letter_combinations (digit2 )), sorted (answer2 ))
262+
263+
183264if __name__ == '__main__' :
184265
185266 unittest .main ()
186-
0 commit comments