Created
December 19, 2015 15:17
-
-
Save avamsi/2b2dbd24f16fb5f3a8ce 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
from random import randint | |
class ARR(object): | |
def __init__(self): | |
self.arr = [] | |
self.pos = {} | |
self.len = 0 | |
def append(self, a): | |
self.arr.append(a) | |
self.pos[a] = self.len | |
self.len += 1 | |
def remove(self, a): | |
i = self.pos.pop(a) | |
self.arr[i] = self.arr[-1] | |
self.pos[self.arr[i]] = i | |
self.len -= 1 | |
def random(self): | |
return self.arr[randint(0, self.len - 1)] | |
ds = ARR() | |
for i in xrange(10): | |
ds.append(i) | |
ds.append(32) | |
ds.remove(32) | |
print ds.random() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment