class Stack:
# Python list implementation of stacks
def __init__(self):
# construct an empty stack
self._data = []
def __str__(self):
# string representation of internal state of the stack
return "TOP: "+ str(self._data)
def __len__(self):
# returns number of items in stack
return len(self._data)
def is_empty(self):
# returns True if stack is empty and False otherwise
return self._data == []
def top(self):
# returns top item of stack it stack is not empty; otherwise returns None
return self._data[0]
def pop(self):
# similar to top, except this also deletes top item from stack.
x = self._data[0]
self._data.remove(x)
return x
def push(self,e):
# puts new item e on top of stack
self._data = [e] + self._data
s1 = Stack()
s1.push(5)
print(s1)
s1.push(8)
print(s1)
s1.push(3)
print(s1)
print(s1.pop())
print(s1)
s = [1,2,3,4,5,6,7,8,9,10]
s[3]
s[3:6]
s[0]
s.remove(s[0])
print(s)
len(s)
s = "Sunderraman"
s.find('ram')
s = [1,2,3]
str(s)
def main():
# implements the Reverse program
while True:
s = input("input a string (q to quit): ").strip()
if s == 'q':
break
st = Stack()
for c in s:
st.push(c)
t = ""
while True:
if st.is_empty():
break
t = t + st.pop()
print("reverse of string: "+t)
main()