Skip to content
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
37 changes: 30 additions & 7 deletions Exercise_1.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,44 @@
# Time Complexity : O(1)
# Space Complexity : O(N)
# Did this code successfully run on Leetcode : Ran it locally
# Any problem you faced while coding this : Deciding the max length and adding the sanity checks


# Your code here along with comments explaining your approach
class myStack:
#Please read sample.java file before starting.
#Kindly include Time and Space complexity at top of each file
def __init__(self):

self.maxLen = 1000 # Maximum length allowed for the stack
self.stack = [] # Array that will be used as stack
self.length = 0 # length for O(1) access to check the length

def isEmpty(self):
if self.length == 0: # If the stack is empty then the length will be zero
return True
return False

def push(self, item):
if self.length == self.maxLen: # If the length has reached the max allowed length then reject the push
return "Stack is full"
self.stack.append(item)
self.length += 1

def pop(self):
if self.length == 0: # If the stack is empty then reject the pop
return "Stack is empty!"

self.length -= 1
return self.stack.pop()

def peek(self):

def size(self):

def show(self):
if len(self.stack) == 0: # If the stack is empty reject the peek as there is no item to peek
return "Stack is empty!"
return self.stack[-1]

def size(self): # Return the size of the stack
return self.length

def show(self): # Return the entire stack
return self.stack

s = myStack()
s.push('1')
Expand Down
26 changes: 25 additions & 1 deletion Exercise_2.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
# Time Complexity : O(1)
# Space Complexity : O(N)
# Did this code successfully run on Leetcode : Ran it locally
# Any problem you faced while coding this : Forgot what actually stack means and build a queue using linkedlist
# Staying focussed and aware is what I need to improve on.
# Learned to write comments using ''''''


# Your code here along with comments explaining your approach
class Node:
def __init__(self, data):
self.data = data
self.next = None

class Stack:
def __init__(self):
'''Create an empty pointer to store the top of the linked list/stack'''
self.linkedListTop = None

def push(self, data):

'''Creating a node with the data to add to the top of the linked list'''
dataNode = Node(data)
if self.linkedListTop is None:
self.linkedListTop = dataNode
else:
'''Adding datanode to the top of the linked list and setting the linkedListTop to it'''
dataNode.next = self.linkedListTop
self.linkedListTop = dataNode

def pop(self):
if self.linkedListTop is None:
return None
'''Removing the top node and setting linkedListTop to the next node in the list'''
poppedData = self.linkedListTop.data
self.linkedListTop = self.linkedListTop.next
return poppedData

a_stack = Stack()
while True:
Expand Down
85 changes: 85 additions & 0 deletions Exercise_3.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
# Time Complexity : O(N) for all operations
# Space Complexity : O(N)
# Did this code successfully run on Leetcode : Ran it locally
# Any problem you faced while coding this : None


# Your code here along with comments explaining your approach
class ListNode:
"""
A node in a singly-linked list.
"""
def __init__(self, data=None, next=None):
self.data = data
self.next = next

class SinglyLinkedList:
def __init__(self):
Expand All @@ -17,16 +26,92 @@ def append(self, data):
Insert a new element at the end of the list.
Takes O(n) time.
"""
node = ListNode(data=data)
'''If the linkedlist is empty set the head to the node'''
'''Else iterate the linkedlist to reach the end of the list and attach the node'''

if self.head is None:
self.head = node

else:
curr = self.head

while curr.next:
curr = curr.next

curr.next = node


def find(self, key):
"""
Search for the first element with `data` matching
`key`. Return the element or `None` if not found.
Takes O(n) time.
"""
curr = self.head
'''Iterate the linkedlist to find the key'''
while curr:
if curr.data == key:
return curr
curr = curr.next

return None

def remove(self, key):
"""
Remove the first occurrence of `key` in the list.
Takes O(n) time.
"""

'''If the key is the first node then update the head to head.next'''
if self.head.data == key:
curr = self.head
self.head = self.head.next
return curr

'''Iterate the linkedlist and link the node before the key with the node after the key'''
curr = self.head
prev = self.head
while curr:
if curr.data == key:
prev.next = curr.next
return curr
prev = curr
curr = curr.next

def show(self):
curr = self.head
result = []
while curr:
result.append(curr.data)
curr = curr.next
return result


a_stack = SinglyLinkedList()
while True:
print('Linked List: ', a_stack.show())
#Give input as string if getting an EOF error. Give input like "push 10" or "find 10"
print('push <value>')
print('find <value>')
print('remove <value>')
print('quit')
do = input('What would you like to do? ').split()
#Give input as string if getting an EOF error. Give input like "push 10" or "pop"
operation = do[0].strip().lower()
if operation == 'push':
a_stack.append(int(do[1]))
elif operation == 'find':
popped = a_stack.find(int(do[1]))
if popped is None:
print('Element not found')
else:
print('Element found: ', popped.__str__)
elif operation == 'remove':
popped = a_stack.remove(int(do[1]))
if popped is None:
print('Element not found')
else:
print('Element removed successfully')
elif operation == 'quit':
break