Quantcast
 Friday, May 23, 2008

Jacob is doing quite a bit more programming these days. In addition to some of his self-study of Ruby he is enrolled in a game programming class at Sherwood High School where he is refining his skills with GameMaker and learning how to write simple games using VB.Net.

A few weeks back we were working on a small programming problems together - how to detect if a given string is a palindrome (i.e., reads the same both forwards and backwards). I start by walking Jacob through some simple string operations whereby you can walk through a string character by character forwards and backwards, then showing him how to loop and walk his way into the middle, testing for equivalence along the way.

Jacob then says something to the effect of "Dad, why don't you just call reverse on the string and see if it is the same as the original."

Oh. I guess that could work. If you were into that sort of elegance and time-saving. Whatever floats your boat I guess.

Saturday, May 24, 2008 7:51:55 AM (Pacific Daylight Time, UTC-07:00)
Absolutely brilliant! And if you're anything like me that will just reinforce the feeling that your kids are much smarter than you!
Saturday, May 31, 2008 8:55:27 PM (Pacific Daylight Time, UTC-07:00)
var isPalindrome = function(s){return (s.toUpper() === s.toUpper.replace(/([A-Z])/g, '$1,').replace(/,$/, '').split(',').reverse().join(''));}

alert(isPalindrome('racecar'));
Saturday, May 31, 2008 9:03:35 PM (Pacific Daylight Time, UTC-07:00)
Oops, toUpper should be toUpperCase.
Sunday, June 01, 2008 12:40:20 AM (Pacific Daylight Time, UTC-07:00)
Something like that. We did it in ruby:

def is_palindrome?(s)
s == s.reverse
end

puts " Please enter your two digit number."
num = gets.chomp
sum = num.to_i

while !is_palindrome?(sum.to_s)
puts "uh oh - " + sum.to_s + " is not yet a palindrome, so add it to " + sum.to_s.reverse
sum = sum + (sum.to_s.reverse).to_i
end

puts "found our palindrome: " + sum.to_s
Comments are closed.