Write a program to implement a Stack using Array. Your task is to use the class as shown in the comments in the code editor and complete the functions push() and pop() to implement a stack. 
Example 1:
Input: 
push(2)
push(3)
pop()
push(4) 
pop()
Output: 3, 4
Explanation: 
push(2)    the stack will be {2}
push(3)    the stack will be {2 3}
pop()      poped element will be 3,
           the stack will be {2}
push(4)    the stack will be {2 4}
pop()      poped element will be 4
Example 2:
Input: 
pop()
push(4)
push(5)
pop()
Output: -1, 5
Your Task:
You are required to complete two methods push() and pop(). The push() method takes one argument, an integer 'x' to be pushed into the stack and pop() which returns an integer present at the top and popped out from the stack. If the stack is empty then return -1 from the pop() method.
Expected Time Complexity : O(1) for both push() and pop().
Expected Auixilliary Space : O(1) for both push() and pop().
Constraints:
1 <= Q <= 100
1 <= x <= 100

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3



class MyStack:

    

    def __init__(self):

        self.arr=[]

    

    #Function to push an integer into the stack.

    def push(self,data):

        #add code here

    

    #Function to remove an item from top of the stack.

    def pop(self):

        #add code here

        

        

Solutions:
class MyStack:

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

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

	def pop(self):
		if self.arr == []:
			return -1
		else:
			return self.arr.pop()


class MyStack:

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

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

	def pop(self):
		if len(self.arr) > 0:
			return self.arr.pop()
		return -1


class MyStack(object):

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

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

	def pop(self):
		if self.stack:
			return self.stack.pop()
		return -1


class MyStack:

	def __init__(self):
		self.arr = []
		self.top = -1

	def isEmpty(x):
		if len(x) == 0:
			return True
		else:
			return False

	def push(self, data):
		self.top += 1
		self.arr.append(data)
		self.arr[self.top] = data

	def pop(self):
		if self.top == -1:
			return self.top
		if self.top != -1:
			self.top -= 1
			return self.arr[self.top + 1]
			self.arr.pop()


from sys import maxsize

class MyStack:

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

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

	def pop(self):
		if len(self.arr) == 0:
			return -1
		else:
			return self.arr.pop(-1)


class MyStack:

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

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

	def pop(self):
		n = len(self.arr)
		if len(self.arr) == 0:
			return -1
		else:
			item = self.arr[n - 1]
			del self.arr[n - 1]
			return item


class MyStack:

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

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

	def pop(self):
		if not self.is_empty():
			return self.stack.pop()
		else:
			return -1

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

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

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