From 7319c592ad5656112788b38a0830f2560d91d600 Mon Sep 17 00:00:00 2001 From: Sanna Persson Date: Sat, 28 Mar 2020 15:40:24 +0100 Subject: [PATCH 1/3] add test for median_maintenance --- .../other_tests/test_medianmaintenance.py | 60 +++++++++++++++ .../median_maintenance.cpython-38.pyc | Bin 0 -> 1266 bytes Algorithms/other/median_maintenance.py | 70 ++++++++---------- 3 files changed, 90 insertions(+), 40 deletions(-) create mode 100644 Algorithm_tests/other_tests/test_medianmaintenance.py create mode 100644 Algorithms/other/__pycache__/median_maintenance.cpython-38.pyc diff --git a/Algorithm_tests/other_tests/test_medianmaintenance.py b/Algorithm_tests/other_tests/test_medianmaintenance.py new file mode 100644 index 0000000..01c1e40 --- /dev/null +++ b/Algorithm_tests/other_tests/test_medianmaintenance.py @@ -0,0 +1,60 @@ +# Import packages +import sys +import unittest +# 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') + +# If run from local: +#sys.path.append('../../Algorithms/other') + +from median_maintenance import Maintain_Median + + +class MyTestCase(unittest.TestCase): + def setUp(self): + self.data1 = [1, 3, 8, 5, 10] + self.correct1 = 5 + + self.data2 = [] + self.correct2 = [] + + self.data3 = [10] + self.correct3 = 10 + + self.data4 = [1, 2, 3, 4] + self.correct4 = 2.5 + + self.data5 = [1, 10, 2, 9, 11, 4, 6, 5, 3, 8, 7] + self.correct5 = 6 + + + def test_basic(self): + maintain_median = Maintain_Median() + median = maintain_median.main(self.data1) + self.assertEqual(median, self.correct1) + + def test_empty(self): + maintain_median = Maintain_Median() + median = maintain_median.main(self.data2) + self.assertEqual(median, self.correct2) + + def test_single(self): + maintain_median = Maintain_Median() + median = maintain_median.main(self.data3) + self.assertEqual(median, self.correct3) + + def test_even(self): + maintain_median = Maintain_Median() + median = maintain_median.main(self.data4) + self.assertEqual(median, self.correct4) + + def test_longer_example(self): + maintain_median = Maintain_Median() + median = maintain_median.main(self.data5) + self.assertEqual(median, self.correct5) + + + +if __name__ == '__main__': + unittest.main() diff --git a/Algorithms/other/__pycache__/median_maintenance.cpython-38.pyc b/Algorithms/other/__pycache__/median_maintenance.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..3dd3489dd11bc885fabd34e10addb768c820996c GIT binary patch literal 1266 zcmZ`&!EO^V5cPPy$u>=sPy)0dAr43o38g=PP!SbKNX-RtSplmhHci@OlX|yOTW!T5 z{Rj>m+9Q9!x9k-ODOZl1c(ci-p$Ka|9^2zLZ~VM(*VZZmZ9Dw@;VZl!JXvloCfn%o zYZxsw4TLXV2`#mAF0^B4;`q`yS{+FHQoFjO%i7Z`d#9SpJ5|eH8_#uoDK55}gO6@@6ocjHw-QL3Y ze6TmN{iHXHw?;|N*muJ`WiVt4CPH%49-m}h5cH#d5(Mj24=(douDrtKoE%M^>o z;oNh|zu+XGyiXI-gj;@)$P0((DXof2Nh12fGfkUfRh-F`QhA_kGLKRzaZHY&CR89H z;yWT-oP;)K&H~e!W9}?g=2?o91=0rIz!C~wB5r}BQfHAByo)nn|MQ9~=VsP-@Q&_5 zh^rllqB@q9o92cur&Y?Ffr+dGKXX~3!_4Dhd=&RAUsqPnr=xL8S$?(|9cMJm=p+|# zX3D%?gJ!H3B#MnqZeuOJ3nQpT>rRzCs>vo*;Wy+4)@sh_{p-U04e@{Z9s?HT&%_h9 zk>c^Ev?JcvAeFd)SbY75&D25BvSU{e-;#C}gXOnV)S`5lgtm^AtTcOa*Q-)o(iQ^Z z{Er5IB|WOj)8;?i5!k_f;*mz-&;&tN34-BB9}U>A2EpM`IGF8NuOMP?vB$r{lHu#L ztj-Pem^VH0l((6$F@Ao{L0;9Am76l&l+~Fs%hrmmV#rFPkNc*ZMnDIT?cmz@L2Uh40r6COc literal 0 HcmV?d00001 diff --git a/Algorithms/other/median_maintenance.py b/Algorithms/other/median_maintenance.py index 078991e..31b4669 100644 --- a/Algorithms/other/median_maintenance.py +++ b/Algorithms/other/median_maintenance.py @@ -14,55 +14,45 @@ import heapq -maxheap = None -minheap = None +class Maintain_Median(object): + def __init__(self): + self.maxheap = [] + self.minheap = [] -def initialize(): - global maxheap, minheap - - maxheap = [] - minheap = [] - - -def medmain_insert(x): - global maxheap, minheap - - if (len(maxheap)==0): - heapq.heappush(maxheap, -x) - else: - m = -maxheap[0] - if x > m: - heapq.heappush(minheap,x) - - if len(minheap) > len(maxheap): - y = heapq.heappop(minheap) - heapq.heappush(maxheap, -y) + def medmain_insert(self, x): + if (len(self.maxheap) == 0): + heapq.heappush(self.maxheap, -x) else: - heapq.heappush(maxheap, -x) - - if len(maxheap) - len(minheap) > 1: - y = -heapq.heappop(maxheap) - heapq.heappush(minheap, y) - - return -maxheap[0] - - -def test(): - data = [1,3,8,5,10] + m = -self.maxheap[0] + if x > m: + heapq.heappush(self.minheap, x) - initialize() + if len(self.minheap) > len(self.maxheap): + y = heapq.heappop(self.minheap) + heapq.heappush(self.maxheap, -y) - for x in data: - median = medmain_insert(x) + else: + heapq.heappush(self.maxheap, -x) - print(median) + if len(self.maxheap) - len(self.minheap) > 1: + y = -heapq.heappop(self.maxheap) + heapq.heappush(self.minheap, y) -heap_low = None -heap_high = None + return (-self.maxheap[0] + self.minheap[0])/2 if len(self.maxheap) == len(self.minheap) else -self.maxheap[0] -test() + def main(self, data): + if len(data) < 1: + return data + for x in data: + median = self.medmain_insert(x) + return median +if __name__ == '__main__': + data = [1, 3, 8, 5, 10] + maintain_median = Maintain_Median() + median = maintain_median.main(data) + print(median) \ No newline at end of file From c2b3a676f0d30bec0d31309aff17de99ac03ea00 Mon Sep 17 00:00:00 2001 From: Sanna Persson Date: Sat, 28 Mar 2020 15:42:38 +0100 Subject: [PATCH 2/3] updated travis file --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index d59aab4..67fc3d0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,3 +25,4 @@ script: - python Algorithm_tests/math_tests/union_test.py - python Algorithm_tests/cryptology_tests/ceasar_test.py - python Algorithm_tests/other_tests/test_intervalscheduling.py + - python Algorithm_tests/other_tests/test_medianmaintenance.py \ No newline at end of file From 9c4b6d5130d8574154bd5f18b785878b7e602438 Mon Sep 17 00:00:00 2001 From: Sanna Persson Date: Sat, 28 Mar 2020 15:50:02 +0100 Subject: [PATCH 3/3] modified travis file --- .travis.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 67fc3d0..f0bb081 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,5 +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 - - python Algorithm_tests/other_tests/test_medianmaintenance.py \ No newline at end of file + - python Algorithm_tests/other_tests/test_intervalscheduling.py \ No newline at end of file