ton_chat
Check me out on the web at http://felonyoilchange.dyndns.info/.
Independent Developer in Bellingham, WA
Loading User Information from Channel 9
Something went wrong getting user information from Channel 9
Loading User Information from MSDN
Something went wrong getting user information from MSDN
Loading Visual Studio Achievements
Something went wrong getting the Visual Studio Achievements
Gary Daniels and Evan Goldring - Mock whiteboard problem
Nov 25, 2005 at 10:15 AM#!/usr/bin/ruby
#extend Ruby's built-in String class to allow palindrome checking, then give it a try!
#public domain, november 25th, 2005 by dale anderson
class String
def palindrome?
#normalize the string, result: s
s = self.gsub(/\W/, '').upcase
#string length, half length
sl = s.length
hl = sl / 2
# sl hl p1 p2 SAMPLE FIGURES
# 8 4 0-3 4-7
# 7 3 0-2 4-6
# 6 3 0-2 3-5
# 5 2 0-1 3-4
# 4 2 0-1 2-3
# 3 1 0-0 2-2
# 2 1 0-0 1-1
# 1 0 0-0 0-0 *
# 0 0 0-0 0-0 *
# * we observe that 0 and 1 are special cases
return true if sl <= 1
#compare the p1 versus the backwards p2
p1 = s[ 0 .. (hl-1) ]
p2 = s[ (sl-hl) .. (sl-1) ]
#puts " *** '#{p1}' vs. '#{p2}' ***"
return true if p1 == p2.reverse
return false
end
end
tests = {
'pull-up' => true,
'Oo.pSpOo' => true,
'' => true,
'A' => true,
'4321/abc\ba 1 2 3 4' => true,
"hi there" => false,
'mememe' => false
}
for potpal,georgeboole in tests
raise "Potential Palindrome: '#{potpal}' was #{not georgeboole}!" unless
potpal.palindrome? == georgeboole
end
Thanks for the video.