2

I want to use the stack method to get reverse string in this revers question.
"Write a function revstring(mystr) that uses a stack to reverse the characters in a string."
This is my code.

from pythonds.basic.stack import Stack
def revstring(mystr):

    myStack = Stack()   //this is how i have myStack

    for ch in mystr:   //looping through characters in my string
        myStack.push(ch)   //push the characters to form a stack

        revstr = ''  //form an empty reverse string
        while not myStack.isEmpty():

            revstr = revstr + myStack.pop()  //adding my characters to the empty reverse string in reverse order
            return revstr

print revstring("martin")

the output seems to print out only the first letter of mystr that is "m" why this??

4
  • 2
    Because your return statement is part of your while block. Which is itself part of your for block. In other words, your indentation needs work. Commented Aug 16, 2016 at 9:41
  • 1
    Can you at least post valid Python code? Python uses # for comments, not //. Commented Aug 16, 2016 at 9:42
  • You need to unindent code that is not part of the loop anymore. Your while loop is executed for each iteration of the for loop. Your return ends both the while and the for loop, because it is part of both. Commented Aug 16, 2016 at 9:43
  • Finally got it!! thanks guys Commented Aug 16, 2016 at 15:50

2 Answers 2

3

Here's 3 solutions to the same problem, just pick one:

1ST SOLUTION

Fixing your solution, you almost got it, you just need to indent properly your blocks like this:

from pythonds.basic.stack import Stack


def revstring(mystr):

    myStack = Stack() # this is how i have myStack

    for ch in mystr: # looping through characters in my string
        myStack.push(ch) # push the characters to form a stack

    revstr = '' # form an empty reverse string
    while not myStack.isEmpty():
        # adding my characters to the empty reverse string in reverse order
        revstr = revstr + myStack.pop()

    return revstr

print revstring("martin")

2ND SOLUTION

This one is structurally the same than yours but instead of using a custom stack, it's just using the builtin python list

def revstring(mystr):

    myStack = [] # this is how i have myStack

    for ch in mystr:
        myStack.append(ch) # push the characters to form a stack

    revstr = '' # form an empty reverse string
    while len(myStack):
        # adding my characters to the empty reverse string in reverse order
        revstr = revstr + myStack.pop()

    return revstr

print revstring("martin")

3RD SOLUTION

To reverse strings, just use this pythonic way :)

print "martin"[::-1]
Sign up to request clarification or add additional context in comments.

Comments

0
  • the while should not be in the for
  • the return should be outside, not in the while

code:

from pythonds.basic.stack import Stack

def revstring(mystr):

    myStack = Stack()      # this is how i have myStack

    for ch in mystr:       # looping through characters in my string
        myStack.push(ch)   # push the characters to form a stack

    revstr = ''            # form an empty reverse string
    while not myStack.isEmpty():
        # adding my characters to the empty reverse string in reverse order
        revstr = revstr + myStack.pop()

    return revstr


print revstring("martin")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.