Skip to content

Commit 5109cca

Browse files
Merge pull request #34 from AladdinPerzon/format_code
format code using black
2 parents 94f21ff + 61ec610 commit 5109cca

File tree

67 files changed

+972
-723
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+972
-723
lines changed

Algorithm_tests/cryptology_tests/ceasar_test.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,25 @@
44

55
# For importing from different folders
66
# OBS: This is supposed to be done with automated testing, hence relative to folder we want to import from
7-
sys.path.append('Algorithms/cryptology/ceasar_shifting_cipher')
7+
sys.path.append("Algorithms/cryptology/ceasar_shifting_cipher")
88

99
# If run from local:
10-
#sys.path.append('../../Algorithms/cryptology/ceasar_shifting_cipher')
10+
# sys.path.append('../../Algorithms/cryptology/ceasar_shifting_cipher')
1111

1212
from ceasar_shift_cipher import encrypt, decrypt
1313

1414
# Note this is not robust.. but im trying to make it a habit to make some tests.
1515
# Some are better than nothing. But these are not complete at all.
1616
class test_ceasar_cipher(unittest.TestCase):
17-
1817
def setUp(self):
1918
# test cases we wish to run
20-
self.message1 = 'abc'
19+
self.message1 = "abc"
2120
self.shift1 = 3
22-
self.correct_encrypt1 = 'def'
21+
self.correct_encrypt1 = "def"
2322

24-
self.message2 = 'xyz '
23+
self.message2 = "xyz "
2524
self.shift2 = 1
26-
self.correct_encrypt2 = 'yz a'
27-
25+
self.correct_encrypt2 = "yz a"
2826

2927
def test_encryption_message1(self):
3028
encrypted_message1 = encrypt(self.message1, self.shift1)
@@ -42,6 +40,7 @@ def test_decryption_message2(self):
4240
decrypted_message2 = decrypt(self.correct_encrypt2, self.shift2)
4341
self.assertEqual(decrypted_message2, self.message2)
4442

45-
if __name__ == '__main__':
43+
44+
if __name__ == "__main__":
4645
print("Running ceasar cipher tests:")
47-
unittest.main()
46+
unittest.main()

Algorithm_tests/dynamic_programming_tests/knapsack_tests/knapsack_bottomup_test.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33

44
# For importing from different folders
55
# OBS: This is supposed to be done with automated testing, hence relative to folder we want to import from
6-
sys.path.append('Algorithms/dynamic_programming/knapsack')
6+
sys.path.append("Algorithms/dynamic_programming/knapsack")
77

88
# If run from local:
9-
#sys.path.append('../../../Algorithms/dynamic_programming/knapsack/')
9+
# sys.path.append('../../../Algorithms/dynamic_programming/knapsack/')
1010
from knapsack_bottomup import knapsack
1111

12+
1213
class test_KnapSack(unittest.TestCase):
1314
def setUp(self):
1415
self.weights1, self.values1, self.capacity1 = [], [], 100
@@ -23,7 +24,11 @@ def setUp(self):
2324
self.n3 = len(self.weights2)
2425
self.correctvalue3, self.correctitems3 = 0, []
2526

26-
self.weights4, self.values4, self.capacity4 = [1, 2, 4, 2, 5], [5, 3, 5, 3, 2], 5
27+
self.weights4, self.values4, self.capacity4 = (
28+
[1, 2, 4, 2, 5],
29+
[5, 3, 5, 3, 2],
30+
5,
31+
)
2732
self.n4 = len(self.weights4)
2833
self.correctvalue4, self.correctitems4 = 11, [0, 1, 3]
2934

@@ -32,30 +37,41 @@ def setUp(self):
3237
self.correctvalue5, self.correctitems5 = 0, []
3338

3439
def test_noitems(self):
35-
total_value, items = knapsack(self.n1, self.capacity1, self.weights1, self.values1)
40+
total_value, items = knapsack(
41+
self.n1, self.capacity1, self.weights1, self.values1
42+
)
3643
self.assertEqual(self.correctvalue1, total_value)
3744
self.assertEqual(self.correctitems1, items)
3845

3946
def test_singleitem_value(self):
40-
total_value, items = knapsack(self.n2, self.capacity2, self.weights2, self.values2)
47+
total_value, items = knapsack(
48+
self.n2, self.capacity2, self.weights2, self.values2
49+
)
4150
self.assertEqual(self.correctvalue2, total_value)
4251
self.assertEqual(self.correctitems2, items)
4352

4453
def test_negativevalues(self):
45-
total_value, items = knapsack(self.n3, self.capacity3, self.weights3, self.values3)
54+
total_value, items = knapsack(
55+
self.n3, self.capacity3, self.weights3, self.values3
56+
)
4657
self.assertEqual(self.correctvalue3, total_value)
4758
self.assertEqual(self.correctitems3, items)
4859

4960
def test_simpleexample(self):
50-
total_value, items = knapsack(self.n4, self.capacity4, self.weights4, self.values4)
61+
total_value, items = knapsack(
62+
self.n4, self.capacity4, self.weights4, self.values4
63+
)
5164
self.assertEqual(self.correctvalue4, total_value)
5265
self.assertEqual(self.correctitems4, items)
5366

5467
def test_weight_too_heavy(self):
55-
total_value, items = knapsack(self.n5, self.capacity5, self.weights5, self.values5)
68+
total_value, items = knapsack(
69+
self.n5, self.capacity5, self.weights5, self.values5
70+
)
5671
self.assertEqual(self.correctvalue5, total_value)
5772
self.assertEqual(self.correctitems5, items)
5873

59-
if __name__ == '__main__':
74+
75+
if __name__ == "__main__":
6076
print("Running Knapsack tests:")
61-
unittest.main()
77+
unittest.main()

Algorithm_tests/dynamic_programming_tests/sequence_alignment/sequence_alignment_test.py

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,58 @@
33

44
# For importing from different folders
55
# OBS: This is supposed to be done with automated testing, hence relative to folder we want to import from
6-
sys.path.append('Algorithms/dynamic_programming/')
6+
sys.path.append("Algorithms/dynamic_programming/")
77

88
# If run from local:
9-
#sys.path.append('../../../Algorithms/dynamic_programming/')
9+
# sys.path.append('../../../Algorithms/dynamic_programming/')
1010
from sequence_alignment import SequenceAlignment
1111

12+
1213
class test_sequence_alignment(unittest.TestCase):
1314
def setUp(self):
14-
self.x1 = 'ABC'
15-
self.y1 = 'ADC'
15+
self.x1 = "ABC"
16+
self.y1 = "ADC"
1617
self.correct_editstep1 = 1
1718

18-
self.x2 = 'AB'
19-
self.y2 = 'A'
19+
self.x2 = "AB"
20+
self.y2 = "A"
2021
self.correct_editstep2 = 1
2122

22-
self.x3 = 'A'
23-
self.y3 = ''
23+
self.x3 = "A"
24+
self.y3 = ""
2425
self.correct_editstep3 = 1
2526

26-
self.x4 = 'ABC'
27-
self.y4 = 'ABCDE'
27+
self.x4 = "ABC"
28+
self.y4 = "ABCDE"
2829
self.correct_editstep4 = 2
2930

30-
self.x5 = 'ABCKL'
31-
self.y5 = 'ADCE'
31+
self.x5 = "ABCKL"
32+
self.y5 = "ADCE"
3233
self.correct_editstep5 = 3
3334

34-
self.x6 = 'A'*10
35-
self.y6 = ''
35+
self.x6 = "A" * 10
36+
self.y6 = ""
3637
self.correct_editstep6 = 10
3738

38-
self.x7 = ''
39-
self.y7 = 'A' * 10
39+
self.x7 = ""
40+
self.y7 = "A" * 10
4041
self.correct_editstep7 = 10
4142

42-
self.x8 = 'TGACGTGC'
43-
self.y8 = 'TCGACGTCA'
43+
self.x8 = "TGACGTGC"
44+
self.y8 = "TCGACGTCA"
4445
self.correct_editstep8 = 3
4546

46-
self.x9 = 'XYZ'
47-
self.y9 = 'XKZ'
48-
self.correct_solution9 = ['align_X', 'align_K', 'align_Z']
47+
self.x9 = "XYZ"
48+
self.y9 = "XKZ"
49+
self.correct_solution9 = ["align_X", "align_K", "align_Z"]
4950

50-
self.x10 = 'XX'
51-
self.y10 = ''
52-
self.correct_solution10 = ['remove_X', 'remove_X']
51+
self.x10 = "XX"
52+
self.y10 = ""
53+
self.correct_solution10 = ["remove_X", "remove_X"]
5354

54-
self.x11 = ''
55-
self.y11 = 'XX'
56-
self.correct_solution11 = ['insert_X', 'insert_X']
55+
self.x11 = ""
56+
self.y11 = "XX"
57+
self.correct_solution11 = ["insert_X", "insert_X"]
5758

5859
def test_simplecase(self):
5960
sequence_align = SequenceAlignment(self.x1, self.y1)
@@ -110,6 +111,7 @@ def test_findsolution_empty_x(self):
110111
_, solution = sequence_align.alignment()
111112
self.assertEqual(self.correct_solution11, solution)
112113

113-
if __name__ == '__main__':
114+
115+
if __name__ == "__main__":
114116
print("Running Sequence Alignment tests:")
115-
unittest.main()
117+
unittest.main()

Algorithm_tests/dynamic_programming_tests/weighted_interval_scheduling/weighted_interval_scheduling_test.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,36 @@
33

44
# For importing from different folders
55
# OBS: This is supposed to be done with automated testing, hence relative to folder we want to import from
6-
sys.path.append('Algorithms/dynamic_programming/')
6+
sys.path.append("Algorithms/dynamic_programming/")
77

88
# If run from local:
9-
#sys.path.append('../../../Algorithms/dynamic_programming/')
9+
# sys.path.append('../../../Algorithms/dynamic_programming/')
1010
from weighted_interval_scheduling import WeightedIntervalScheduling
1111

12+
1213
class test_weighted_interval_scheduling(unittest.TestCase):
1314
def setUp(self):
1415
self.I1 = []
1516
self.correct_maxweight1 = 0
1617
self.correct_intervals1 = []
1718

18-
self.I2 = [(0,3,10)]
19+
self.I2 = [(0, 3, 10)]
1920
self.correct_maxweight2 = 10
20-
self.correct_intervals2 = [(0,3,10)]
21+
self.correct_intervals2 = [(0, 3, 10)]
2122

22-
self.I3 = [(0,3,5), (2,5,15), (4, 6, 5)]
23+
self.I3 = [(0, 3, 5), (2, 5, 15), (4, 6, 5)]
2324
self.correct_maxweight3 = 15
24-
self.correct_intervals3 = [(2,5,15)]
25+
self.correct_intervals3 = [(2, 5, 15)]
2526

2627
self.I4 = [(0, 3, 5), (3, 5, 15), (5, 7, 5)]
2728
self.correct_maxweight4 = 25
2829
self.correct_intervals4 = [(0, 3, 5), (3, 5, 15), (5, 7, 5)]
2930

3031
self.I5 = [(0, 3, 5), (3, 5, -100), (5, 7, -50)]
3132
self.correct_maxweight5 = 5
32-
self.correct_intervals5 = [(0,3,5)]
33+
self.correct_intervals5 = [(0, 3, 5)]
3334

34-
self.I6 = [(0, 50, 1), (0, 49, 1), (0, 48, 1), (15,20,10)]
35+
self.I6 = [(0, 50, 1), (0, 49, 1), (0, 48, 1), (15, 20, 10)]
3536
self.correct_maxweight6 = 10
3637
self.correct_intervals6 = [(15, 20, 10)]
3738

@@ -101,6 +102,7 @@ def test_earliest_finish_time_not_best(self):
101102
self.assertEqual(self.correct_maxweight9, max_weight)
102103
self.assertEqual(self.correct_intervals9, best_intervals)
103104

104-
if __name__ == '__main__':
105+
106+
if __name__ == "__main__":
105107
print("Running Weighted Interval Scheduling tests:")
106-
unittest.main()
108+
unittest.main()

Algorithm_tests/graphtheory_tests/BFS_test.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,38 @@
55

66
# For importing from different folders
77
# OBS: This is supposed to be done with automated testing, hence relative to folder we want to import from
8-
sys.path.append('Algorithms/graphtheory/breadth-first-search/')
8+
sys.path.append("Algorithms/graphtheory/breadth-first-search/")
99

1010
# If run from local:
11-
#sys.path.append('../../Algorithms/graphtheory/breadth-first-search/')
11+
# sys.path.append('../../Algorithms/graphtheory/breadth-first-search/')
1212
from BFS_queue_iterative import BFS
1313

14-
class test_BFS(unittest.TestCase):
1514

15+
class test_BFS(unittest.TestCase):
1616
def setUp(self):
17-
self.G1 = {1:[2],2:[1,3],3:[2]}
17+
self.G1 = {1: [2], 2: [1, 3], 3: [2]}
1818
self.correct_visited1 = [True] * 3
19-
self.correct_path1 = [1,2,3]
19+
self.correct_path1 = [1, 2, 3]
2020

21-
self.G2 = {1:[2], 2:[1,3,4], 3:[2], 4:[2,5], 5:[4]}
21+
self.G2 = {1: [2], 2: [1, 3, 4], 3: [2], 4: [2, 5], 5: [4]}
2222
self.correct_visited2 = [True] * 5
2323

24-
self.G3 = {1:[2], 2:[1,3,4], 3:[2], 4:[2], 5:[]}
25-
self.correct_visited3 = [True]*4 + [False]
24+
self.G3 = {1: [2], 2: [1, 3, 4], 3: [2], 4: [2], 5: []}
25+
self.correct_visited3 = [True] * 4 + [False]
2626

27-
self.G4 = {1:[2,3,4], 2:[1,3,4], 3:[1,2,4], 4:[1,2,3]}
28-
self.correct_visited4 = [True]*4
27+
self.G4 = {1: [2, 3, 4], 2: [1, 3, 4], 3: [1, 2, 4], 4: [1, 2, 3]}
28+
self.correct_visited4 = [True] * 4
2929

30-
self.G5 = {1:[2,3,4], 2:[1,5], 3:[1,7], 4:[1,6], 5:[2], 6:[4], 7:[3]}
31-
self.correct_visited5 = [True]*7
30+
self.G5 = {
31+
1: [2, 3, 4],
32+
2: [1, 5],
33+
3: [1, 7],
34+
4: [1, 6],
35+
5: [2],
36+
6: [4],
37+
7: [3],
38+
}
39+
self.correct_visited5 = [True] * 7
3240

3341
def test_linear_graph(self):
3442
visited, path = BFS(self.G1, start_node=1)
@@ -64,6 +72,6 @@ def test_breadth_before_depth(self):
6472
self.assertTrue(path.index(4) < path.index(7))
6573

6674

67-
if __name__ == '__main__':
75+
if __name__ == "__main__":
6876
print("Running BFS/DFS tests:")
69-
unittest.main()
77+
unittest.main()

Algorithm_tests/graphtheory_tests/DFS_test.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,33 @@
55

66
# For importing from different folders
77
# OBS: This is supposed to be done with automated testing, hence relative to folder we want to import from
8-
sys.path.append('Algorithms/graphtheory/depth-first-search/')
8+
sys.path.append("Algorithms/graphtheory/depth-first-search/")
99

1010
# If run from local:
11-
#sys.path.append('../../Algorithms/graphtheory/depth-first-search/')
11+
# sys.path.append('../../Algorithms/graphtheory/depth-first-search/')
1212
from DFS_recursive import DFS as DFS_rec
1313
from DFS_stack_iterative import DFS as DFS_stack
1414

15-
class test_DFS(unittest.TestCase):
1615

16+
class test_DFS(unittest.TestCase):
1717
def setUp(self):
18-
self.G1 = {1:[2],2:[1,3],3:[2]}
18+
self.G1 = {1: [2], 2: [1, 3], 3: [2]}
1919
self.correct_visited1 = [True] * 3
20-
self.correct_path1 = [1,2,3]
20+
self.correct_path1 = [1, 2, 3]
2121
self.DFS_recursive_visited1 = [False for i in range(1, len(self.G1) + 1)]
2222

23-
self.G2 = {1:[2], 2:[1,3,4], 3:[2], 4:[2,5], 5:[4]}
23+
self.G2 = {1: [2], 2: [1, 3, 4], 3: [2], 4: [2, 5], 5: [4]}
2424
self.correct_visited2 = [True] * 5
2525
self.DFS_recursive_visited2 = [False for i in range(1, len(self.G2) + 1)]
2626

27-
self.G3 = {1:[2], 2:[1,3,4], 3:[2], 4:[2], 5:[]}
28-
self.correct_visited3 = [True]*4 + [False]
29-
self.DFS_recursive_visited3= [False for i in range(1, len(self.G3) + 1)]
27+
self.G3 = {1: [2], 2: [1, 3, 4], 3: [2], 4: [2], 5: []}
28+
self.correct_visited3 = [True] * 4 + [False]
29+
self.DFS_recursive_visited3 = [False for i in range(1, len(self.G3) + 1)]
3030

31-
self.G4 = {1:[2,3,4], 2:[1,3,4], 3:[1,2,4], 4:[1,2,3]}
32-
self.correct_visited4 = [True]*4
31+
self.G4 = {1: [2, 3, 4], 2: [1, 3, 4], 3: [1, 2, 4], 4: [1, 2, 3]}
32+
self.correct_visited4 = [True] * 4
3333
self.DFS_recursive_visited4 = [False for i in range(1, len(self.G4) + 1)]
3434

35-
3635
def test_linear_graph(self):
3736
visited, path = DFS_stack(self.G1, start_node=1)
3837
self.assertEqual(visited, self.correct_visited1)
@@ -62,6 +61,7 @@ def test_complete_graph(self):
6261
DFS_rec(self.G4, 1, self.DFS_recursive_visited4)
6362
self.assertEqual(self.DFS_recursive_visited4, self.correct_visited4)
6463

65-
if __name__ == '__main__':
64+
65+
if __name__ == "__main__":
6666
print("Running BFS/DFS tests:")
67-
unittest.main()
67+
unittest.main()

0 commit comments

Comments
 (0)