Initialisierung des Tuplesort implementiert
This commit is contained in:
+94
-32
@@ -1,12 +1,16 @@
|
|||||||
import unittest
|
import unittest
|
||||||
|
import math
|
||||||
|
|
||||||
LEFT = 0
|
LEFT = 0
|
||||||
RIGHT = 1
|
RIGHT = 1
|
||||||
|
|
||||||
class Numbers:
|
class Numbers:
|
||||||
def __init__(self, content):
|
def __init__(self, content:list):
|
||||||
self._content = content
|
self._content = content
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return str(self._content)
|
||||||
|
|
||||||
def isEmpty(self):
|
def isEmpty(self):
|
||||||
return len(self._content) == 0
|
return len(self._content) == 0
|
||||||
|
|
||||||
@@ -37,7 +41,6 @@ class Numbers:
|
|||||||
break
|
break
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
||||||
class Roundabout:
|
class Roundabout:
|
||||||
def __init__(self, rotation = LEFT):
|
def __init__(self, rotation = LEFT):
|
||||||
self.coatHangers = list()
|
self.coatHangers = list()
|
||||||
@@ -89,20 +92,18 @@ class Roundabout:
|
|||||||
|
|
||||||
def getNumTuples(self) -> int:
|
def getNumTuples(self) -> int:
|
||||||
content = list(self.content())
|
content = list(self.content())
|
||||||
if self.rotation == LEFT: #linksdrehend
|
if self.rotation == RIGHT: #rechtssdrehend
|
||||||
content.reverse()
|
content.reverse()
|
||||||
n = Numbers(content)
|
n = Numbers(content)
|
||||||
return n.getNumTuples()
|
return n.getNumTuples()
|
||||||
|
|
||||||
def getTuples(self, numToGet:int) -> list:
|
def getTuples(self, numToGet:int) -> list:
|
||||||
content = list(self.content())
|
content = list(self.content())
|
||||||
if self.rotation == LEFT: #linksdrehend
|
if self.rotation == RIGHT: #rechtsdrehend
|
||||||
content.reverse()
|
content.reverse()
|
||||||
n = Numbers(content)
|
n = Numbers(content)
|
||||||
return n.getTuples(numToGet)
|
return n.getTuples(numToGet)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Switch:
|
class Switch:
|
||||||
def __init__(self, left_roundabout: Roundabout, right_roundabout: Roundabout):
|
def __init__(self, left_roundabout: Roundabout, right_roundabout: Roundabout):
|
||||||
self.lR = left_roundabout
|
self.lR = left_roundabout
|
||||||
@@ -139,7 +140,6 @@ class Switch:
|
|||||||
else:
|
else:
|
||||||
self.rR.add(item)
|
self.rR.add(item)
|
||||||
|
|
||||||
|
|
||||||
class RoundaboutPair:
|
class RoundaboutPair:
|
||||||
def __init__(self, left_roundabout: Roundabout, right_roundabout: Roundabout, switch: Switch):
|
def __init__(self, left_roundabout: Roundabout, right_roundabout: Roundabout, switch: Switch):
|
||||||
self.lR = left_roundabout
|
self.lR = left_roundabout
|
||||||
@@ -158,6 +158,35 @@ class RoundaboutPair:
|
|||||||
item = self.lR.remove()
|
item = self.lR.remove()
|
||||||
self.sw.assign(item)
|
self.sw.assign(item)
|
||||||
|
|
||||||
|
def initTupleSort(self):
|
||||||
|
if self.lR.isEmpty():
|
||||||
|
return False
|
||||||
|
|
||||||
|
nleft = Numbers(self.lR.content()).getNumTuples()
|
||||||
|
nright = Numbers(self.rR.content()).getNumTuples()
|
||||||
|
nTarget = math.ceil((nleft + nright)/2)
|
||||||
|
|
||||||
|
if (nleft+nright) % 2 == 0: # even number
|
||||||
|
addOn = 0
|
||||||
|
else:
|
||||||
|
addOn = 1
|
||||||
|
# bei ungerader Anzahl von Bügelträgern sollte der linke immer die Mehrheit besitzen
|
||||||
|
# also z.B. 5 - 4
|
||||||
|
# bei einem geradzahligen Tupel, sollten nach der Initialisierung auf beiden Seiten diesselbe Anzahl von Tupel stehen
|
||||||
|
|
||||||
|
|
||||||
|
while (nleft != nright+addOn):
|
||||||
|
|
||||||
|
if nleft > nright:
|
||||||
|
before = self.doPassageLeftItem()
|
||||||
|
else:
|
||||||
|
before = self.doPassageRightItem()
|
||||||
|
nleft = Numbers(self.lR.content()).getNumTuples()
|
||||||
|
nright = Numbers(self.rR.content()).getNumTuples()
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def sortStepTupleSort(self):
|
def sortStepTupleSort(self):
|
||||||
before = 0
|
before = 0
|
||||||
while True:
|
while True:
|
||||||
@@ -230,20 +259,16 @@ class RoundaboutPair:
|
|||||||
if self.lR.isEmpty():
|
if self.lR.isEmpty():
|
||||||
break
|
break
|
||||||
|
|
||||||
def initTupleSort(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def show(self):
|
def show(self):
|
||||||
return (self.lR.content(), self.rR.content())
|
return (self.lR.content(), self.rR.content())
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class TestObjectMethods(unittest.TestCase):
|
class TestObjectMethods(unittest.TestCase):
|
||||||
|
|
||||||
def test_order(self):
|
def test_order(self):
|
||||||
# tupel werden von rechts nach links bestimmt.
|
# tupel werden von rechts nach links bestimmt.
|
||||||
# Jede Zahl, die kleiner als ihr Vorgänger ist, ist ein neuer Start eines Tupels
|
# Jede Zahl, die kleiner als ihr Vorgänger ist, ist ein neuer Start eines Tupels
|
||||||
n1 = Numbers([1, 2, 3, 4, 5, 6])
|
n1 = Numbers([1, 2, 3, 4, 5, 6])
|
||||||
|
self.assertEqual(repr(n1), "[1, 2, 3, 4, 5, 6]")
|
||||||
self.assertEqual(n1.getNumTuples(), 6)
|
self.assertEqual(n1.getNumTuples(), 6)
|
||||||
self.assertEqual(n1.getTuples(1), [6])
|
self.assertEqual(n1.getTuples(1), [6])
|
||||||
self.assertEqual(n1.getTuples(2), [5, 6])
|
self.assertEqual(n1.getTuples(2), [5, 6])
|
||||||
@@ -269,7 +294,6 @@ class TestObjectMethods(unittest.TestCase):
|
|||||||
self.assertEqual(n5.getTuples(2), [10, 5, 4, 16])
|
self.assertEqual(n5.getTuples(2), [10, 5, 4, 16])
|
||||||
self.assertEqual(n5.getTuples(3), [30, 2, 1, 10, 5, 4, 16])
|
self.assertEqual(n5.getTuples(3), [30, 2, 1, 10, 5, 4, 16])
|
||||||
|
|
||||||
|
|
||||||
def test_roundabout(self):
|
def test_roundabout(self):
|
||||||
k1 = Roundabout()
|
k1 = Roundabout()
|
||||||
k1.addList([1, 2, 3])
|
k1.addList([1, 2, 3])
|
||||||
@@ -303,13 +327,38 @@ class TestObjectMethods(unittest.TestCase):
|
|||||||
self.assertEqual(k5.isEmpty(), False)
|
self.assertEqual(k5.isEmpty(), False)
|
||||||
self.assertEqual(k5.getSmallestItem(), 1)
|
self.assertEqual(k5.getSmallestItem(), 1)
|
||||||
|
|
||||||
k6 = Roundabout()
|
def test_roundabout_tuples(self):
|
||||||
self.assertEqual(k6.isEmpty(), True)
|
|
||||||
k6.addList([1, 2, 3, 4, 5, 6])
|
|
||||||
self.assertEqual(k6.content(), [6, 5, 4, 3, 2, 1])
|
|
||||||
|
|
||||||
self.assertEqual(k6.getNumTuples(), 6)
|
k1 = Roundabout(LEFT)
|
||||||
self.assertEqual(k6.getTuples(3), [1, 2, 3])
|
self.assertEqual(k1.isEmpty(), True)
|
||||||
|
k1.addList([1, 2, 3, 4])
|
||||||
|
# in a left rotationg roundabout: one tuple is read as ascending from right to left
|
||||||
|
self.assertEqual(k1.content(), [4, 3, 2, 1])
|
||||||
|
# this is one tuple
|
||||||
|
self.assertEqual(k1.getNumTuples(), 1)
|
||||||
|
|
||||||
|
k1.clear()
|
||||||
|
k1.addList([4, 3, 2, 1])
|
||||||
|
# these are 4 tuples:
|
||||||
|
self.assertEqual(k1.content(), [1, 2, 3, 4])
|
||||||
|
self.assertEqual(k1.getNumTuples(), 4)
|
||||||
|
|
||||||
|
self.assertEqual(k1.getTuples(2), [3, 4])
|
||||||
|
self.assertEqual(k1.getTuples(3), [2, 3, 4])
|
||||||
|
|
||||||
|
|
||||||
|
k2 = Roundabout(RIGHT)
|
||||||
|
self.assertEqual(k2.isEmpty(), True)
|
||||||
|
k2.addList([1, 2, 3, 4, 5, 6])
|
||||||
|
# in a right rotationg roundabout: read tuples from left to right
|
||||||
|
self.assertEqual(k2.content(), [1, 2, 3, 4, 5, 6])
|
||||||
|
self.assertEqual(k2.getNumTuples(), 1)
|
||||||
|
k2.clear()
|
||||||
|
k2.addList([6, 5, 4, 3, 2, 1])
|
||||||
|
self.assertEqual(k2.getNumTuples(), 6)
|
||||||
|
|
||||||
|
self.assertEqual(k2.getTuples(3), [4, 5, 6])
|
||||||
|
self.assertEqual(k2.getTuples(5), [2, 3, 4, 5, 6])
|
||||||
|
|
||||||
|
|
||||||
def test_switch(self):
|
def test_switch(self):
|
||||||
@@ -324,7 +373,7 @@ class TestObjectMethods(unittest.TestCase):
|
|||||||
def test_pair(self):
|
def test_pair(self):
|
||||||
lK = Roundabout()
|
lK = Roundabout()
|
||||||
lK.addList([6, 5, 4, 3, 2, 1])
|
lK.addList([6, 5, 4, 3, 2, 1])
|
||||||
rK = Roundabout(1)
|
rK = Roundabout(RIGHT)
|
||||||
sw = Switch(lK, rK)
|
sw = Switch(lK, rK)
|
||||||
p = RoundaboutPair(lK, rK, sw)
|
p = RoundaboutPair(lK, rK, sw)
|
||||||
self.assertEqual(lK.content(), [1, 2, 3, 4, 5, 6])
|
self.assertEqual(lK.content(), [1, 2, 3, 4, 5, 6])
|
||||||
@@ -346,7 +395,7 @@ class TestObjectMethods(unittest.TestCase):
|
|||||||
def test_picksort(self):
|
def test_picksort(self):
|
||||||
lK = Roundabout()
|
lK = Roundabout()
|
||||||
lK.addList([6, 5, 4, 3, 2, 1])
|
lK.addList([6, 5, 4, 3, 2, 1])
|
||||||
rK = Roundabout(1)
|
rK = Roundabout(RIGHT)
|
||||||
sw = Switch(lK, rK)
|
sw = Switch(lK, rK)
|
||||||
p = RoundaboutPair(lK, rK, sw)
|
p = RoundaboutPair(lK, rK, sw)
|
||||||
self.assertEqual(lK.content(), [1, 2, 3, 4, 5, 6])
|
self.assertEqual(lK.content(), [1, 2, 3, 4, 5, 6])
|
||||||
@@ -357,29 +406,28 @@ class TestObjectMethods(unittest.TestCase):
|
|||||||
def test_tuplesort_equal(self):
|
def test_tuplesort_equal(self):
|
||||||
lK = Roundabout()
|
lK = Roundabout()
|
||||||
lK.addList([3, 2, 1])
|
lK.addList([3, 2, 1])
|
||||||
rK = Roundabout(1)
|
rK = Roundabout(RIGHT)
|
||||||
rK.addList([6, 5, 4])
|
rK.addList([6, 5, 4])
|
||||||
sw = Switch(lK, rK)
|
sw = Switch(lK, rK)
|
||||||
p = RoundaboutPair(lK, rK, sw)
|
p = RoundaboutPair(lK, rK, sw)
|
||||||
sw.set_to_right()
|
sw.set_to_right()
|
||||||
self.assertEqual(p.show(), ([1, 2, 3], [6, 5, 4]))
|
self.assertEqual(p.show(), ([1, 2, 3], [6, 5, 4])) # -> 3, 6
|
||||||
|
|
||||||
p.sortStepTupleSort()
|
p.sortStepTupleSort()
|
||||||
self.assertEqual(p.show(), ([1, 2], [5, 4, 3, 6]))
|
self.assertEqual(p.show(), ([1, 2], [5, 4, 3, 6])) # -> 2, 5
|
||||||
p.sortStepTupleSort()
|
p.sortStepTupleSort()
|
||||||
self.assertEqual(p.show(), ([5, 2, 1], [4, 3, 6]))
|
self.assertEqual(p.show(), ([5, 2, 1], [4, 3, 6])) # -> 1, 4
|
||||||
p.sortStepTupleSort()
|
p.sortStepTupleSort()
|
||||||
self.assertEqual(p.show(), ([5, 2], [3, 6, 1, 4]))
|
self.assertEqual(p.show(), ([5, 2], [3, 6, 1, 4])) # -> 2, 3, 5, 6
|
||||||
p.sortStepTupleSort()
|
p.sortStepTupleSort()
|
||||||
self.assertEqual(p.show(), ([6, 5, 3, 2], [1, 4]))
|
self.assertEqual(p.show(), ([6, 5, 3, 2], [1, 4])) # -> 1, 2, 3, 4, 5, 6
|
||||||
p.sortStepTupleSort()
|
p.sortStepTupleSort()
|
||||||
self.assertEqual(p.show(), ([], [1, 2, 3, 4, 5, 6]))
|
self.assertEqual(p.show(), ([], [1, 2, 3, 4, 5, 6]))
|
||||||
|
|
||||||
|
|
||||||
def test_tuplesort_stepbystep(self):
|
def test_tuplesort_stepbystep(self):
|
||||||
lK = Roundabout()
|
lK = Roundabout()
|
||||||
lK.addList([6, 5, 4, 3, 2, 1])
|
lK.addList([6, 5, 4, 3, 2, 1])
|
||||||
rK = Roundabout(1)
|
rK = Roundabout(RIGHT)
|
||||||
rK.addList([])
|
rK.addList([])
|
||||||
sw = Switch(lK, rK)
|
sw = Switch(lK, rK)
|
||||||
p = RoundaboutPair(lK, rK, sw)
|
p = RoundaboutPair(lK, rK, sw)
|
||||||
@@ -397,12 +445,11 @@ class TestObjectMethods(unittest.TestCase):
|
|||||||
# p.sortStepTupleSort()
|
# p.sortStepTupleSort()
|
||||||
# self.assertEqual(p.show(), ([], [1, 2, 3, 4, 5, 6]))
|
# self.assertEqual(p.show(), ([], [1, 2, 3, 4, 5, 6]))
|
||||||
|
|
||||||
|
|
||||||
def test_tuplesort_onlyleft(self):
|
def test_tuplesort_onlyleft(self):
|
||||||
# Roundabouts befüllen
|
# Roundabouts befüllen
|
||||||
lK = Roundabout()
|
lK = Roundabout()
|
||||||
lK.addList([1, 2, 3, 4, 5])
|
lK.addList([1, 2, 3, 4, 5])
|
||||||
rK = Roundabout(1)
|
rK = Roundabout(RIGHT)
|
||||||
rK.addList([])
|
rK.addList([])
|
||||||
|
|
||||||
sw = Switch(lK, rK)
|
sw = Switch(lK, rK)
|
||||||
@@ -417,7 +464,7 @@ class TestObjectMethods(unittest.TestCase):
|
|||||||
def test_tuplesort_presorted(self):
|
def test_tuplesort_presorted(self):
|
||||||
lK = Roundabout()
|
lK = Roundabout()
|
||||||
lK.addList([2, 5])
|
lK.addList([2, 5])
|
||||||
rK = Roundabout(1)
|
rK = Roundabout(RIGHT)
|
||||||
rK.addList([3, 6, 1, 4])
|
rK.addList([3, 6, 1, 4])
|
||||||
|
|
||||||
sw = Switch(lK, rK)
|
sw = Switch(lK, rK)
|
||||||
@@ -430,6 +477,21 @@ class TestObjectMethods(unittest.TestCase):
|
|||||||
p.sortStepTupleSort()
|
p.sortStepTupleSort()
|
||||||
self.assertEqual(p.show(), ([], [1, 2, 3, 4, 5, 6] )) # l
|
self.assertEqual(p.show(), ([], [1, 2, 3, 4, 5, 6] )) # l
|
||||||
|
|
||||||
|
def test_tuplesort_init(self):
|
||||||
|
lK = Roundabout()
|
||||||
|
lK.addList([6, 5, 4, 3, 2, 1])
|
||||||
|
rK = Roundabout(RIGHT)
|
||||||
|
rK.addList([])
|
||||||
|
sw = Switch(lK, rK)
|
||||||
|
p = RoundaboutPair(lK, rK, sw)
|
||||||
|
sw.set_to_right()
|
||||||
|
self.assertEqual(p.show(), ([1, 2, 3, 4, 5, 6], []))
|
||||||
|
|
||||||
|
|
||||||
|
p.initTupleSort()
|
||||||
|
|
||||||
|
self.assertEqual(p.show(), ([1, 2, 3], [6, 5, 4]))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|||||||
Reference in New Issue
Block a user