Exemplo n.º 1
0
class Market:
	def __init__(self):
		self._line1=Myqueue()
		self._line2=Myqueue()
		
	def addToLine(self,c,q):
		"""c is an istance of the Customer class and q is the line number"""
		if q==1:
			self._line1.enqueue(c)
		else:
			self._line2.enqueue(c) 
			
	def chooseLine(self):
		"""chooses the shortest line, but will choose randomly if they are of equal length"""
		if len(self._line1)<len(self._line2):
			return 1
		elif len(self._line1)>len(self._line2):
			return 2
		else:
			return random.choice([1,2])
			
	def shouldIAddCustomer(self,k):
		"""the parameter K gives the percent chance of adding a customer during the given clock cycle"""
		return random.randint(0,9)<k
		
	def printMarketState(self):
		print('line 1: ' + self._line1.__str__())
		print('line 2: ' + self._line2.__str__())
		
	def simulate(self,steps,k):
		custNumber = 4
		for i in range(steps):
			print('step number'+i.__str__()+'\n')
			if not self._line1.isEmpty():
				if self._line1.peek().getItemCount()<=1:
					self._line1.dequeue()
				else:	
					self._line1.peek().decrementItems()
			if not self._line2.isEmpty():
				if self._line2.peek().getItemCount()<=1:
					self._line2.dequeue()
				else:	
					self._line2.peek().decrementItems()
			#should i add a new customer?
			if self.shouldIAddCustomer(k):
				NewCustomer=Customer(custNumber)
				custNumber+=1
				lineChoice=self.chooseLine()
				self.addToLine(NewCustomer,lineChoice)
			self.printMarketState()
Exemplo n.º 2
0
import Myqueue

Myqueue.enqueue(1)
Myqueue.enqueue(3)
Myqueue.enqueue(5)

print Myqueue.top()

Myqueue.dequeue()

print Myqueue.top()