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
55 changes: 36 additions & 19 deletions Exercise_1.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
# Time Complexity : everything is O(1)
# Space Complexity : O(n)
# 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):

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
def __init__(self):
self.arr = []

def isEmpty(self):
if len(self.arr) == 0:
return True
return False

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

def pop(self):
return self.arr.pop()

def peek(self):
return self.arr[-1]

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

def show(self):
return self.arr


s = myStack()
s.push('1')
s.push('2')
s.push("1")
s.push("2")
print(s.pop())
print(s.show())
50 changes: 33 additions & 17 deletions Exercise_2.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,48 @@
# Time Complexity : O(1)
# Space Complexity : O(n)
# 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 Node:
def __init__(self, data):
self.data = data
self.next = None

self.data = data
self.next = None


class Stack:
def __init__(self):

self.arr = []

def push(self, data):

self.arr.append(data)

def pop(self):

if len(self.arr) == 0:
return None
return self.arr.pop()


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')
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"
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"
print(do)
operation = do[0].strip().lower()
if operation == 'push':
if operation == "push":
a_stack.push(int(do[1]))
elif operation == 'pop':
elif operation == "pop":
popped = a_stack.pop()
if popped is None:
print('Stack is empty.')
print("Stack is empty.")
else:
print('Popped value: ', int(popped))
elif operation == 'quit':
print("Popped value: ", int(popped))
elif operation == "quit":
break
70 changes: 67 additions & 3 deletions Exercise_3.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
# Time Complexity : O(n)
# Space Complexity : O(n)
# Did this code successfully run on Leetcode : yes
# Any problem you faced while coding this : took me a little bit to warp my head around singly linked list but i think i get it now


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

newNode = ListNode(data)
if self.head is None:
self.head = newNode
else:
current = self.head
while current.next is not None:
current = current.next
current.next = newNode
return newNode

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 is not None:
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 self.head.data == key:
self.head = self.head.next
return

current = prev = self.head
while current is not None:
if current.data == key:
prev.next = current.next
return
else:
prev = current
current = current.next


ll = SinglyLinkedList()
while True:
print("append <value>")
print("find <value>")
print("remove <value>")
print("quit")
do = input("What would you like to do? ").split()
operation = do[0].strip().lower()
if operation == "append":
ll.append(int(do[1]))
print("Appended", do[1])
elif operation == "find":
result = ll.find(int(do[1]))
if result is None:
print("Not found.")
else:
print("Found:", result.data)
elif operation == "remove":
ll.remove(int(do[1]))
print("Removed", do[1])
elif operation == "quit":
break