9 Replies - 984 Views - Last Post: 07 February 2013 - 03:22 AM Rate Topic: -----

#1 medaa   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 24-October 12

Reversing a string

Posted 06 February 2013 - 04:47 PM

class Stack:
     def __init__(self):
          self.items=[]
          
     def isEmpty(self):
         return self.items==[]

     def push(self,item):
         self.items.append(item)

     def pop(self):
         return self.items.pop()

     def peek(self):
         return self.items[len(self.items)-1]

     def size(self):
         return len(self.items)
    
s=Stack()
line=input("Enter a string:")
print(line[::-1])
s.push(line)
print(s.pop())



I want this code to reverse the string:

Enter a string:how are you?
uoy era woh

How come its poping it out in order:
woh era uoy??

I figured it out, need to assign a new variable

Is This A Good Question/Topic? 0
  • +

Replies To: Reversing a string

#2 darek9576   User is offline

  • D.I.C Lover

Reputation: 204
  • View blog
  • Posts: 1,747
  • Joined: 13-March 10

Re: Reversing a string

Posted 06 February 2013 - 04:59 PM

I think your pushing onto the stack should be done in a loop.

class Stack:
     def __init__(self):
          self.items=[]
          
     def isEmpty(self):
         return self.items==[]

     def push(self,item):
         self.items.append(item)

     def pop(self):
         return self.items.pop()

     def peek(self):
         return self.items[len(self.items)-1]

     def size(self):
         return len(self.items)
    
s=Stack()
line= input("Enter a string:")
print(line[::-1])

for character in line:
     s.push(character)
     

#printing the values back
while not s.isEmpty():
     print(s.pop(), end = " ")





Was This Post Helpful? 0
  • +
  • -

#3 medaa   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 24-October 12

Re: Reversing a string

Posted 06 February 2013 - 05:06 PM

s=Stack()
line=input("Enter a string:")
r=(line[::-1])
s.push(r)
print(s.pop())


I made a variable,r, and pushed it into the stack and it works.

Thanks anyways
Was This Post Helpful? 0
  • +
  • -

#4 darek9576   User is offline

  • D.I.C Lover

Reputation: 204
  • View blog
  • Posts: 1,747
  • Joined: 13-March 10

Re: Reversing a string

Posted 06 February 2013 - 05:13 PM

Then why are you using the stack to reverse a string? string[::-1] already does it for you and you are using it.
Was This Post Helpful? 0
  • +
  • -

#5 medaa   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 24-October 12

Re: Reversing a string

Posted 06 February 2013 - 05:16 PM

Im having a little trouble with this program, I have a file that contains:
print(“}”)
number_list = [5, 12, 54, 4, 5]
print(number_list[2])
list=[]

I want my program to read check parenthesis and return

Enter the file name: test1.py
() pairs: 2
{} pairs: 1
[] pairs: 2
All () matched.
Not all {} matched.
All [] matched.

But i dont know how to do this using stacks

class Stack:
     def __init__(self):
          self.items=[]
          
     def isEmpty(self):
         return self.items==[]

     def push(self,item):
         self.items.append(item)

     def pop(self):
         return self.items.pop()

     def peek(self):
         return self.items[len(self.items)-1]

     def size(self):
         return len(self.items)
    
f=input("Enter file name:")
file=open(f,"r")

Was This Post Helpful? 0
  • +
  • -

#6 Simown   User is offline

  • Blue Sprat
  • member icon

Reputation: 323
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Reversing a string

Posted 06 February 2013 - 05:35 PM

You will need 3 stacks, one for each form of parenthesis. The next step would be to push any open parentheses on to the correct stack and then push when you see a close parenthesis. If the stack is non-empty when you fully finish parsing the file, there is a pair that does not correctly match. To count the number of pairs, you could count the number of pushes and pops.

As darek9576 said, your first use of a stack to reverse a string is not correct, you are reversing it with [::1] then simply pushing it and popping it from the stack, unmodified.

This post has been edited by Simown: 06 February 2013 - 05:36 PM

Was This Post Helpful? 0
  • +
  • -

#7 medaa   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 24-October 12

Re: Reversing a string

Posted 06 February 2013 - 05:41 PM

s=Stack()
p=Stack()
r=Stack()
f=input("Enter file name:")
file=open(f,"r")

for line in file:
     if symbol=="[":
          s.push(symbol)
     else:
          s.pop()


Can I make three if and elif statements
Was This Post Helpful? 0
  • +
  • -

#8 Simown   User is offline

  • Blue Sprat
  • member icon

Reputation: 323
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Reversing a string

Posted 06 February 2013 - 05:43 PM

You can if you want, try it out and see where you get!
Was This Post Helpful? 0
  • +
  • -

#9 medaa   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 24-October 12

Re: Reversing a string

Posted 06 February 2013 - 05:49 PM

s=Stack()
p=Stack()
r=Stack()
f=input("Enter file name:")
file=open(f,"r").read()
for symbol in file:
     if symbol=="[":
          s.push(symbol)
     elif symbol=="]":
          s.pop()
     elif symbol=="(":
          r.push(symbol)
     elif symbol==")":
          r.pop()
     elif symbol=="{":
          p.push(symbol)
     elif symbol=="}":
          p.pop()


line 37, line 12, in pop
builtins.IndexError: pop from empty list
Was This Post Helpful? 0
  • +
  • -

#10 Simown   User is offline

  • Blue Sprat
  • member icon

Reputation: 323
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Reversing a string

Posted 07 February 2013 - 03:22 AM

It's not enough to just do that, you need to check if the stack is empty before popping, (use isEmpty), if the stack is empty when you try to pop, that must mean there is a close parenthesis without a matching opening one.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1