Skip to content

Instantly share code, notes, and snippets.

@sumedhe
Created July 9, 2016 10:59
Show Gist options
  • Select an option

  • Save sumedhe/fe15f56b1829e907b2ffa65e640f4a9c to your computer and use it in GitHub Desktop.

Select an option

Save sumedhe/fe15f56b1829e907b2ffa65e640f4a9c to your computer and use it in GitHub Desktop.
reverse a string using stack
class Stack:
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def peek(self):
return self.items[0]
def isEmpty(self):
return len(self.items) == 0
def reverse(text):
s = Stack()
for c in text:
s.push(c)
revText = ""
while not s.isEmpty():
revText += s.pop()
return revText
##test
print(reverse("abcdef"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment