Skip to content

Commit b764ca8

Browse files
author
Alex Hagiopol
committed
implement 8.7 and 8.8 python
1 parent 0b29937 commit b764ca8

File tree

4 files changed

+60
-8
lines changed

4 files changed

+60
-8
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ I need help on this project! PRs are very welcome. Here are some ways you can he
5151
5. Chapter 5 - Bit Manipulation: `7 / 7` complete.
5252
6. Chapter 6 - Math and Logic: `0 / 10` complete.
5353
7. Chapter 7 - Object Oriented Design: `0 / 12` complete.
54-
8. Chapter 8 - Recursion and Dynamic Programming: `7 / 14` complete.
54+
8. Chapter 8 - Recursion and Dynamic Programming: `9 / 14` complete.
5555
9. Chapter 9 - System Design and Scalability: N/A
5656
10. Chapter 10 - Sorting and Searching: `0 / 11` complete.
5757
11. Chapter 11 - Testing: N/A
@@ -62,7 +62,7 @@ I need help on this project! PRs are very welcome. Here are some ways you can he
6262
16. Chapter 16 - Moderate: `1 / 26` complete.
6363
17. Chapter 17 - Hard: `3 / 26` complete.
6464

65-
Total: `51 / 148`
65+
Total: `53 / 148`
6666

6767
#### C++ Solutions:
6868
1. Chapter 1 - Arrays and Strings: `1 / 9` complete.
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,18 @@
1-
def permutations_no_dups():
2-
pass
1+
def permutations_no_dups(string):
2+
if len(string) <= 0:
3+
return None
4+
if len(string) == 1:
5+
return [string[0]]
6+
subproblem_permutations = permutations_no_dups(string[1:])
7+
new_permutations = []
8+
for subproblem_permutation in subproblem_permutations:
9+
new_permutations.extend(create_permutations(subproblem_permutation, string[0]))
10+
return new_permutations
11+
12+
13+
def create_permutations(string, inserted_character):
14+
permutations = []
15+
for index in range(0, len(string)):
16+
permutations.append(string[0:index] + inserted_character + string[index:])
17+
permutations.append(string + inserted_character)
18+
return permutations
Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,23 @@
1-
def permutations_with_dups():
2-
pass
1+
def permutations_with_dups(string):
2+
hash_table = {}
3+
permutations = []
4+
for character in string:
5+
if character in hash_table:
6+
hash_table[character] += 1
7+
else:
8+
hash_table[character] = 1
9+
helper('', hash_table, permutations)
10+
return permutations
11+
12+
13+
def helper(string, hash_table, permutations):
14+
if sum(hash_table.values()) <= 0:
15+
permutations.append(string)
16+
else:
17+
for character in hash_table:
18+
local_hash_table = hash_table.copy()
19+
if local_hash_table[character] <= 1:
20+
local_hash_table.pop(character, None)
21+
else:
22+
local_hash_table[character] -= 1
23+
helper(string + character, local_hash_table, permutations)

tests.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -827,10 +827,25 @@ def test_problem_8_6(self):
827827
self.assertEqual(tower2.disks, [5, 4, 3, 2, 1])
828828

829829
def test_problem_8_7(self):
830-
pass
830+
self.assertEqual(set(p_8_7.permutations_no_dups('a')), {'a'})
831+
self.assertEqual(set(p_8_7.permutations_no_dups('al')), {'al', 'la'})
832+
self.assertEqual(set(p_8_7.permutations_no_dups('ale')), {'ela', 'lea', 'lae', 'eal', 'ael', 'ale'})
833+
self.assertEqual(set(p_8_7.permutations_no_dups('alex')), {'xela', 'exla', 'elxa', 'elax',
834+
'xlea', 'lxea', 'lexa', 'leax',
835+
'xlae', 'lxae', 'laxe', 'laex',
836+
'xeal', 'exal', 'eaxl', 'ealx',
837+
'xael', 'axel', 'aexl', 'aelx',
838+
'xale', 'axle', 'alxe', 'alex'})
831839

832840
def test_problem_8_8(self):
833-
pass
841+
self.assertEqual(set(p_8_8.permutations_with_dups('ala')), {'ala', 'laa', 'aal'})
842+
self.assertEqual(set(p_8_8.permutations_with_dups('alaa')), {'aala', 'alaa', 'alaa', 'laaa', 'aala', 'aaal'})
843+
self.assertEqual(set(p_8_8.permutations_with_dups('alex')), {'xela', 'exla', 'elxa', 'elax',
844+
'xlea', 'lxea', 'lexa', 'leax',
845+
'xlae', 'lxae', 'laxe', 'laex',
846+
'xeal', 'exal', 'eaxl', 'ealx',
847+
'xael', 'axel', 'aexl', 'aelx',
848+
'xale', 'axle', 'alxe', 'alex'})
834849

835850
def test_problem_8_9(self):
836851
pass

0 commit comments

Comments
 (0)