Posts Tagged ‘PHP’

Palindrome finder in Java (+ Ruby)

October 10th, 2010

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.

Checkpoint

May 12th, 2010

Checkpoint is a traffic and gate management system designed for logistic companies. It is used to schedule and control incoming line haul traffic, display gate statuses among other things. The software is designed to run on a variety of devices such as truck-mounted touchscreen PCs.

In addition to this, Checkpoint is also communicating and providing information with two internal systems such as a Windows Mobile PDA interface.

Technical specifications:

  • PHP 5
  • MySQL 5.1 (code is database independent)
  • JavaScript
  • Runs on Apache 2 & IIS
  • ~7.000 lines of code
  • Truck-mounted PC interface

Gallery

Return:Customer

May 12th, 2010

Return:Customer is a light-weight unified parcel tracker currently used at 11 different locations across Scandinavia. It provides a variety of functionality such as parcel scanning, track & trace, customizable statistics, e-mail reporting and financial data.

It’s currently tracking roughly 500.000 parcels with 15.000 new entries added monthly.

Technical specifications:

  • PHP 5 + GD
  • MySQL 5.1 (code is database independent)
  • JavaScript
  • Runs on Apache 2 and IIS
  • ~ 5.000 lines of code
  • ~ 100 users
  • Compatible with IE6+, FF2+, Opera & Chrome.

Gallery