Last active
December 11, 2015 22:58
-
-
Save pfctdayelise/4672990 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment