From db639b1994699f0fd986324b737b1abb697f266f Mon Sep 17 00:00:00 2001 From: Sarthak Mundhra <63492158+stkmnd@users.noreply.github.com> Date: Fri, 25 Oct 2024 00:29:57 -0400 Subject: [PATCH 1/2] Add files via upload --- hashmap.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 hashmap.py diff --git a/hashmap.py b/hashmap.py new file mode 100644 index 00000000..79f03ccc --- /dev/null +++ b/hashmap.py @@ -0,0 +1,64 @@ +class ListNode: + def __init__(self, key: int, value: int): + self.key = key + self.value = value + self.next = None + +class MyHashMap: + + def __init__(self): + self.size = 1000 + self.hashTable = [None] * self.size + + def hashFunction(self, val): + return val % self.size + + def put(self, key: int, value: int) -> None: + hashVal = self.hashFunction(key) + + if self.hashTable[hashVal] == None: + self.hashTable[hashVal] = ListNode(key, value) + return + else: + curr = self.hashTable[hashVal] + prev = None + while curr: + if curr.key == key: + curr.value = value + return + prev = curr + curr = curr.next + + if prev: + prev.next = ListNode(key, value) + return + + def get(self, key: int) -> int: + hashVal = self.hashFunction(key) + curr = self.hashTable[hashVal] + + while curr: + if curr.key == key: + return curr.value + curr = curr.next + return -1 + + def remove(self, key: int) -> None: + hashVal = self.hashFunction(key) + curr = self.hashTable[hashVal] + prev = None + if curr == None: + return + if curr.key == key: + self.hashTable[hashVal] = curr.next + curr.next = None + return + while curr: + if curr.key == key: + prev.next = curr.next + curr.next = None + return + + prev = curr + curr = curr.next + return From cb77508c5dc38f28c1b7ec74f73c2ef1df81bb8f Mon Sep 17 00:00:00 2001 From: Sarthak Mundhra <63492158+stkmnd@users.noreply.github.com> Date: Fri, 25 Oct 2024 01:14:47 -0400 Subject: [PATCH 2/2] Add files via upload --- queue.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 queue.py diff --git a/queue.py b/queue.py new file mode 100644 index 00000000..89d781ae --- /dev/null +++ b/queue.py @@ -0,0 +1,23 @@ +class MyQueue: + + def __init__(self): + self.instack = [] + self.outstack = [] + + def push(self, x: int) -> None: + self.instack.append(x) + + def pop(self) -> int: + if len(self.outstack) == 0: + while len(self.instack) > 0: + self.outstack.append(self.instack.pop()) + return self.outstack.pop() if self.outstack else None + + def peek(self) -> int: + if len(self.outstack) == 0: + while len(self.instack) > 0: + self.outstack.append(self.instack.pop()) + return self.outstack[-1] if self.outstack else None + + def empty(self) -> bool: + return len(self.outstack) == 0 and len(self.instack) == 0