From 1f2eccd33bc973b5fcd72b5bfdede5c21d04f629 Mon Sep 17 00:00:00 2001 From: Sanna Persson Date: Sat, 28 Mar 2020 10:08:00 +0100 Subject: [PATCH 1/2] Changed Travis file --- .travis.yml | 2 +- .../other_tests/test_intervalscheduling.py | 39 ++++++++----------- Algorithms/other/interval_scheduling.py | 30 +++++++------- 3 files changed, 32 insertions(+), 39 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9ac4963..c53b331 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,7 +20,7 @@ script: - awesome_bot README.md --allow-dupe --allow-redirect - python Algorithm_tests/sorting_tests/test_sorting.py - python Algorithm_tests/graphtheory_tests/bellman_ford_test.py - - python Algorithm_tests/search_tests/test_binarysearch.py + - python Algorithm_tests/other_tests/test_binarysearch.py - python Algorithm_tests/math_tests/intersection_test.py - python Algorithm_tests/math_tests/union_test.py - python Algorithm_tests/cryptology_tests/ceasar_test.py diff --git a/Algorithm_tests/other_tests/test_intervalscheduling.py b/Algorithm_tests/other_tests/test_intervalscheduling.py index c2cf0a4..66e4e5c 100644 --- a/Algorithm_tests/other_tests/test_intervalscheduling.py +++ b/Algorithm_tests/other_tests/test_intervalscheduling.py @@ -9,13 +9,27 @@ # If run from local: sys.path.append('../../Algorithms/other') -from binarysearch import binarysearch_iterative, binarysearch_recursive +from interval_scheduling import interval_scheduling -class test_binarysearch(unittest.TestCase): +class test_intervalscheduling(unittest.TestCase): def setUp(self): # test cases we wish to run + R1 = [(0, 3), (0, 5), (3, 6),(5, 10)] + R1_correct = [] + r2 = (1, 3) + r3 = (0, 5) + r4 = (3, 6) + r5 = (4, 7) + r6 = (3, 9) + r7 = (5, 10) + r8 = (8, 10) + + R = [r1, r2, r3, r4, r5, r6, r7, r8] + O = [] + O = interval_scheduling(R, O) + self.L1 = [1, 3, 5, 8, 10, 12] self.L1_target = 5 self.L1_correct = True, 2 @@ -24,27 +38,6 @@ def setUp(self): self.L2_target = 6 self.L2_correct = False, None - self.L3 = [1,1,1,1,1,1,1,1] - self.L3_target = 1 - self.L3_correct = True, (0+len(self.L3)-1)//2 - - self.L4 = [1, 3, 6, 11, 16, 21, 25, 27] - self.L4_target = 27 - self.L4_correct = True, len(self.L4)-1 - - self.L5 = [1, 3, 6, 11, 16, 21, 27] - self.L5_target = 1 - self.L5_correct = True, 0 - - self.L6 = [] - self.L6_target = 10 - self.L6_correct = False, None - - self.L7 = [11,12,15,19,23,41,173,298] - self.L7_target = 12 - self.L7_correct = True, 1 - - def test_binarysearch_basic(self): L1_result_iterative = binarysearch_iterative(self.L1, self.L1_target) L1_result_recursive = binarysearch_recursive(self.L1, self.L1_target, 0, len(self.L1)-1) diff --git a/Algorithms/other/interval_scheduling.py b/Algorithms/other/interval_scheduling.py index f8b9852..67d6381 100644 --- a/Algorithms/other/interval_scheduling.py +++ b/Algorithms/other/interval_scheduling.py @@ -20,21 +20,21 @@ def interval_scheduling(R, O): return O -def interval_scheduling_complicated_version(R, O): - while R: # keep going while R still has elements - (si, fi) = R[0] - O.append((si,fi)) - idx = 0 - - while idx < len(R): - (sj, fj) = R[idx] - - if fi > sj: - R.remove(R[idx]) - idx -= 1 - - idx += 1 - return O +# def interval_scheduling_complicated_version(R, O): +# while R: # keep going while R still has elements +# (si, fi) = R[0] +# O.append((si,fi)) +# idx = 0 +# +# while idx < len(R): +# (sj, fj) = R[idx] +# +# if fi > sj: +# R.remove(R[idx]) +# idx -= 1 +# +# idx += 1 +# return O if __name__ == '__main__': # run small example From 76054adb34a7487b8f124049decdc010e42e9430 Mon Sep 17 00:00:00 2001 From: Sanna Persson Date: Sat, 28 Mar 2020 10:34:37 +0100 Subject: [PATCH 2/2] Add interval scheduling test --- .travis.yml | 1 + .../other_tests/test_intervalscheduling.py | 96 ++++++------------ .../interval_scheduling.cpython-38.pyc | Bin 0 -> 805 bytes 3 files changed, 34 insertions(+), 63 deletions(-) create mode 100644 Algorithms/other/__pycache__/interval_scheduling.cpython-38.pyc diff --git a/.travis.yml b/.travis.yml index c53b331..d59aab4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,3 +24,4 @@ script: - python Algorithm_tests/math_tests/intersection_test.py - python Algorithm_tests/math_tests/union_test.py - python Algorithm_tests/cryptology_tests/ceasar_test.py + - python Algorithm_tests/other_tests/test_intervalscheduling.py diff --git a/Algorithm_tests/other_tests/test_intervalscheduling.py b/Algorithm_tests/other_tests/test_intervalscheduling.py index 66e4e5c..a23d7e3 100644 --- a/Algorithm_tests/other_tests/test_intervalscheduling.py +++ b/Algorithm_tests/other_tests/test_intervalscheduling.py @@ -4,10 +4,10 @@ # For importing from different folders # OBS: This is supposed to be done with automated testing, hence relative to folder we want to import from -#sys.path.append('Algorithms/other') +sys.path.append('Algorithms/other') # If run from local: -sys.path.append('../../Algorithms/other') +#sys.path.append('../../Algorithms/other') from interval_scheduling import interval_scheduling @@ -16,78 +16,48 @@ class test_intervalscheduling(unittest.TestCase): def setUp(self): # test cases we wish to run - R1 = [(0, 3), (0, 5), (3, 6),(5, 10)] - R1_correct = [] - r2 = (1, 3) - r3 = (0, 5) - r4 = (3, 6) - r5 = (4, 7) - r6 = (3, 9) - r7 = (5, 10) - r8 = (8, 10) - - R = [r1, r2, r3, r4, r5, r6, r7, r8] - O = [] - O = interval_scheduling(R, O) - - self.L1 = [1, 3, 5, 8, 10, 12] - self.L1_target = 5 - self.L1_correct = True, 2 - - self.L2 = [1, 3, 5, 8, 10, 12] - self.L2_target = 6 - self.L2_correct = False, None - - def test_binarysearch_basic(self): - L1_result_iterative = binarysearch_iterative(self.L1, self.L1_target) - L1_result_recursive = binarysearch_recursive(self.L1, self.L1_target, 0, len(self.L1)-1) - - self.assertEqual(L1_result_iterative, self.L1_correct) - self.assertEqual(L1_result_recursive, self.L1_correct) - - def test_binarysearch_nonexistant(self): - L2_result_iterative = binarysearch_iterative(self.L2, self.L2_target) - L2_result_recursive = binarysearch_recursive(self.L2, self.L2_target, 0, len(self.L1)-1) + self.R1 = [(0, 5), (3, 6),(5, 10)] + self.R1_correct = [(0,5), (5,10)] - self.assertEqual(L2_result_iterative, self.L2_correct) - self.assertEqual(L2_result_recursive, self.L2_correct) + self.R2 = [] + self.R2_correct = [] - def test_binarysearch_identical(self): - L3_result_iterative = binarysearch_iterative(self.L3, self.L3_target) - L3_result_recursive = binarysearch_recursive(self.L3, self.L3_target, 0, len(self.L3) - 1) + self.R3 = [(0, 3), (3,6), (6,9), (9, 10)] + self.R3_correct = [(0, 3), (3,6), (6,9), (9, 10)] - self.assertEqual(L3_result_iterative, self.L3_correct) - self.assertEqual(L3_result_recursive, self.L3_correct) + self.R4 = [(1, 3), (0, 2), (1, 4), (2, 5)] + self.R4_correct = [(0,2), (2,5)] - def test_binarysearch_lastvalue(self): - L4_result_iterative = binarysearch_iterative(self.L4, self.L4_target) - L4_result_recursive = binarysearch_recursive(self.L4, self.L4_target, 0, len(self.L4) - 1) + self.R5 = [(0,3)] + self.R5_correct = [(0,3)] - self.assertEqual(L4_result_iterative, self.L4_correct) - self.assertEqual(L4_result_recursive, self.L4_correct) - def test_binarysearch_firstvalue(self): - L5_result_iterative = binarysearch_iterative(self.L5, self.L5_target) - L5_result_recursive = binarysearch_recursive(self.L5, self.L5_target, 0, len(self.L5) - 1) - - self.assertEqual(L5_result_iterative, self.L5_correct) - self.assertEqual(L5_result_recursive, self.L5_correct) + def test_intervalscheduling_basic(self): + O = [] + O = interval_scheduling(self.R1, O) + self.assertEqual(O, self.R1_correct) - def test_binarysearch_empty(self): - L6_result_iterative = binarysearch_iterative(self.L6, self.L6_target) - L6_result_recursive = binarysearch_recursive(self.L6, self.L6_target, 0, len(self.L6) - 1) + def test_intervalscheduling_empty(self): + O = [] + O = interval_scheduling(self.R2, O) + self.assertEqual(O, self.R2_correct) - self.assertEqual(L6_result_iterative, self.L6_correct) - self.assertEqual(L6_result_recursive, self.L6_correct) + def test_intervalscheduling_take_all(self): + O = [] + O = interval_scheduling(self.R3, O) + self.assertEqual(O, self.R3_correct) - def test_binarysearch_standard(self): - L7_result_iterative = binarysearch_iterative(self.L7, self.L7_target) - L7_result_recursive = binarysearch_recursive(self.L7, self.L7_target, 0, len(self.L7) - 1) + def test_intervalscheduling_unsorted(self): + O = [] + O = interval_scheduling(self.R4, O) + self.assertEqual(O, self.R4_correct) - self.assertEqual(L7_result_iterative, self.L7_correct) - self.assertEqual(L7_result_recursive, self.L7_correct) + def test_intervalscheduling_one_element(self): + O = [] + O = interval_scheduling(self.R5, O) + self.assertEqual(O, self.R5_correct) if __name__ == '__main__': - print("Running sorting tests:") + print("Running Interval Scheduling tests:") unittest.main() \ No newline at end of file diff --git a/Algorithms/other/__pycache__/interval_scheduling.cpython-38.pyc b/Algorithms/other/__pycache__/interval_scheduling.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..de7c31ba0ab906b78433db877c000cd05b6024aa GIT binary patch literal 805 zcmZuvOK;Oa5Z+xsvUydlf}laF6d`f&fmEfu1W{B3=O9tItN_c+PGb|t4!Z`?$`yz9 z%71_(e`&9reCEc9SyzD|b?k5S&Fs89>(||Gi=gy+U*0|O2>EG^b>V0nAoMXRNhG}> zf&i6l&eV}~1e30Cq$gbI3r{wLFS%&Qrr@$An!vyT7d-Hx0UVk$E?P4p+CZQM?Q@2# zBiqnLPH&KNN{Q?olAt?f*lZ;(5{qXDeSoSY?{N;vE{Jr#+8p~+F9bQg zeO2fv&8JbS$HP@T_I2iPQrQ$`M+um5b*>8IN6RH-GH?xjY3K{#A17I&V?))yF_m4L z>0Qyu-nrgDMd&7T8D|~JX@@E6GCT|yQIdsWz?5A_<)A`t`>V}MUi