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
50 changes: 37 additions & 13 deletions Exercise_1.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,48 @@
# Time Complexity: O(1)
# Space Complexity: O(1)
"""
Init class with empty stack of default size 1000 and starts top with -1
push will increment the top and use that index to add the element
pop will use top val to decrement the index
"""
class myStack:
#Please read sample.java file before starting.
#Kindly include Time and Space complexity at top of each file
def __init__(self):

def isEmpty(self):

def push(self, item):
def __init__(self, capacity=1000):
self.stack = [None] * capacity
self.top = -1

def isEmpty(self):
if self.top == -1:
return True
return False

def pop(self):

def push(self, item):
if self.top == len(self.stack) - 1:
return "Stack Overflow"
self.top += 1
self.stack[self.top] = item

def pop(self):
if self.isEmpty():
return "Stack Underflow"
self.top -= 1
return self.stack[self.top + 1]

def peek(self):
def peek(self):
if self.isEmpty():
return "Stack Underflow"
return self.stack[self.top]

def size(self):
def size(self):
return self.top + 1

def show(self):
def show(self):
return self.stack


s = myStack()
s.push('1')
s.push('2')
print(s.pop())
print(s.show())
# print(s.show())


28 changes: 27 additions & 1 deletion Exercise_2.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,46 @@

"""
Init class with head with null
Push will add the node to the top of the list
Pop will point the pointer to the next node
Time complexity O(1)
Space O(n)
"""
class Node:
def __init__(self, data):
self.data = data
self.next = None

class Stack:
def __init__(self):
self.head = None

def push(self, data):
temp = Node(data)
temp.next = self.head
self.head = temp

def pop(self):
if self.head == None:
return None

temp = self.head
self.head = temp.next
return temp.data

def show(self):
temp = self.head
while temp:
print(temp.data, end=" -> ")
temp = temp.next
print("None")

a_stack = Stack()
while True:
#Give input as string if getting an EOF error. Give input like "push 10" or "pop"
print('push <value>')
print('pop')
print('quit')
print('print')
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()
Expand All @@ -28,5 +52,7 @@ def pop(self):
print('Stack is empty.')
else:
print('Popped value: ', int(popped))
elif operation == 'print':
print(a_stack.show())
elif operation == 'quit':
break
52 changes: 52 additions & 0 deletions Exercise_3.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
# Initilaized the class with head and tail
# append method will check if there is any head avaiable and creates head if not none
# used tail pointer to append to the end of the linked list without iterating through full list
# updating tail everytime when inserting
# find method will start from the head node and the iterate through the list returns the val if found else None
# remove method will find the key first by maintaing previous node
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 @@ -11,22 +19,66 @@ def __init__(self):
Takes O(1) time.
"""
self.head = None
self.tail = None

def append(self, data):
"""
Insert a new element at the end of the list.
Takes O(n) time.
"""
new_node = ListNode(data)

if self.head is None:
self.head = new_node
self.tail = new_node
return

# curr = self.head

# while curr:
# curr = curr.next
# curr.next = new_node

self.tail.next = new_node
self.tail = new_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

while curr:

if curr.data == key:
return curr.data
curr = curr.next
return None

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

curr = self.head
prev = None
while curr:
if curr.data == key:
if prev == None:
self.head = curr.next
else:
prev.next = curr.next


if curr is self.tail:
self.tail = prev
return True

prev = curr
curr = curr.next

return False