Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I will appreciate if i can get help with these python codes because they are tog

ID: 3828149 • Letter: I

Question

I will appreciate if i can get help with these python codes because they are together.

Problem 1A (Stack Implementation) In the previous assignment, you implemented a queue. A stack is a similar type of data structure, but instead of using the first-in-first-out principle of a queue, a stack uses last-in-first out. This means that the next item taken from a stack is the most recent item that was inserted. Operations on a stack occur in the same way as they did for the plate stacking problems that were used in Assignment 1.1. Create a list type variable that will be used to store the stack data in a file called stack.py. Implement the following functions inside that file: 1) push(value): Adds the argument value onto the top of the stack (i.e., the end of the list variable). 2) pop0: If the stack is empty, this function returns None. Otherwise, this function removes and returns the top value from the stack (i.e., the last value in the list variable). 3) is empty 0: Returns True if the stack is empty (i.e., has no items), False otherwise. 4) getlist0: Returns the list that stores the stack data. 5) clear0: Removes all data from the stack. You can use the stacktester.py file from cuLearn to verify the operations of your stack are working as they should be.

Explanation / Answer

stack.py:

#!/usr/bin/env python
# coding=utf-8

class Stack(object):
def __init__(self):
self.stack = list()

def push(self, val):
"""
Push the element to top
:param: int: val
:return: None
"""
self.stack.append(val)

def pop(self):
"""
Pops out the last element and return it
:param: None
:return:
"""
if not self.stack:
return None
return self.stack.pop()

def is_empty(self):
"""
Returns true if stack is empty else false
:param: self:
:return: bool
"""
return not self.stack

def getlist(self):
"""
returns the stack as list
:param: self
:return: list
"""
return self.stack

def clear(self):
"""
Clears the stack
:param: self
:return: None
"""
while(not self.is_empty()):
self.pop()

def top(self):
"""
Returns top of the element
:param: self
:return: int
"""
return self.stack[-1]


if __name__ == '__main__':
obj = Stack()
obj.push(5)
obj.push(9)
obj.push(8)
obj.push(7)
obj.push(6)
obj.push(3)
print(obj.is_empty())
print(obj.pop())
print(obj.pop())
print(obj.clear())
print(obj.pop())
print(obj.is_empty())

validator.py

#!/usr/bin/env python
# coding=utf-8
from stack import Stack

class Validator(object):

def paranthesis(self, s):
"""
Validates the paranthesis and tells if it's valid or not
:param: self:
:param: str: s
:return: str: INVALID or VALID
"""
obj = Stack()
for x in s:
if x in ['(', '{', '[']:
obj.push(x) # if we came across a open braces push it to stack
elif x in [')', '}', ']']:
if obj.is_empty(): # check if stack is empty it's an valid why?? that means we have extra close braces, example ({}))
# it will match ({}) then it has one more ), but stack is empty means it's an invalid right?
return "Invalid"
elif (obj.top() == '(' and x == ')') or (obj.top() == '[' and x == ']') or (obj.top() == '{' and x == '}'):
# if the top of the stack has open braces, then it must be closed first, it means the next character
# should be it's closing braces
obj.pop() # if we found closing tag pop it
else:
return "Invalid" # if it found invalid match, it's an invalid

if not obj.is_empty(): # at the end, if the stack is not empty, means it still have some braces, then INVALID
return "Invalid"

return "Valid" # if it passes all the cases VALID


if __name__ == '__main__':
ob = Validator()
print(ob.paranthesis("{[()]}")) # Valid
print(ob.paranthesis("{[(]}")) # Invalid
print(ob.paranthesis("{[(a+b)]}")) # Valid
print(ob.paranthesis("{a+(b*c)}")) # Valid
print(ob.paranthesis("({[]}{{{{[}}}})")) # Invalid

Instructions:

* Please put both files in the same folder

* then run validator.py

Here's the demo:

Stack: http://ideone.com/cyIl4T

Validator: http://ideone.com/oDAsw3