The LRUCache's get() function sets the self.hand variable to just after the current position. If the same entry is read repeatedly, put() calls will always work in the same area of the cache and not reach the empty parts. As a result, the cache will start removing entries even though it is not full:
>>> c = LRUCache(1000)
>>> c.put(1, 1)
>>> c.put(2, 2) # <---- "2" is added
>>> c.get(1)
1
>>> c.put(3, 3) # <---- ref is set to False for "2"
>>> c.get(1)
1
>>> c.put(4, 4) # <----- "2" is evicted and replaced by "4"
>>> c.data
{1: (0, 1), 3: (2, 3), 4: (1, 4)} # <---- "2" is not there, even though cache is 99% empty
A solution would be to just not set self.hand in the get() method.
The LRUCache's get() function sets the self.hand variable to just after the current position. If the same entry is read repeatedly, put() calls will always work in the same area of the cache and not reach the empty parts. As a result, the cache will start removing entries even though it is not full:
A solution would be to just not set self.hand in the get() method.