Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Stack:
- def __init__(self):
- self.items = []
- def isEmpty(self):
- return self.items == []
- def push(self, item):
- self.items.append(item)
- def rpush(self, item):
- self.items.append(item)
- return self
- def pop(self):
- return self.items.pop()
- def penultimate_element(self):
- return self.items[len(self.items) - 1]
- def size(self):
- return len(self.items)
- def __str__(self):
- return str(self.items)
- def __add__(self, other):
- if type(other) is int or type(other) is float:
- return self.rpush(other)
- elif type(other) is type(self):
- for i in range(len(other.items)):
- self.push(other.items[i])
- return self
- else:
- return 'ERROR'
- def clearall(self):
- self.items.clear()
- stack1 = Stack()
- stack2 = Stack()
- if stack1.isEmpty():
- print('Empty')
- stack1.push(1)
- stack1.push(2)
- stack1.push(3)
- stack1.push(4)
- stack2.push(2)
- stack2.push(2)
- stack2.push(2)
- stack2.push(2)
- print(stack1.penultimate_element())
- print(stack1)
- print(stack2)
- result = stack1 + stack2
- print(result)
- print(type(result))
- result.clearall()
- print(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement