From d8676abb4dbc16caf1a070547ce24b5f09f9509e Mon Sep 17 00:00:00 2001 From: nikhylw <1.nikhil.wani+nikhly@gmail.com> Date: Fri, 29 May 2026 23:56:41 +0530 Subject: [PATCH] add design-2 --- implement_hashmap.py | 36 ++++++++++++++++++++++++++++++++++++ queue_using_stack.py | 22 ++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 implement_hashmap.py create mode 100644 queue_using_stack.py diff --git a/implement_hashmap.py b/implement_hashmap.py new file mode 100644 index 00000000..6aa8cfad --- /dev/null +++ b/implement_hashmap.py @@ -0,0 +1,36 @@ +class MyHashMap: + + def __init__(self): + self.primaryBuckets = 1000 + self.secondaryBuckets = 1000 + self.storage = [None] * self.primaryBuckets + + def getPrimaryHash(self, key): + return key % self.primaryBuckets + + def getSecondaryHash(self, key): + return key // self.secondaryBuckets + + def put(self, key, value): + primaryIndex = self.getPrimaryHash(key) + if self.storage[primaryIndex] == None: + if primaryIndex == 0: + self.storage[primaryIndex] = [-1] * (self.secondaryBuckets + 1) + else: + self.storage[primaryIndex] = [-1] * self.secondaryBuckets + secondaryIndex = self.getSecondaryHash(key) + self.storage[primaryIndex][secondaryIndex] = value + + def remove(self, key): + primaryIndex = self.getPrimaryHash(key) + if self.storage[primaryIndex] == None: + return + secondaryIndex = self.getSecondaryHash(key) + self.storage[primaryIndex][secondaryIndex] = -1 + + def get(self, key): + primaryIndex = self.getPrimaryHash(key) + if self.storage[primaryIndex] == None: + return -1 + secondaryIndex = self.getSecondaryHash(key) + return self.storage[primaryIndex][secondaryIndex] \ No newline at end of file diff --git a/queue_using_stack.py b/queue_using_stack.py new file mode 100644 index 00000000..55a440cb --- /dev/null +++ b/queue_using_stack.py @@ -0,0 +1,22 @@ +class MyQueue: + def __init__(self): + self.inSt = [] + self.outSt = [] + + def push(self, x: int) -> None: + self.inSt.append(x) + + def pop(self) -> int: + if self.empty(): + return -1 + self.peek() + return self.outSt.pop() + + def peek(self) -> int: + if not self.outSt: + while self.inSt: + self.outSt.append(self.inSt.pop()) + return self.outSt[-1] + + def empty(self) -> bool: + return not self.inSt and not self.outSt