Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions Algorithms/datastructs/hash_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import random

class RandomizedSet:

def __init__(self):
self.hash_table = dict()
self.lst = []
def insert(self, val: int) -> bool:
if val not in self.hash_table:
self.hash_table[val] = len(self.lst)
self.lst.append(val)
return True
else:
return False
def remove(self, val: int) -> bool:
if val in self.hash_table:
idx = self.hash_table.pop(val)
# swap if index is not last
if idx != len(self.lst)-1:
last = self.lst[-1]
self.lst[-1] = self.lst[idx]
self.lst[idx] = last
self.hash_table[last] = idx
self.lst.pop()
return True
else:
return False
def getRandom(self) -> int:
if not self.lst:
raise IndexError("getRandom() called on empty set")
return self.lst[random.randint(0, len(self.lst) - 1)]

class RandomizedCollection:

def __init__(self):
self.table = dict()
self.lst = []

def insert(self, val: int) -> bool:
self.lst.append(val)
if val not in self.table:
self.table[val] = {len(self.lst)-1}
return True
else:
self.table[val].add(len(self.lst)-1)
return False

def remove(self, val: int) -> bool:
if val in self.table:
idx = self.table[val].pop()
if idx != len(self.lst)-1:
# not last
last = self.lst[-1]
self.lst[-1] = self.lst[idx]
self.lst[idx] = last
self.table[last].remove(len(self.lst)-1)
self.table[last].add(idx)
self.lst.pop()
if not self.table[val]:
del self.table[val]
return True
else:
return False

def getRandom(self) -> int:
return self.lst[random.randint(0, len(self.lst)-1)]
73 changes: 0 additions & 73 deletions Algorithms/datastructs/num_in_intervals.py

This file was deleted.

40 changes: 40 additions & 0 deletions tests/test_hash_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from Algorithms.datastructs.hash_table import RandomizedSet

def test_randomized_set():
rs = RandomizedSet()

# Insert 0, should return True
assert rs.insert(0) == True
# Remove 0, should return True
assert rs.remove(0) == True
# Remove 0 again, should return False (already removed)
assert rs.remove(0) == False
# Insert 0 again, should return True
assert rs.insert(0) == True
# getRandom should return 0 (only element)
assert rs.getRandom() == 0
# Remove 0, should return True
assert rs.remove(0) == True
# Insert -1, should return True
assert rs.insert(-1) == True
# Remove 0 (not present), should return False
assert rs.remove(0) == False

# Multiple getRandom calls — since only -1 is present, it should always return -1
for _ in range(10):
val = rs.getRandom()
assert val == -1, f"Expected -1 but got {val}"

# Remove -1, should return True
assert rs.remove(-1) == True
# Now getRandom on empty set — handle gracefully by catching exception or custom behavior
try:
rs.getRandom()
print("getRandom on empty set did not raise error — consider adding handling for empty set.")
except IndexError:
print("getRandom on empty set raised IndexError as expected.")

print("All tests passed!")

if __name__ == "__main__":
test_randomized_set()