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
48 changes: 31 additions & 17 deletions Exercise_1.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
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 pop(self):


def peek(self):

def size(self):

def show(self):

# Please read sample.java file before starting.
# Kindly include Time and Space complexity at top of each file

# Time Complexity: O(1) for all operations
# Space Complexity: O(n) where n is the number of elements in the stack

def __init__(self):
self.stack = []

def isEmpty(self):
return len(self.stack) == 0

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

def pop(self):
if self.isEmpty():
return None
return self.stack.pop()

def peek(self):
if self.isEmpty():
return None
return self.stack[-1]

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

def show(self):
return self.stack


s = myStack()
s.push('1')
Expand Down
29 changes: 21 additions & 8 deletions Exercise_2.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,37 @@
# Time Complexity: O(1) for all operations
# Space Complexity: O(n) where n is the number of elements in the stack

class Node:
def __init__(self, data):
self.data = data
self.next = None

self.data = data
self.next = None


class Stack:
def __init__(self):

self.head = None

def push(self, data):

new_node = Node(data)
new_node.next = self.head
self.head = new_node

def pop(self):

if self.head is None:
return None
data = self.head.data
self.head = self.head.next
return data


a_stack = Stack()
while True:
#Give input as string if getting an EOF error. Give input like "push 10" or "pop"
# Give input as string if getting an EOF error. Give input like "push 10" or "pop"
print('push <value>')
print('pop')
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"
# 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.push(int(do[1]))
Expand Down
40 changes: 37 additions & 3 deletions Exercise_3.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# Time Complexity: O(n) for append, find, and remove operations
# Space Complexity: O(n) where n is the number of elements in the list

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 +23,44 @@ def append(self, data):
Insert a new element at the end of the list.
Takes O(n) time.
"""

new_node = ListNode(data)
if not self.head:
self.head = new_node
else:
current = self.head
while current.next:
current = current.next
current.next = 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.
"""

current = self.head
while current:
if current.data == key:
return current
current = current.next
return None

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

if self.head.data == key:
self.head = self.head.next
return

current = self.head
while current.next:
if current.next.data == key:
current.next = current.next.next
return
current = current.next
return None