Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

please review #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
46 changes: 46 additions & 0 deletions QueueUsingStack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# used 2 stacks to implement queue.
# before performing pop and peek check move all the items to outstack if that is empty.
# time complexity for push and isempty - O(1)
# time complexity for pop and peek - O(N)
class MyQueue:

def __init__(self):
"""
Initialize your data structure here.
"""
self.inStack = [] # initializing python list as instack
self.outStack = [] # initializing python list as outstack

def push(self, x: int) -> None:
"""
Push element x to the back of queue.
"""

self.inStack.append(x) # push new items to instack
# print(self.inStack, self.outStack)

def pop(self) -> int:
"""
Removes the element from in front of queue and returns that element.
"""
self.moveItems() # calling move method.
return self.outStack.pop() # pop peek(top) item from outstack

def peek(self) -> int:
"""
Get the front element.
"""
self.moveItems() # calling move method.
return self.outStack[-1] # extracting top element i.e., last inserted item to the outstack.

def empty(self) -> bool:
"""
Returns whether the queue is empty.
"""
if not self.inStack and not self.outStack:
return True # if both stacks are empty return true.

def moveItems(self):
if not self.outStack: # if outstack is empty then push all items from instack to outstack
while self.inStack:
self.outStack.append(self.inStack.pop()) # pop one item at a time from instack and push to outstack.
49 changes: 49 additions & 0 deletions hashset_4(usingDoubleHash).py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# As per the class I tried double hashing technique for hashset, in leetcode failed 4 testcases. Not sure.
# My challenges were Initializing lists with boolean variables, and accessing public variables where i was stuck.
class MyHashSet:

def __init__(self):
"""
Initialize your data structure here.
"""
self.buckets = 1000 # list of 1000 items
self.bucketItems = 1000 # list of 1000 items
self.storage = [None for i in range(self.buckets)] # initializing list variables as None at beginning

def bucketsHash(self, value):
return value % self.buckets #return hash code

def bucketItemsHash(self, value):
return value % self.bucketItems # return hashcode

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value // bucketItems


def add(self, value: int) -> None:
buc = self.bucketsHash(value) # get the hashcode for main list
bucItem = self.bucketItemsHash(value) # get the hash code for sublist
# print(buc,bucItem)
if self.storage[buc] is None:
self.storage[buc] = [None for i in range(self.bucketItems)] # if main list is none initialize sublist
if self.storage[buc] is not None and self.storage[buc][bucItem] is None:
# if main list is initialized and having a sublist and sublist hash item
# is None then initialize it to True which adds an item.
Comment on lines +25 to +27

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't need this if

self.storage[buc][bucItem] = True
# print(self.storage[buc][bucItem])

def remove(self, value: int) -> None:
buc = self.bucketsHash(value) # get the hashcode for main list
bucItem = self.bucketItemsHash(value) # get the hash code for sublist
if self.storage[buc] is not None:
# if mainlist is not none then make the item false
# to remove the item.
self.storage[buc][bucItem] = False

def contains(self, value: int) -> bool:
"""
Returns true if this set contains the specified element
"""
buc = self.bucketsHash(value) # get the hashcode for main list
bucItem = self.bucketItemsHash(value) # get the hash code for sublist
if self.storage[buc] is not None and self.storage[buc][bucItem]:
# if main list is not none and sublist is true then return true as element is found
# else return false as the element doesnt exist.
return True

33 changes: 33 additions & 0 deletions hashset_4(usingSingleList).py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# time complexity for operations is O(1) but the space required is too high.

class MyHashSet:

def __init__(self):
"""
Initialize your data structure here.
"""
self.buckets = 1000000 # initializing 1 million records array
self.storage = [None for i in range(self.buckets)] #initializing a list

def bucketsHash(self, value):
return value % self.buckets # developing a hash function

def add(self, value: int) -> None:
buc = self.bucketsHash(value) # getting hash value
# print(buc,bucItem)
# if self.storage[buc] is None:
self.storage[buc] = True # inserting true for the data if exists
# print(self.storage[buc][bucItem])

def remove(self, value: int) -> None:
buc = self.bucketsHash(value) # getting hash value
if self.storage[buc] is not None:
self.storage[buc] = False # if the value is present remove it by making false

def contains(self, value: int) -> bool:
"""
Returns true if this set contains the specified element
"""
buc = self.bucketsHash(value) # getting hash value
if self.storage[buc] == True:
return True # if the value is present return true.