Created
January 29, 2010 16:55
-
-
Save wozozo/289884 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
# coding: utf-8 | |
class Q(object): | |
""" | |
>>> q = Q(2) | |
>>> q.put('a', 1) | |
>>> q.put('b', 2) | |
>>> q.put("c", 3) | |
>>> q.get("a") | |
>>> q.get('b') | |
2 | |
>>> q.get('c') | |
3 | |
>>> q.get('d') | |
""" | |
def __init__(self, size): | |
self.size = size | |
self.keys = [] | |
self.data = {} | |
def put(self, key, val): | |
if len(self.data) >= self.size: | |
del self.data[self.keys[0]] | |
self.keys = self.keys[1:] | |
self.keys.append(key) | |
self.data[key] = val | |
def get(self, key): | |
if key in self.data: | |
pop = self.keys.pop(self.keys.index(key)) | |
self.keys.append(pop) | |
return self.data[key] | |
return None | |
if __name__ == '__main__': | |
import doctest | |
doctest.testmod() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment