Skip to content

Added test median maintenance #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions Algorithm_tests/other_tests/test_medianmaintenance.py
Original file line number Diff line number Diff line change
@@ -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()
Binary file not shown.
70 changes: 30 additions & 40 deletions Algorithms/other/median_maintenance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)