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

Completed Pre-course 1 #2009

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
43 changes: 39 additions & 4 deletions Exercise_1.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,57 @@
# Time Complexity :
# All the methods defined in below have time complexity O(1) except show method, show method prints the stack so it has time complexity of O(n) where n being the number of elements in the stack
# Space Complexity :
# Space complexiy for all the methods is O(1) since its constant like length, pop or peek will return just one single element. However for the show method it would be O(n) were n being the number of elements in the stack.
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : None

class myStack:
#Please read sample.java file before starting.
#Kindly include Time and Space complexity at top of each file
def __init__(self):
self.__stack = [] # here I am defining stack as a list

def isEmpty(self):
if self.__stack:
return False
else:
return True

def push(self, item):
# inserting element at the top of stack meaning end of list
self.__stack.append(item)

def pop(self):
# removing last element from the list
# first check if list is empty
if not self.__stack:
# stack is empty
raise Exception("pop can not be done on empty stack")
else:
# stack is not empty
# remove the last element from the list
return self.__stack.pop()


def peek(self):
# peek will return the top value from the stack without removing it
if not self.__stack:
# stack is empty
raise Exception("peek is called on empty stack")
else:
# stack has some values
# get its length
peek_index = len(self.__stack) - 1
return self.__stack[peek_index]


def size(self):
# size of stack would be length of list
return len(self.__stack)

def show(self):


# return the stack
return self.__stack


s = myStack()
s.push('1')
s.push('2')
Expand Down
36 changes: 36 additions & 0 deletions Exercise_2.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,49 @@
# Time Complexity :
# All the methods defined in below have time complexity O(1) except show method, show method prints the stack so it has time complexity of O(n) where n being the number of elements in the stack
# Space Complexity :
# Space complexiy for all the methods is O(1) since its constant like length, pop or peek will return just one single element. However for the show method it would be O(n) were n being the number of elements in the stack.
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : I had to revise concepts on linked list.

class Node:

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

class Stack:
def __init__(self):
# stack is node, like linked list so initially it would be empty
self.top = None # Basically I am initilizaling a node named top which is None.
# top is just a variable

def push(self, data):
# inserting data
# create new node
new_node = Node(data)
new_node.next = self.top # linking new node to top, so its basically (0)newNode --> top
# make the new node as top
self.top = new_node

def pop(self):
# first check if stack is empty
if self.top is None:
# stack is empty
raise Exception("pop can not be done on empty stack")
else:
pop_data = self.top.data
# unlink that top mode
# plus one the pointer of stack
self.top = self.top.next

def show(self):
# Traverse the stack and collect all values into a list
elements = []
current = self.top
while current is not None:
elements.append(current.data)
current = current.next
return elements

a_stack = Stack()
while True:
Expand All @@ -28,5 +62,7 @@ def pop(self):
print('Stack is empty.')
else:
print('Popped value: ', int(popped))
elif operation == 'show':
print(a_stack.show())
elif operation == 'quit':
break