palindrome finder

J

Josh Charles

I've been working on this piece of code and it's starting to drive me
crazy. I think it's a problem with reference vs value type, but I'm
not sure. I'm just writing a simple program to process a list of
words and print out all the palindromes (words that read the same
forward and backward)

Here is my code:

file =3D File.open("testdict.txt", "r" ) do |file|
file.each_line("\n") do |line|
#filelist.push( line )
line.downcase!
if (line =3D=3D line.reverse)
puts line
end
end
end

I've made sure each line is being read properly, and it is. The if
statement never returns true, however. I'm new to ruby, so I'm
probably just doing something really stupid, but I can't figure it out
yet.

Josh
 
P

Peter Vanbroekhoven

I've been working on this piece of code and it's starting to drive me
crazy. I think it's a problem with reference vs value type, but I'm
not sure. I'm just writing a simple program to process a list of
words and print out all the palindromes (words that read the same
forward and backward)

Here is my code:

file = File.open("testdict.txt", "r" ) do |file|
file.each_line("\n") do |line|
#filelist.push( line )
line.downcase!
line.chomp!

if (line == line.reverse)
puts line
end
end
end

The newline is included and is removed by chomp.

Peter
 
J

James Edward Gray II

I've been working on this piece of code and it's starting to drive me
crazy.

No worries, it's a simple mistake with an easy fix.

The Problem:

Each line read has a "\n" at the end of it, which is making your test
fail, because "wow\n" does equal "\nwow".

The fix:

Strip the newline, like so
Here is my code:

file = File.open("testdict.txt", "r" ) do |file|
file.each_line("\n") do |line|
#filelist.push( line )

line.strip! # removes all whitespace at the front and
back of the line
line.downcase!
if (line == line.reverse)
puts line
end
end
end

Just FYI, you can also simplify the above a little. Here's an example:

Neo:~$ cat testdict.txt
nothing
wow
Neo:~$ cat palidrome.rb
ARGF.each_line do |line|
line.strip!
line.downcase!

puts line if line == line.reverse
end
Neo:~$ ruby palidrome.rb testdict.txt
wow

Hope that helps.

James Edward Gray II
 
S

Simon Strandgaard

I've been working on this piece of code and it's starting to drive me
crazy. I think it's a problem with reference vs value type, but I'm
not sure. I'm just writing a simple program to process a list of
words and print out all the palindromes (words that read the same
forward and backward)
[snip]

palindrome =3D Regexp.new(<<'EOPALIN')
(?x)(?i)\b
=09(?:(\S) (?:\s|\p)*
=09=09(?:\S|(\S) (?:\s|\p)*
=09=09=09(?:\S|(\S) (?:\s|\p)*=20
=09=09=09=09(?:\S|(\S) (?:\s|\p)*=20
=09=09=09=09\4)? (?:\s|\p)*=20
=09=09=09\3)? (?:\s|\p)*
=09=09\2)? (?:\s|\p)*
=09\1)
\b
EOPALIN

p "Win a Toyota blah".match(palindrome).to_s
p "Why Abba rocks?".match(palindrome).to_s
p "A Mismatch".match(palindrome).to_s
 
J

Josh Charles

palindrome =3D Regexp.new(<<'EOPALIN')
(?x)(?i)\b
(?:(\S) (?:\s|\p)*
(?:\S|(\S) (?:\s|\p)*
(?:\S|(\S) (?:\s|\p)*
(?:\S|(\S) (?:\s|\p)*
\4)? (?:\s|\p)*
\3)? (?:\s|\p)*
\2)? (?:\s|\p)*
\1)
\b
EOPALIN
=20
p "Win a Toyota blah".match(palindrome).to_s
p "Why Abba rocks?".match(palindrome).to_s
p "A Mismatch".match(palindrome).to_s
=20

Ok, I"m not even going to pretend to know how this code works exactly,
and was suitibly impressed when I ran it. I don't know what to say
other than that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,170
Latest member
Andrew1609
Top