Skip to content

Commit e1eb4bd

Browse files
JKLiang9714keon
authored andcommitted
fix bugs, add descriptions, and add tests (keon#487)
* fix: test_heap, tree/traversal/__init__ * fix a error in longest_non_repeat.py * fix bugs, add notes, and add tests * fix bugs, add descriptions, and add tests * fix bugs, add descriptions, and add tests
1 parent f438cd3 commit e1eb4bd

File tree

4 files changed

+112
-2
lines changed

4 files changed

+112
-2
lines changed

algorithms/backtrack/palindrome_partitioning.py

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
up into palindromic substrings.
44
(There's always at least one way,
55
since one-character substrings are always palindromes.)
6+
7+
ex)
8+
'abcbab' => [['abcba', 'b'], ['a', 'bcb', 'a', 'b'], ['a', 'b', 'c', 'bab'], ['a', 'b', 'c', 'b', 'a', 'b']]
69
"""
710

811

algorithms/backtrack/permute.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def permute(elements):
1919
returns a list with the permuations.
2020
"""
2121
if len(elements) <= 1:
22-
return elements
22+
return [elements]
2323
else:
2424
tmp = []
2525
for perm in permute(elements[1:]):

algorithms/backtrack/subsets.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def backtrack(res, nums, cur, pos):
5252

5353

5454
# Iteratively
55-
def subsets_v2(self, nums):
55+
def subsets_v2(nums):
5656
res = [[]]
5757
for num in sorted(nums):
5858
res += [item+[num] for item in res]

tests/test_backtrack.py

+107
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,14 @@
1313
generate_parenthesis_v1,
1414
generate_parenthesis_v2,
1515
letter_combinations,
16+
palindromic_substrings,
1617
pattern_match,
18+
permute_unique,
19+
permute,
20+
permute_recursive,
21+
subsets_unique,
22+
subsets,
23+
subsets_v2,
1724
)
1825

1926
import unittest
@@ -261,6 +268,106 @@ def test_letter_combinations(self):
261268
self.assertEqual(sorted(letter_combinations(digit2)), sorted(answer2))
262269

263270

271+
class TestPalindromicSubstrings(unittest.TestCase):
272+
273+
def test_palindromic_substrings(self):
274+
string1 = "abc"
275+
answer1 = [['a', 'b', 'c']]
276+
self.assertEqual(palindromic_substrings(string1), sorted(answer1))
277+
278+
string2 = "abcba"
279+
answer2 = [['abcba'], ['a', 'bcb', 'a'], ['a', 'b', 'c', 'b', 'a']]
280+
self.assertEqual(sorted(palindromic_substrings(string2)), sorted(answer2))
281+
282+
string3 = "abcccba"
283+
answer3 = [['abcccba'], ['a', 'bcccb', 'a'], ['a', 'b', 'ccc', 'b', 'a'],
284+
['a', 'b', 'cc', 'c', 'b', 'a'], ['a', 'b', 'c', 'cc', 'b', 'a'],
285+
['a', 'b', 'c', 'c', 'c', 'b', 'a']]
286+
self.assertEqual(sorted(palindromic_substrings(string3)), sorted(answer3))
287+
288+
289+
class TestPermuteUnique(unittest.TestCase):
290+
291+
def test_permute_unique(self):
292+
nums1 = [1, 1, 2]
293+
answer1 = [[2, 1, 1], [1, 2, 1], [1, 1, 2]]
294+
self.assertEqual(sorted(permute_unique(nums1)), sorted(answer1))
295+
296+
nums2 = [1, 2, 1, 3]
297+
answer2 = [[3, 1, 2, 1], [1, 3, 2, 1], [1, 2, 3, 1], [1, 2, 1, 3], [3, 2, 1, 1],
298+
[2, 3, 1, 1], [2, 1, 3, 1], [2, 1, 1, 3], [3, 1, 1, 2], [1, 3, 1, 2], [1, 1, 3, 2], [1, 1, 2, 3]]
299+
self.assertEqual(sorted(permute_unique(nums2)), sorted(answer2))
300+
301+
nums3 = [1, 2, 3]
302+
answer3 = [[3, 2, 1], [2, 3, 1], [2, 1, 3], [3, 1, 2], [1, 3, 2], [1, 2, 3]]
303+
self.assertEqual(sorted(permute_unique(nums3)), sorted(answer3))
304+
305+
306+
class TestPermute(unittest.TestCase):
307+
308+
def test_permute(self):
309+
nums1 = [1, 2, 3, 4]
310+
answer1 = [[1, 2, 3, 4], [2, 1, 3, 4], [2, 3, 1, 4], [2, 3, 4, 1], [1, 3, 2, 4],
311+
[3, 1, 2, 4], [3, 2, 1, 4], [3, 2, 4, 1], [1, 3, 4, 2], [3, 1, 4, 2],
312+
[3, 4, 1, 2], [3, 4, 2, 1], [1, 2, 4, 3], [2, 1, 4, 3], [2, 4, 1, 3],
313+
[2, 4, 3, 1], [1, 4, 2, 3], [4, 1, 2, 3], [4, 2, 1, 3], [4, 2, 3, 1],
314+
[1, 4, 3, 2], [4, 1, 3, 2], [4, 3, 1, 2], [4, 3, 2, 1]]
315+
self.assertEqual(sorted(permute(nums1)), sorted(answer1))
316+
317+
nums2 = [1, 2, 3]
318+
answer2 = [[3, 2, 1], [2, 3, 1], [2, 1, 3], [3, 1, 2], [1, 3, 2], [1, 2, 3]]
319+
self.assertEqual(sorted(permute(nums2)), sorted(answer2))
320+
321+
def test_permute_recursive(self):
322+
nums1 = [1, 2, 3, 4]
323+
answer1 = [[1, 2, 3, 4], [2, 1, 3, 4], [2, 3, 1, 4], [2, 3, 4, 1], [1, 3, 2, 4],
324+
[3, 1, 2, 4], [3, 2, 1, 4], [3, 2, 4, 1], [1, 3, 4, 2], [3, 1, 4, 2],
325+
[3, 4, 1, 2], [3, 4, 2, 1], [1, 2, 4, 3], [2, 1, 4, 3], [2, 4, 1, 3],
326+
[2, 4, 3, 1], [1, 4, 2, 3], [4, 1, 2, 3], [4, 2, 1, 3], [4, 2, 3, 1],
327+
[1, 4, 3, 2], [4, 1, 3, 2], [4, 3, 1, 2], [4, 3, 2, 1]]
328+
self.assertEqual(sorted(permute_recursive(nums1)), sorted(answer1))
329+
330+
nums2 = [1, 2, 3]
331+
answer2 = [[3, 2, 1], [2, 3, 1], [2, 1, 3], [3, 1, 2], [1, 3, 2], [1, 2, 3]]
332+
self.assertEqual(sorted(permute_recursive(nums2)), sorted(answer2))
333+
334+
335+
class TestSubsetsUnique(unittest.TestCase):
336+
337+
def test_subsets_unique(self):
338+
nums1 = [1, 2, 2]
339+
answer1 = [(1, 2), (1,), (1, 2, 2), (2,), (), (2, 2)]
340+
self.assertEqual(sorted(subsets_unique(nums1)), sorted(answer1))
341+
342+
nums2 = [1, 2, 3, 4]
343+
answer2 = [(1, 2), (1, 3), (1, 2, 3, 4), (1,), (2,), (3,), (1, 4), (1, 2, 3),
344+
(4,), (), (2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4), (3, 4), (2, 4)]
345+
self.assertEqual(sorted(subsets_unique(nums2)), sorted(answer2))
346+
347+
348+
class TestSubsets(unittest.TestCase):
349+
350+
def test_subsets(self):
351+
nums1 = [1, 2, 3]
352+
answer1 = [[1, 2, 3], [1, 2], [1, 3], [1], [2, 3], [2], [3], []]
353+
self.assertEqual(sorted(subsets(nums1)), sorted(answer1))
354+
355+
nums2 = [1, 2, 3, 4]
356+
answer2 = [[1, 2, 3, 4], [1, 2, 3], [1, 2, 4], [1, 2], [1, 3, 4],
357+
[1, 3], [1, 4], [1], [2, 3, 4], [2, 3], [2, 4], [2], [3, 4], [3], [4], []]
358+
self.assertEqual(sorted(subsets(nums2)), sorted(answer2))
359+
360+
def test_subsets_v2(self):
361+
nums1 = [1, 2, 3]
362+
answer1 = [[1, 2, 3], [1, 2], [1, 3], [1], [2, 3], [2], [3], []]
363+
self.assertEqual(sorted(subsets_v2(nums1)), sorted(answer1))
364+
365+
nums2 = [1, 2, 3, 4]
366+
answer2 = [[1, 2, 3, 4], [1, 2, 3], [1, 2, 4], [1, 2], [1, 3, 4],
367+
[1, 3], [1, 4], [1], [2, 3, 4], [2, 3], [2, 4], [2], [3, 4], [3], [4], []]
368+
self.assertEqual(sorted(subsets_v2(nums2)), sorted(answer2))
369+
370+
264371
if __name__ == '__main__':
265372

266373
unittest.main()

0 commit comments

Comments
 (0)