13 Jul PMCA Saturday 18:30 Python Homework 21.07.10.
Question:
Input a string. Using a stack, reverse the string and print it.
Sample Input 1: Enter String to reverse : Apple Sample Output 1 elppA Sample Input 2 Orange Sample Output 2 eganrO Hint: Below is the stack class, please use it to reverse the string
class Stack:
# we are using a list to create a stack
# so when we create a new stack object, we will start
# with empty list
def __init__(self):
self.s = []
def push(self,item):
# use the list method to put the item into the list
self.s.append(item)
def pop(self):
# get the item at the top
# print that item
# delete that from that index in the
return self.s.pop()
def peek(self):
# print the item at the top of the stack
# do not delete it
return self.s[-1]
def isEmpty(self):
# check if the stack is empty
return len(self.s) == 0
def size(self):
# print the size of the stack
return len(self.s)
Sorry, the comment form is closed at this time.