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!
if (line == 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
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
···
On Fri, 2 Sep 2005, Josh Charles wrote:
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
···
On Sep 1, 2005, at 10:06 AM, Josh Charles wrote:
[snip]
palindrome = 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
p "Win a Toyota blah".match(palindrome).to_s
p "Why Abba rocks?".match(palindrome).to_s
p "A Mismatch".match(palindrome).to_s
···
On 9/1/05, Josh Charles <josh.charles@gmail.com> wrote:
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)
--
Simon Strandgaard
The newline is included and is removed by chomp.
Peter
gosh, I knew it had to be something stupid. Thanks!
palindrome = 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
p "Win a Toyota blah".match(palindrome).to_s
p "Why Abba rocks?".match(palindrome).to_s
p "A Mismatch".match(palindrome).to_s
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.
Simon Strandgaard <neoneye@gmail.com> writes:
palindrome = 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
p "Win a Toyota blah".match(palindrome).to_s
p "Why Abba rocks?".match(palindrome).to_s
p "A Mismatch".match(palindrome).to_s
And what if I have a palindrome longer than eight chars?
···
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org
Such as http://www.cs.usm.maine.edu/~welty/fun/palindromes.html
I wonder if there are more 2-dimensional palindromes than the 3 listed on that page; that would be an interesting little quiz to write.
···
On Sep 2, 2005, at 6:23 AM, Christian Neukirchen wrote:
And what if I have a palindrome longer than eight chars?