public
Last active

  • Download Gist
progressive.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
"""Public domain, yo.
See http://brianna.laugher.id.au/blog/79/the-progressive-speaking-stack-in-python for background.
"""
 
class Speaker:
def __init__(self, name):
self.name = name
self._hasSpoken = False
 
def __eq__(self, other):
return self.name == other.name
 
def __str__(self):
return 'Speaker<{}>'.format(self.name)
 
def speak(self):
self._hasSpoken = True
return '{}!'.format(self.name*3)
 
def privilege(self):
"""A more accurate implementation is
left as an exercise to the reader.
"""
return self._hasSpoken
 
 
class ProgressiveStack:
"""I can't help noting that this is actually a queue
not a stack. ahem.
In real code I would also implement len, contains, etc.
"""
def __init__(self, sortMethod):
"""
@param sortMethod: a function used to sort queue items
such that the first-sorted item will be selected first
(so smaller values = selected earlier).
"""
self._queue = []
self._sortMethod = sortMethod
 
def __str__(self):
return 'ProgressiveStack<[{}]>'.format(','.join(str(i) for i in self._queue))
 
def __iter__(self):
return self
 
def next(self):
bestChoice = self._queue.pop(0)
return bestChoice
 
def add(self, item):
self._queue.append(item)
self._queue.sort(key=self._sortMethod)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.