Skip to content

Instantly share code, notes, and snippets.

@whalesalad
Created October 20, 2017 02:17
Show Gist options
  • Save whalesalad/7d1129ef08cd847f492a1435fa39f9b9 to your computer and use it in GitHub Desktop.
Save whalesalad/7d1129ef08cd847f492a1435fa39f9b9 to your computer and use it in GitHub Desktop.
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
@whalesalad
Copy link
Author

Note: do not use this -- use itertools.cycle ¯_(ツ)_/¯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment