Skip to content

Commit 641d30e

Browse files
Merge pull request #30 from SannaPersson/master
Add interval scheduling test
2 parents e1d65b7 + 76054ad commit 641d30e

File tree

4 files changed

+52
-88
lines changed

4 files changed

+52
-88
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ script:
2020
- awesome_bot README.md --allow-dupe --allow-redirect
2121
- python Algorithm_tests/sorting_tests/test_sorting.py
2222
- python Algorithm_tests/graphtheory_tests/bellman_ford_test.py
23-
- python Algorithm_tests/search_tests/test_binarysearch.py
23+
- python Algorithm_tests/other_tests/test_binarysearch.py
2424
- python Algorithm_tests/math_tests/intersection_test.py
2525
- python Algorithm_tests/math_tests/union_test.py
2626
- python Algorithm_tests/cryptology_tests/ceasar_test.py
27+
- python Algorithm_tests/other_tests/test_intervalscheduling.py

Algorithm_tests/other_tests/test_intervalscheduling.py

Lines changed: 35 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -4,97 +4,60 @@
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/other')
7+
sys.path.append('Algorithms/other')
88

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

12-
from binarysearch import binarysearch_iterative, binarysearch_recursive
12+
from interval_scheduling import interval_scheduling
1313

1414

15-
class test_binarysearch(unittest.TestCase):
15+
class test_intervalscheduling(unittest.TestCase):
1616

1717
def setUp(self):
1818
# test cases we wish to run
19-
self.L1 = [1, 3, 5, 8, 10, 12]
20-
self.L1_target = 5
21-
self.L1_correct = True, 2
19+
self.R1 = [(0, 5), (3, 6),(5, 10)]
20+
self.R1_correct = [(0,5), (5,10)]
2221

23-
self.L2 = [1, 3, 5, 8, 10, 12]
24-
self.L2_target = 6
25-
self.L2_correct = False, None
22+
self.R2 = []
23+
self.R2_correct = []
2624

27-
self.L3 = [1,1,1,1,1,1,1,1]
28-
self.L3_target = 1
29-
self.L3_correct = True, (0+len(self.L3)-1)//2
25+
self.R3 = [(0, 3), (3,6), (6,9), (9, 10)]
26+
self.R3_correct = [(0, 3), (3,6), (6,9), (9, 10)]
3027

31-
self.L4 = [1, 3, 6, 11, 16, 21, 25, 27]
32-
self.L4_target = 27
33-
self.L4_correct = True, len(self.L4)-1
28+
self.R4 = [(1, 3), (0, 2), (1, 4), (2, 5)]
29+
self.R4_correct = [(0,2), (2,5)]
3430

35-
self.L5 = [1, 3, 6, 11, 16, 21, 27]
36-
self.L5_target = 1
37-
self.L5_correct = True, 0
31+
self.R5 = [(0,3)]
32+
self.R5_correct = [(0,3)]
3833

39-
self.L6 = []
40-
self.L6_target = 10
41-
self.L6_correct = False, None
4234

43-
self.L7 = [11,12,15,19,23,41,173,298]
44-
self.L7_target = 12
45-
self.L7_correct = True, 1
35+
def test_intervalscheduling_basic(self):
36+
O = []
37+
O = interval_scheduling(self.R1, O)
38+
self.assertEqual(O, self.R1_correct)
4639

40+
def test_intervalscheduling_empty(self):
41+
O = []
42+
O = interval_scheduling(self.R2, O)
43+
self.assertEqual(O, self.R2_correct)
4744

48-
def test_binarysearch_basic(self):
49-
L1_result_iterative = binarysearch_iterative(self.L1, self.L1_target)
50-
L1_result_recursive = binarysearch_recursive(self.L1, self.L1_target, 0, len(self.L1)-1)
45+
def test_intervalscheduling_take_all(self):
46+
O = []
47+
O = interval_scheduling(self.R3, O)
48+
self.assertEqual(O, self.R3_correct)
5149

52-
self.assertEqual(L1_result_iterative, self.L1_correct)
53-
self.assertEqual(L1_result_recursive, self.L1_correct)
50+
def test_intervalscheduling_unsorted(self):
51+
O = []
52+
O = interval_scheduling(self.R4, O)
53+
self.assertEqual(O, self.R4_correct)
5454

55-
def test_binarysearch_nonexistant(self):
56-
L2_result_iterative = binarysearch_iterative(self.L2, self.L2_target)
57-
L2_result_recursive = binarysearch_recursive(self.L2, self.L2_target, 0, len(self.L1)-1)
58-
59-
self.assertEqual(L2_result_iterative, self.L2_correct)
60-
self.assertEqual(L2_result_recursive, self.L2_correct)
61-
62-
def test_binarysearch_identical(self):
63-
L3_result_iterative = binarysearch_iterative(self.L3, self.L3_target)
64-
L3_result_recursive = binarysearch_recursive(self.L3, self.L3_target, 0, len(self.L3) - 1)
65-
66-
self.assertEqual(L3_result_iterative, self.L3_correct)
67-
self.assertEqual(L3_result_recursive, self.L3_correct)
68-
69-
def test_binarysearch_lastvalue(self):
70-
L4_result_iterative = binarysearch_iterative(self.L4, self.L4_target)
71-
L4_result_recursive = binarysearch_recursive(self.L4, self.L4_target, 0, len(self.L4) - 1)
72-
73-
self.assertEqual(L4_result_iterative, self.L4_correct)
74-
self.assertEqual(L4_result_recursive, self.L4_correct)
75-
76-
def test_binarysearch_firstvalue(self):
77-
L5_result_iterative = binarysearch_iterative(self.L5, self.L5_target)
78-
L5_result_recursive = binarysearch_recursive(self.L5, self.L5_target, 0, len(self.L5) - 1)
79-
80-
self.assertEqual(L5_result_iterative, self.L5_correct)
81-
self.assertEqual(L5_result_recursive, self.L5_correct)
82-
83-
def test_binarysearch_empty(self):
84-
L6_result_iterative = binarysearch_iterative(self.L6, self.L6_target)
85-
L6_result_recursive = binarysearch_recursive(self.L6, self.L6_target, 0, len(self.L6) - 1)
86-
87-
self.assertEqual(L6_result_iterative, self.L6_correct)
88-
self.assertEqual(L6_result_recursive, self.L6_correct)
89-
90-
def test_binarysearch_standard(self):
91-
L7_result_iterative = binarysearch_iterative(self.L7, self.L7_target)
92-
L7_result_recursive = binarysearch_recursive(self.L7, self.L7_target, 0, len(self.L7) - 1)
93-
94-
self.assertEqual(L7_result_iterative, self.L7_correct)
95-
self.assertEqual(L7_result_recursive, self.L7_correct)
55+
def test_intervalscheduling_one_element(self):
56+
O = []
57+
O = interval_scheduling(self.R5, O)
58+
self.assertEqual(O, self.R5_correct)
9659

9760

9861
if __name__ == '__main__':
99-
print("Running sorting tests:")
62+
print("Running Interval Scheduling tests:")
10063
unittest.main()
Binary file not shown.

Algorithms/other/interval_scheduling.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,21 @@ def interval_scheduling(R, O):
2020

2121
return O
2222

23-
def interval_scheduling_complicated_version(R, O):
24-
while R: # keep going while R still has elements
25-
(si, fi) = R[0]
26-
O.append((si,fi))
27-
idx = 0
28-
29-
while idx < len(R):
30-
(sj, fj) = R[idx]
31-
32-
if fi > sj:
33-
R.remove(R[idx])
34-
idx -= 1
35-
36-
idx += 1
37-
return O
23+
# def interval_scheduling_complicated_version(R, O):
24+
# while R: # keep going while R still has elements
25+
# (si, fi) = R[0]
26+
# O.append((si,fi))
27+
# idx = 0
28+
#
29+
# while idx < len(R):
30+
# (sj, fj) = R[idx]
31+
#
32+
# if fi > sj:
33+
# R.remove(R[idx])
34+
# idx -= 1
35+
#
36+
# idx += 1
37+
# return O
3838

3939
if __name__ == '__main__':
4040
# run small example

0 commit comments

Comments
 (0)