Created
October 20, 2017 02:17
-
-
Save whalesalad/7d1129ef08cd847f492a1435fa39f9b9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import operator | |
class LeastUsed(object): | |
""" | |
Class that stores a list of items. | |
When an item is requested, that item is considered to have been 'used' | |
Each time an item is requested, request the one that has been used the least. | |
""" | |
def __init__(self, items): | |
self.items = {item: 0 for item in items} | |
@property | |
def next_item(self): | |
return sorted(self.items.items(), key=operator.itemgetter(1))[0] | |
@property | |
def get(self): | |
key, _ = self.next_item | |
self.items[key] += 1 | |
return key |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: do not use this -- use itertools.cycle ¯_(ツ)_/¯