9 Replies - 6767 Views - Last Post: 16 February 2010 - 07:21 AM Rate Topic: -----

#1 brramiz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 14-April 09

Palindrome Checker using stacks

Post icon  Posted 02 October 2009 - 09:47 PM

i'm making a palindrome checker using stacks, what i'm doing in my code is making 2 stacks of the same thing then reversing one of the stacks, where i'm getting stuck is how to then compare the 2 stacks to see if they would be a palindrome or not.

if you can think of maybe a better way to do this (while still using stacks, it's for a class so i have to use stacks) i would love to hear any better ways thank you!!

here is my code:

from stack import Stack
stack = Stack()
stack2 = Stack()
def palindromeChecker(phrase):
   
	for ch in phrase:
		if 'A'<=ch<='Z' or 'a'<=ch<='z' or '16'<=ch<='25':
			stack.push(ch)
	for letters in phrase:
		if 'A'<=letters<='Z' or 'a'<=letters<='z' or '16'<=leters<='25':
			stack2.push(letters)
		stack2.reverse()



Is This A Good Question/Topic? 0
  • +

Replies To: Palindrome Checker using stacks

#2 Stutzbach  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 29
  • Joined: 06-July 08

Re: Palindrome Checker using stacks

Posted 03 October 2009 - 05:04 AM

"stack" isn't one of the modules included in the Python library. If you want an answer on how to use the module, it would be helpful if you told us where the module came from. :D
Was This Post Helpful? 0
  • +
  • -

#3 brramiz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 14-April 09

Re: Palindrome Checker using stacks

Posted 03 October 2009 - 07:42 AM

ok, i've created "stacks" using lists with the class below that's how i have stacks but i cant use the slice operator to compare them either

class Stack():
	"""Top of the stack is at the end of the list"""
	def __init__(self):
		self._items = []

	def push(self,obj):
		self._items.append(obj)

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

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

	def isEmpty(self):
		return len(self._items)==0

	def __len__(self):
		return len(self._items)

	def __str__(self):
		return "bottom "+str(self._items)+ " top"

	def reverse(self):
		return self._items.reverse()


Was This Post Helpful? 0
  • +
  • -

#4 Nallo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 255
  • Joined: 19-July 09

Re: Palindrome Checker using stacks

Posted 03 October 2009 - 10:16 AM

Ah, so the problem is how to compare two stacks.

Think of the stacks as 2 decks of cards face down. Lets say you are only allowed to turn the top cards of both decks and put them away afterwards (you are allowed to repeat that). So how do you test if the order of cards in both decks is the same?

You turn the top cards and compare them. If they are unequall then the order of cards is different. If they are equall you put them away and turn the top cards of the remaining decks. You compare them .... Do this untill one deck is empty or you find an unequality.

The problem to compare two stacks is exactly the same.


A side note: I am a bit surprised, the your stack class has a reverse method. Usually stacks dont have a reverse method. Only push, pull, isempty, maybe peek and length.

This post has been edited by Nallo: 03 October 2009 - 10:29 AM

Was This Post Helpful? 0
  • +
  • -

#5 programble  Icon User is offline

  • (cons :dic :head)

Reputation: 49
  • View blog
  • Posts: 1,315
  • Joined: 21-February 09

Re: Palindrome Checker using stacks

Posted 12 October 2009 - 03:24 PM

Simply pop a value from each stack and compare them. If all the pairs are equal, it is a palindrome.
Was This Post Helpful? 0
  • +
  • -

#6 Tshiknn  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 20
  • Joined: 18-October 09

Re: Palindrome Checker using stacks

Posted 18 October 2009 - 06:06 PM

Simple enough.

def stack_equals(stack1, stack2):
	while True:
		try:
			if stack1.pop() != stack2.pop():
				return False
		except IndexError:
			return True


Was This Post Helpful? 0
  • +
  • -

#7 Guest_nenette jazmin*


Reputation:

Re: Palindrome Checker using stacks

Posted 15 February 2010 - 01:13 AM

make a palindrome using only stacks not string
Was This Post Helpful? 0

#8 chemicalfan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 88
  • Joined: 16-October 09

Re: Palindrome Checker using stacks

Posted 15 February 2010 - 04:39 AM

Couldn't you implement Stack as a deque? That way, you could just use the one object rather than two - would be more memory efficient (ok, in this small scale it's irrelevant!)
Was This Post Helpful? 0
  • +
  • -

#9 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: Palindrome Checker using stacks

Posted 15 February 2010 - 05:27 AM

Hey, guys, this topic is half a year old. It was necro'd by that guest. >.< Just thought you ought to know.
Was This Post Helpful? 0
  • +
  • -

#10 chemicalfan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 88
  • Joined: 16-October 09

Re: Palindrome Checker using stacks

Posted 16 February 2010 - 07:21 AM

View PostDogstopper, on 15 February 2010 - 04:27 AM, said:

Hey, guys, this topic is half a year old. It was necro'd by that guest. >.< Just thought you ought to know.

:rolleyes:
Doh! Clearly still noob at this forum!
Lock please mods!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1