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
40 changes: 34 additions & 6 deletions Exercise_1.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,52 @@
# Time Complexity : O(1)
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : yes
# Any problem you faced while coding this : No


# 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.arr = list()

def isEmpty(self):

return self.size() == 0

def push(self, item):
self.arr.append(item)

def pop(self):

if self.isEmpty():
return "Stack is Empty"

return self.arr.pop()

def peek(self):

def size(self):
if self.isEmpty():
return "Stack is Empty"

return self.arr[-1]

def size(self):
return len(self.arr)

def show(self):
return self.arr


s = myStack()
print("Push: 1")
s.push('1')
print("Push: 2")
s.push('2')
print(s.pop())
print(s.show())
print("Show: ",s.show())
print("isEmpty: ",s.isEmpty())
print("Pop: ",s.pop())
print("Show: ",s.show())
print("Pop: ",s.pop())
print("Show: ",s.show())
print("isEmpty: ",s.isEmpty())
print("Pop: ",s.pop())
35 changes: 35 additions & 0 deletions Exercise_2.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# Time Complexity : O(1) with show() it will be O(n)
# push: O(1)
# pop: O(1)
# show: O(n)
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : yes
# Any problem you faced while coding this : No

class Node:
def __init__(self, data):
Expand All @@ -6,17 +13,43 @@ def __init__(self, data):

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

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

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

tmp = self.head.data
self.head = self.head.next
return tmp

def show(self):
current = self.head
if current is None:
print("Stack is empty")
return
print("\n--- Stack (top to bottom) ---")
while current is not None:
addr = id(current)
next_addr = id(current.next) if current.next else None
print(f"Data: {current.data} | Address: {addr} | Next: {next_addr}")
current = current.next
print("-----------------------------\n")


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('show')
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 +61,7 @@ def pop(self):
print('Stack is empty.')
else:
print('Popped value: ', int(popped))
elif operation == 'show':
a_stack.show()
elif operation == 'quit':
break
50 changes: 50 additions & 0 deletions Exercise_3.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
# Time Complexity : O(n)
# append: O(n)
# find: O(n)
# remove: O(n)
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : yes
# Any problem you faced while coding this : No

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 +21,62 @@ def __init__(self):
Takes O(1) time.
"""
self.head = None


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

if self.head is not None:
current = self.head
while current.next is not None:
current = current.next

current.next = ListNode(data)
else:
self.head = ListNode(data)

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.
"""
if self.head is not None:
current = self.head
while current is not None:
if current.data == key:
return "Found the matching key"
current = current.next
return None
return None

def remove(self, key):
"""
Remove the first occurrence of `key` in the list.
Takes O(n) time.
"""
if self.head is not None:
current = self.head
pointer = self.head
while current is not None:
if current.data == key:
if current == self.head:
self.head = self.head.next
return "key removed"
pointer.next = current.next
return "key removed"
pointer = current
current = current.next

return None
return None