示例#1
0
def test_array_queue():
    array_queue = ArrayQueue()

    # Assert that we start with an empty queue
    assert array_queue.is_empty()
    with pytest.raises(Empty):
        array_queue.first()

    # Enqueue a bunch of elements into the back of the queue
    for i in range(10):
        array_queue.enqueue(i)
        array_queue, first = array_queue.first()
        assert first == 0
        assert len(array_queue) == i + 1

    # Dequeue a bunch of elements from the front of the queue
    for i in range(10):
        array_queue, val = array_queue.dequeue()
        assert val == i
        assert len(array_queue) == 10 - 1 - i

    # Assert we are back to an empty queue
    assert array_queue.is_empty()
    with pytest.raises(Empty):
        array_queue.dequeue()
示例#2
0
class StockTracker:
#set constructor as an empty object
	def __init__(self):
		self._total_qty = 0
		self._profit = 0.0
		self._arque = ArrayQueue()
		
#buy stock. quantity must be greater than 0 and price must be a floating point number otherwise error occurs
	def buy ( self, quantity, price ):
		assert ( quantity > 0 ), 'quantity needs to be a positive integer'
		assert ( price > 0 ), 'price needs to be a positive floating point number'
		assert ( type( price ) is float ), 'price needs to be a positive floating point number'
		self._arque.enqueue( ( quantity, price ) )
		self._total_qty += quantity
		
#sell stock. quantity must be greater than 0, price must be floating point number,
#quantity must be less than or equal to the quantity on hand other wise error occurs
	def sell ( self, quantity, price ):
		assert ( quantity > 0 ), 'quantity needs to be a positive integer'
		assert ( price > 0 ), 'price needs to be a positive floating point number'
		assert ( type( price ) is float ), 'type must be float'
		assert ( quantity <= self.getQuantityOnHand() ), 'quantity needs to be less than or equal to the total number of shares on hand'
		
		selling = 0		
		while quantity > 0:
			transaction = self._arque.dequeue()			#Deque value
			amount = transaction[ 0 ]					#Parse value
			value = transaction[ 1 ]
			
			if quantity >= amount:						#Make sure the specific transaction has enough to fill the order
				selling += amount * ( price - value ) 	#Calculate the profit
				self._total_qty -= amount				#Update remaining shares sell
			else:
				selling += quantity  * ( price - value )
				self._total_qty -= quantity
				self._arque.enqueue( ( amount - quantity, value ) ) #Re-add what is left of this transaction
				while self._arque.first() != ( amount - quantity, value ): #Send it back to the front
					self._arque.enqueue( self._arque.dequeue() )
				
			quantity -= amount
		
		self._profit += selling
		return 	selling

#returns total gain or loss since creating the empty object        
	def getProfit (self):
		return self._profit
	
#returns the quantity of shares on hand   
	def getQuantityOnHand (self):
		return self._total_qty
示例#3
0
class ShopTracker:

    def __init__(self):
        self._listQueue = ArrayQueue()
        
        
    def startDay(self):
        """ Starts the day off by starting the listing process """
        
        alpha = raw_input("Please enter the name (Enter End when done): ")
        while alpha != "End":
             self._listQueue.enqueue(alpha)
             alpha = raw_input("Please enter the name (Enter End when done): ")
        print len(self._listQueue) , "client(s) left"

    def addMore(self):
        """ Restarts the listing process again to add more names """
        
        alpha = raw_input("Please enter the name (Enter End when done): ")
        while alpha != "End":
             alpha = raw_input("Please enter the name (Enter End when done): ")
             self._listQueue.enqueue(alpha)
        print len(self._listQueue) , "client(s) left"        

    def endDay(self):
        """ Ends the day by clearing out the queue"""
        while len(self._listQueue) != 0:
            self._listQueue.dequeue()
        print len(self._listQueue) , "client(s) left"
        

    def getLength(self):
        """ Return length of elements in queue """
        return len(self._listQueue)

    def primero(self):
        """ Prints out the first name in the queue (will not remove) """
        if len(self._listQueue) == 0:
            #raise Empty('Queue is empty')
            print "You have no client(s) left"
        else:
            return self._listQueue.first()

    def getNext(self):
        """ Return the first name in the queue (will remove) """
        if len(self._listQueue) == 0:
            #raise Empty('Queue is empty')
            print "You have no client(s) left"
        else:
            return self._listQueue.dequeue()
示例#4
0
class ShopTracker:
    def __init__(self):
        self._listQueue = ArrayQueue()

    def startDay(self):
        """ Starts the day off by starting the listing process """

        alpha = raw_input("Please enter the name (Enter End when done): ")
        while alpha != "End":
            self._listQueue.enqueue(alpha)
            alpha = raw_input("Please enter the name (Enter End when done): ")
        print len(self._listQueue), "client(s) left"

    def addMore(self):
        """ Restarts the listing process again to add more names """

        alpha = raw_input("Please enter the name (Enter End when done): ")
        while alpha != "End":
            alpha = raw_input("Please enter the name (Enter End when done): ")
            self._listQueue.enqueue(alpha)
        print len(self._listQueue), "client(s) left"

    def endDay(self):
        """ Ends the day by clearing out the queue"""
        while len(self._listQueue) != 0:
            self._listQueue.dequeue()
        print len(self._listQueue), "client(s) left"

    def getLength(self):
        """ Return length of elements in queue """
        return len(self._listQueue)

    def primero(self):
        """ Prints out the first name in the queue (will not remove) """
        if len(self._listQueue) == 0:
            #raise Empty('Queue is empty')
            print "You have no client(s) left"
        else:
            return self._listQueue.first()

    def getNext(self):
        """ Return the first name in the queue (will remove) """
        if len(self._listQueue) == 0:
            #raise Empty('Queue is empty')
            print "You have no client(s) left"
        else:
            return self._listQueue.dequeue()
示例#5
0
def main():
    """Pulls data from file, assigns days and writes to submission file"""
    queue = ArrayQueue()
    cost = 0
    assignments = []
    days = {k: 0 for k in range(1, 101)}
    read_file("family_data.csv", queue)
    for i in range(2, 12):
        sort_list(queue, i)
        cost += first_pass(queue, days, assignments, i)
    while queue.first() != None:
        for i in range(2, 12):
            sort_list(queue, i)
            cost += single_pass(queue, days, assignments, i)
    cost += (days[100]-125.0) / 400.0 * days[100]**(0.5)
    cost += calc_accounting_penalty(days, 99, 100)
    write_file("submission_file.csv", assignments)
    print('${:.2f}'.format(cost))