Skip to content

Instantly share code, notes, and snippets.

@Abhayparashar31
Created September 30, 2020 03:18
Show Gist options
  • Save Abhayparashar31/9c3c0dad38cf946b53d9c408ea391334 to your computer and use it in GitHub Desktop.
Save Abhayparashar31/9c3c0dad38cf946b53d9c408ea391334 to your computer and use it in GitHub Desktop.
"""
String : Python
reversed string : nohtyp
python
[p,y,t,h,o,n]
""+n = n
"n"+o = no
'nohtyp'
"""
class Stack():
def __init__(self):
self.items = [] ## Empty list intilize
def push(self,item):
self.items.append(item) ## simple appending to list
def pop(self):
return self.items.pop() ## Removing top element of the stack
def is_empty(self):
return self.items==[] ### Checking whether the stack is empty or not
def peek(self):
if not self.is_empty():
return self.items[-1] ## returnign top element using [-1]=last element
def show_stack(self):
return self.items ## Printing all the items
def reverse_strings(string):
s = Stack() ## object for stack class
for i in range(len(string)): ## Loop from 0 to len of string
s.push(string[i]) ## Push each character onto string
reverse_string = "" ## Create a empyt reverse string
while not s.is_empty(): ## while loop until the stack is empty or not
reverse_string += s.pop() ## pop top element and concatenate with the reverse string
return reverse_string ## Return reverse string
string = str(input("Enter a string : ")) ## Taking string as input
print(reverse_strings(string)) ## Printing reverse string as output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment