Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Precourse-1 completed #2004

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
52 changes: 33 additions & 19 deletions Exercise_1.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
# Time Complexity : O(1) for isEmpty, push, pop, peek, size
# Space Complexity : O(n) for storing elements in stack
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.stack = []
self.count = 0

def isEmpty(self):
return self.count == 0

def push(self, item):
self.stack.append(item)
self.count += 1

def pop(self):
if self.count >= 1:
self.count -= 1
return self.stack.pop()
else:
return None

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

def size(self):
return self.count

def show(self):
return self.stack


s = myStack()
s.push('1')
s.push('2')
s.push("1")
s.push("2")
print(s.pop())
print(s.show())
49 changes: 31 additions & 18 deletions Exercise_2.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,45 @@

# The intution here is like inserting in reverese order
# Time Complexity : O(1) for push, O(n) for pop
# Space Complexity : O(1) for storing n nodes in 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 # Stack is empty
pop_value = self.head.data # Store the value to return
self.head = self.head.next # Move the top pointer down
return pop_value


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"
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
40 changes: 36 additions & 4 deletions Exercise_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,63 @@ 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):
"""
Create a new singly-linked list.
Takes O(1) time.
"""
self.head = None
self.head = ListNode()

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

curr = self.head
while curr.next:
curr = curr.next
curr.next = 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.
"""

curr = self.head.next
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.next
while curr.next:
if curr.next.data == key:
curr.next = curr.next.next
return
curr = curr.next


sl = SinglyLinkedList()
for i in range(5):
sl.append(i)
sl.remove(3)
sl.append(7)
print("Found key {}".format(sl.find(2)))
sl.remove(2)
if sl.find(2) is None:
print("Not found")