Skip Navigation
Primary Navigation
Channel 9 Home
Site Navigation
All Content
Shows
Events
Toggle Search
Search Channel 9
Search
Sign In
ton_chat
ton_chat
Biography
Niner since 2005
Independent Developer in Bellingham, WA
Check me out on the web at http://felonyoilchange.dyndns.info/.
Comments
Gary Daniels and Evan Goldring - Mock whiteboard problem
Try using my new non-KISS, très l33t solution to check any String for palindromosity. I'm sure Matz should add this to 1.8.4.
#!/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.
Comments
Gary Daniels and Evan Goldring - Mock whiteboard problem
#!/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.