Ejemplo n.º 1
0
def priority_queue_test(a):
    """
    -------------------------------------------------------
    Tests priority queue implementation.
    Use: pq_test(a)
    -------------------------------------------------------
    Preconditions:
        a - list of data (list of ?)
    Postconditions:
        the methods of PriorityQueue are tested for both empty and 
        non-empty priority queues using the data in a:
        empty, insert, remove, peek
    -------------------------------------------------------
    """
    pq = PriorityQueue()

    # tests for the priority queue methods go here
    # print the results of the method calls and verify by hand
    print("Empty pqueue: {}".format(pq.is_empty()))
    for i in a:
        pq.insert(i)
    print("Items inserted into pqueue:")
    for j in pq:
        print("{}".format(j))
    print()
    print("Remove: {}".format(pq.remove()))
    print()
    if not pq.is_empty():
        print("Peek: {}".format(pq.peek()))
    else:
        print("Pqueue is empty.")
    return
Ejemplo n.º 2
0
def prims(g, node):
    """
    -------------------------------------------------------
    an exmplar of prims algorithum
    -------------------------------------------------------
    Preconditions:
       g -  a graph to be navigated
       node - a node to start on, in the graph
    Postconditions:
       prints:
       final - a list of how to navigate the graph at the lowest distance
    -------------------------------------------------------
    """
    final = []
    activated =[]
    pq = PriorityQueue()
    done = False
    first_node = g.get_edges(node)
    for i in range(len(first_node)):
        pq.insert(first_node[i])
    while not done:
        lowest = pq.remove()
        if lowest.end not in activated or lowest.start not in activated:
            activated.append(lowest.start)
            activated.append(lowest.end)
            final.append(lowest)
            node = lowest.end
            next_node = g.get_edges(node)
            for i in range(len(next_node)):
                pq.insert(next_node[i])
        if pq.is_empty() == True:
            done = True
    for i in range(len(final)):
        print(final[i])
def priority_queue_test(a):
    """
    -------------------------------------------------------
    Tests priority queue implementation.
    Use: pq_test(a)
    -------------------------------------------------------
    Preconditions:
        a - list of data (list of ?)
    Postconditions:
        the methods of PriorityQueue are tested for both empty and 
        non-empty priority queues using the data in a:
        empty, insert, remove, peek
    -------------------------------------------------------
    """
    pq = PriorityQueue()

    # tests for the priority queue methods go here
    print("***Is_empty test 1: {}".format(pq.is_empty()))
    print("***Array_to_queue ->")
    array_to_pq(pq, a)
    print("***Is_empty test 2: {}".format(pq.is_empty()))

    print("***Tests remove(): {}".format(pq.remove()))
    print("***Peek test: {}".format(pq.peek()))
    # print the results of the method calls and verify by hand
    print('***Alphabetical test:')
    while not pq.is_empty():
        item = pq.remove()
        print(item)

    return
Ejemplo n.º 4
0
    def setUp(self):
        self.empty_queue_circular = CircularQueue(10)
        full_queue_circular = CircularQueue(SIZE)
        values_circular= create_queue(SIZE,full_queue_circular)
        self.full_queue_circular = full_queue_circular
        self.values_circular=values_circular


        #Priority Queue Stuff
        self.empty_queue_priority = PriorityQueue()
        full_queue_priority = PriorityQueue()
        values_priority= create_queue(SIZE,full_queue_priority)
        self.full_queue_priority = full_queue_priority
        values_priority.sort()
        self.values_priority=values_priority


        #Sorted List
        self.empty_sorted_list = SortedList()
        full_sorted_list = SortedList()
        values_sorted_list= create_queue(SIZE,full_sorted_list)
        self.full_sorted_list = full_sorted_list
        values_sorted_list.sort()
        self.values_sorted_list=values_sorted_list
Ejemplo n.º 5
0
def priority_queue_test(a):
    """
    -------------------------------------------------------
    Tests priority queue implementation.
    Use: pq_test(a)
    -------------------------------------------------------
    Preconditions:
        a - list of data (list of ?)
    Postconditions:
        the methods of PriorityQueue are tested for both empty and 
        non-empty priority queues using the data in a:
        empty, insert, remove, peek
    -------------------------------------------------------
    """
    pq = PriorityQueue()
    for i in a:
        pq.insert(i)
        
    print("Is the queue empty: {}".format(pq.is_empty()))
    print("Removed: {}".format(pq.remove()))
    print("Peek {}".format(pq.peek()))
    
    return
def pq_combine(pq1, pq2):
    """
    -------------------------------------------------------
    Combines contents of two priority queues into a new 
    priority queue. Alternate the values from pq1 and pq2.
    Use: q3 = pq_combine(pq1, pq2)
    -------------------------------------------------------
    Preconditions:
        pq1 - a priority queue (PriorityQueue)
        pq2 - a priority queue (PriorityQueue)
    Postconditions:
        returns
        pq3 - Contents of pq1 and pq2 are moved 
            into pq3 (PriorityQueue)
    -------------------------------------------------------
    """
    pq3 = PriorityQueue()
    while len(pq1) > 0 or len(pq2) > 0:
        if len(pq1) > 0:
            pq3.insert(pq1.remove())
        if len(pq2) > 0:
            pq3.insert(pq2.remove())
    return pq3
Ejemplo n.º 7
0
"""
----------------------------------------------------
q4.py
pq_combine
----------------------------------------------------
Author: Qadeer Assan
ID: 160257370
Email: [email protected]
_updated_="2018-02-01"
----------------------------------------------------
"""
from asgn04 import pq_combine
from priority_queue_array import PriorityQueue
pq1 = PriorityQueue()
pq2 = PriorityQueue()
pq1.insert(0)
pq1.insert(2)
pq2.insert(1)
pq2.insert(3)
pq3 = pq_combine(pq1, pq2)

for i in pq3:
    print(i)

Ejemplo n.º 8
0
class Test(unittest.TestCase):


    def setUp(self):
        self.empty_queue_circular = CircularQueue(10)
        full_queue_circular = CircularQueue(SIZE)
        values_circular= create_queue(SIZE,full_queue_circular)
        self.full_queue_circular = full_queue_circular
        self.values_circular=values_circular


        #Priority Queue Stuff
        self.empty_queue_priority = PriorityQueue()
        full_queue_priority = PriorityQueue()
        values_priority= create_queue(SIZE,full_queue_priority)
        self.full_queue_priority = full_queue_priority
        values_priority.sort()
        self.values_priority=values_priority


        #Sorted List
        self.empty_sorted_list = SortedList()
        full_sorted_list = SortedList()
        values_sorted_list= create_queue(SIZE,full_sorted_list)
        self.full_sorted_list = full_sorted_list
        values_sorted_list.sort()
        self.values_sorted_list=values_sorted_list



    #Circular

    def testCircularQueueIsEmpty_Empty(self):
        actual = self.empty_queue_circular.is_empty()
        self.assertTrue(actual, "CQ is not empty");
        return

    def testCircularQueueIsEmpty_NotEmpty(self):
        actual = self.full_queue_circular.is_empty()
        self.assertFalse(actual, "CQ is empty")
        return

    def testCircularQueueLen_Empty(self):
        expected = 0
        actual = len(self.empty_queue_circular)
        self.assertEqual(expected, actual, "length is incorrect")
        return

    def testCircularQueueLen_NotEmpty(self):
        actual = len(self.full_queue_circular)
        self.assertEqual(SIZE, actual, "length is incorrect")
        return

    def testCircularQueueIsFull_Empty(self):
        actual = self.empty_queue_circular.is_full()
        self.assertFalse(actual, "CQ is full")
        return

    def testCircularQueueIsFull_Full(self):
        actual = self.full_queue_circular.is_full()
        self.assertTrue(actual, "CQ is not full")
        return

    def testCircularQueuePeek_Empty(self):
        actual = self.empty_queue_circular.peek()
        self.assertIsNone(actual, "peeked is None")
        return

    def testCircularQueuePeek_Full(self):
        expected = self.values_circular[0]
        actual = self.full_queue_circular.peek()
        self.assertEqual(expected,actual);
        return

    def testCircularQueueRemove_Empty(self):
        actual = self.empty_queue_circular.remove()
        self.assertIsNone(actual, "peeked is None")
        return

    def testCircularQueueRemove_Full(self):
        expected = self.values_circular[0]
        actual = self.full_queue_circular.remove()
        self.assertEqual(expected,actual);
        self.assertNotEqual(SIZE, len(self.full_queue_circular))
        return

    def testCircularQueueRemove_FullQueueRemoval(self):
        i=0
        while not self.full_queue_circular.is_empty():
            v = self.full_queue_circular.remove()
            self.assertEqual(self.values_circular[i],v)
            i+=1

        self.assertEqual(0, len(self.full_queue_circular))

    #Priority

    def testPriorityQueueIsEmpty_Empty(self):
        expected = True
        actual = self.empty_queue_priority.is_empty()
        self.assertEqual(expected,actual);
        return

    def testPriorityQueueIsEmpty_NotEmpty(self):
        expected = False
        actual = self.full_queue_priority.is_empty()
        self.assertEqual(expected,actual);
        return

    def testPriorityQueuePeek_Empty(self):
        expected = None
        actual = self.empty_queue_priority.peek()
        self.assertEqual(expected,actual);
        return

    def testPriorityQueuePeek_Full(self):
        expected = self.values_priority[0]
        actual = self.full_queue_priority.peek()
        self.assertEqual(expected,actual);
        return

    def testPriorityQueueRemove_Empty(self):
        expected = None
        actual = self.empty_queue_priority.remove()
        self.assertEqual(expected,actual);
        return

    def testPriorityQueueRemove_Full(self):
        expected = self.values_priority[0]
        actual = self.full_queue_priority.remove()
        self.assertEqual(expected,actual);
        self.assertNotEqual(SIZE, len(self.full_queue_priority))
        return

    def testPriorityQueueRemove_FullQueueRemoval(self):
        i=0
        while not self.full_queue_priority.is_empty():
            v = self.full_queue_priority.remove()
            self.assertEqual(self.values_priority[i],v)
            i+=1

        self.assertEqual(0, len(self.full_queue_priority))

    #Sorted List
    def testSortedList_is_empty_Empty(self):
        expected = True
        actual = self.empty_sorted_list.is_empty()
        self.assertEqual(expected,actual)

        return

    def testSortedList_getitem_Empty(self):
        expected = None
        actual = self.empty_sorted_list[SIZE+1]
        self.assertEqual(expected,actual)
        return

    def testSortedList_index_goodKey_Empty(self):
        expected = -1
        actual = self.empty_sorted_list.index(0)
        self.assertEqual(expected,actual)
        return

    def testSortedList_index_badKey_Empty(self):
        expected = -1
        actual = self.empty_sorted_list.index(30)
        self.assertEqual(expected,actual)
        return

    def testSortedList_min_Empty(self):
        expected = None
        actual = self.empty_sorted_list.min()
        self.assertEqual(expected,actual)
        return

    def testSortedList_max_Empty(self):
        expected = None
        actual = self.empty_sorted_list.max()
        self.assertEqual(expected,actual)
        return

    def testSortedList_contains_goodKey_Empty(self):
        expected = False
        actual = 5 in self.empty_sorted_list
        self.assertEqual(expected,actual)
        return

    def testSortedList_contains_badKey_Empty(self):
        expected = False
        actual = SIZE+1 in self.empty_sorted_list
        self.assertEqual(expected,actual)
        return

    def testSortedList_count_goodKey_Empty(self):
        expected = 0
        actual = self.empty_sorted_list.count(5)
        self.assertEqual(expected,actual)
        return

    def testSortedList_count_badKey_Empty(self):
        expected = 0
        actual = self.empty_sorted_list.count(SIZE+1)
        self.assertEqual(expected,actual)
        return

    def testSortedList_remove_goodKey_Empty(self):
        expected = None
        actual = self.empty_sorted_list.remove(5)
        self.assertEqual(expected,actual)
        return

    def testSortedList_remove_badKey_Empty(self):
        expected = None
        actual = self.empty_sorted_list.remove(SIZE+1)
        self.assertEqual(expected,actual)
        return

    def testSortedList_split_Empty(self):
        test_list=[]
        left_list = []
        right_list = []
        sortedList = SortedList()
        for i in test_list:
            sortedList.insert(i)
            
        left = SortedList()
        right = SortedList()      
        sortedList.split(left, right)
        self.assertEqual(left_list,left._values)
        self.assertEqual(right_list,right._values)

        return

    def testSortedList_copy_Empty(self):
        rhs = SortedList()
        for i in range(10):
            rhs.insert(i)
        self.empty_sorted_list.copy(rhs)

        i=0
        for expected in range(0,10):
            self.assertEqual(expected,self.empty_sorted_list[i])
            i+=1
        return
		
		
	def testSortedList_clean_Empty(self):
        test_list=[]
        sortedList = SortedList()
        for i in test_list:
            sortedList.insert(i)
        sortedList.clean()
        
        clean_list=[]
        
        for expected in clean_list:
            actual = sortedList.remove(expected)
            self.assertEqual(expected, actual)
        return

# Full
    def testSortedList_is_empty_Full(self):
        expected = False
        actual = self.full_sorted_list.is_empty()
        self.assertEqual(expected,actual)

        return

    def testSortedList_getitem_Full(self):
        expected = self.values_sorted_list[2]
        actual = self.full_sorted_list[2]
        self.assertEqual(expected,actual)
        return

    def testSortedList_index_goodKey_Full(self):
        value = self.values_sorted_list[SIZE//2]
        expected = self.values_sorted_list.index(value)
        actual = self.full_sorted_list.index(value)
        self.assertEqual(expected,actual)
        return

    def testSortedList_index_badKey_Full(self):
        expected = -1
        actual = self.full_sorted_list.index(30)
        self.assertEqual(expected,actual)
        return

    def testSortedList_min_Full(self):
        expected = min(self.values_sorted_list)
        actual = self.full_sorted_list.min()
        self.assertEqual(expected,actual)
        return

    def testSortedList_max_Full(self):
        expected = max(self.values_sorted_list)
        actual = self.full_sorted_list.max()
        self.assertEqual(expected,actual)
        return

    def testSortedList_contains_goodKey_Full(self):
        expected = True
        actual = self.values_sorted_list[SIZE//2] in self.full_sorted_list
        self.assertEqual(expected,actual)
        return

    def testSortedList_contains_badKey_Full(self):
        expected = False
        actual = SIZE+1 in self.full_sorted_list
        self.assertEqual(expected,actual)
        return

    def testSortedList_count_goodKey_Full(self):
        value = self.values_sorted_list[SIZE//2]
        expected = self.values_sorted_list.count(value)
        actual = self.full_sorted_list.count(value)
        self.assertEqual(expected,actual)
        return

    def testSortedList_count_badKey_Full(self):
        expected = 0
        actual = self.full_sorted_list.count(SIZE+1)
        self.assertEqual(expected,actual)
        return

    def testSortedList_remove_goodKey_Full(self):
        expected = self.values_sorted_list[0]
        actual = self.full_sorted_list.remove(expected)
        self.assertEqual(expected,actual)
        return

    def testSortedList_remove_badKey_Full(self):
        expected = None
        actual = self.full_sorted_list.remove(SIZE+1)
        self.assertEqual(expected,actual)
        return

    def testSortedList_split_Full(self):
        test_list=[1,3,5,7,9,2,4,6,8,10]
        left_list = [1,3,5,7,9]
        right_list = [2,4,6,8,10]
        sortedList = SortedList()
        for i in test_list:
            sortedList.insert(i)
            
        left = SortedList()
        right = SortedList()      
        sortedList.split(left, right)
        self.assertEqual(left_list,left._values)
        self.assertEqual(right_list,right._values)
        

        return

    def testSortedList_copy_Full(self):
        rhs = SortedList()
        for i in range(10):
            rhs.insert(i)
        self.full_sorted_list.copy(rhs)

        i=0
        for expected in range(0,10):
            self.assertEqual(expected,self.full_sorted_list[i])
            i+=1
        return
		
    def testSortedList_clean_Full(self):
        test_list=[1,1,2,3,5,5,10]
        sortedList = SortedList()
        for i in test_list:
            sortedList.insert(i)
        sortedList.clean()
        
        clean_list=[1,2,3,5,10]
        
        for expected in clean_list:
            actual = sortedList.remove(expected)
            self.assertEqual(expected, actual)
        return







if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()
Ejemplo n.º 9
0
"""
----------------------------------------------------
q3.py

----------------------------------------------------
Author: Qadeer Assan
ID: 160257370
Email: [email protected]
_updated_="2018-02-01"
----------------------------------------------------
"""
from priority_queue_array import PriorityQueue
pq1 = PriorityQueue()
key = 3
pq1.insert(1)
pq1.insert(2)
pq1.insert(3)
pq1.insert(4)

pq2, pq3 = pq1.split(key)
for i in pq2:
    print(i, 'pq2')
for i in pq3:
    print(i, 'pq3')
Ejemplo n.º 10
0
"""
----------------------------------------------------
q2.py
Tests combine() in priority queue.
----------------------------------------------------
Author: Qadeer Assan
ID: 160257370
Email: [email protected]
_updated_="2018-01-31"
----------------------------------------------------
"""
from priority_queue_array import PriorityQueue

pq1 = PriorityQueue()
pq2 = PriorityQueue()

pq1.insert(0)
pq2.insert(1)
pq1.insert(2)
pq2.insert(3)
for i in pq1:
    print(i, "pq1")
for i in pq2:
    print(i, "pq2")

pq3 = pq1.combine(pq2)
for i in pq3:
    print(i, "pq3")