I was doing the first of three steps in the Greplin programming challenge the other night in PHP before I went to sleep, just for the fun of it. I found the link via Hacker News (love that site) and there are a lot of really neat solutions if you read the comments. Much much better ones than mine.
The challenge was basically to find the longest palindrome (a word, phrase, number or other sequence of units that can be read the same way in either direction) in a given string. I re-wrote the thing the day after in Java (I thought it was a good exercise for me since I’m taking a Java course in school at the moment) and here’s how it turned out:
import java.util.ArrayList;
public class PalindromeFinder {
public static void main(String[] args) {
String text = "FhourscoreandsevenyearsagoourfaathersbroughtforthonthiscontainentanewnationconceivedinzLibertyanddedicatedtothepropositionthatallmenarecreatedequalNowweareengagedinagreahtcivilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth";
ArrayList results = new ArrayList();
for (int blockSize = text.length(); blockSize != 1; blockSize--)
{
for (int x = 0; x != text.length() - blockSize; x++) {
String stringPiece = text.substring(x, x + blockSize);
if (stringPiece.equals(new StringBuilder(stringPiece).reverse().toString())) {
results.add(stringPiece);
// Break here if you only want to find largest palindrome.
}
}
}
System.out.println(results.get(0));
}
}
I did the same thing in Ruby today. Just thought it would be fun learning it.
string = "FhourscoreandsevenyearsagoourfaathersbroughtforthonthiscontainentanewnationconceivedinzLibertyanddedicatedtothepropositionthatallmenarecreatedequalNowweareengagedinagreahtcivilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth" results = Array.new for i in 2 .. string.size for j in 0 .. string.size - i piece = string[j,i] if piece == piece.reverse results << piece end end end puts results[-1]
Note: It should be “results < < piece" without a blank space between the < but something gets screwed with the markup.
