Three Ways to Solve a Problem

Standard

Introduction

I wanted to show the differences of three different languages.  I made a palindrome detector with Java, Groovy and Python.  Technically the Python I used is CPython so it is not Java but JPython runs in the JVM so I am counting it as a Java related language.  Groovy is Java related because as of Java 1.6 and later, Groovy natively runs on the JVM.  Since this is a Java blog, Java had to be one of the languages used.  I will point out differences and similarities when all of the solutions are posted.  This will not be a explaination of each language.  There are many excellent resources out there.  For example, one place I find myself at a lot is http://www.python.org.  Another site I found myself for this post is http://groovy.codehaus.org/.

Solutions (code)

Requirements

The solution must analyze a string and see if it is a palindrome.  A palindrome is a word or phrase, that spelled the same forwards and backwards.  For example, 123321 is a palindrome.  Two strings will be used to test the solution, “123321” and “1234321.”  This covers the case of a palindrome with an odd number of characters and a even number of characters,  The solution returns the equivalent of a true when a palindrome is detected and the equivalent of false when one is not detected.

Java

public class Palindrome {

private static boolean isPalindrome(String testString) {

String firsthalf;

StringBuilder secondhalf;

int firstHalfIndex;

int secondHalfIndex;

int length = testString.length();

if(length % 2 == 0) {

firstHalfIndex = (int)(length/2);

firsthalf = testString.substring(0, firstHalfIndex);

secondhalf = new StringBuilder(testString.substring(firstHalfIndex));

} else if ( length % 2 == 1) {

firstHalfIndex = (int)(length/2);

secondHalfIndex = firstHalfIndex + 1;

firsthalf = testString.substring(0, firstHalfIndex);

secondhalf = new StringBuilder(testString.substring(secondHalfIndex));

} else {

firsthalf = “”;

secondhalf = new StringBuilder();

}

return firsthalf.equals(secondhalf.reverse().toString());

}

public static void main(String[] argv) {

System.out.println(isPalindrome(“123321”));

System.out.println(isPalindrome(“1234321”));

}

}

Groovy

def isPalindrome(testString){

def firsthalf

def secondhalf

def firstHalfIndex

def secondHalfIndex

if (testString.length() % 2 == 0) {

firstHalfIndex = testString.length()/2

firsthalf = testString[0..<firstHalfIndex]

secondhalf = testString[firstHalfIndex..<testString.length()]

} else if(testString.length() % 2 == 1) {

firstHalfIndex = testString.length()/2

firsthalf = testString[0..<firstHalfIndex]

secondHalfIndex = firstHalfIndex + 1

secondhalf = testString[secondHalfIndex..<testString.length()]

}

firsthalf == secondhalf.reverse()

}

println isPalindrome(“123321”)
println isPalindrome(“1234321”)

Python

def isPalindrome(test_string):

length = len(test_string)

first_half = None

second_half = None

if length % 2 == 0:

first_half_index = int(length/2)

first_half = test_string[:first_half_index]

second_half = test_string[first_half_index:]

elif length % 2 == 1:

first_half_index = int(length/2)

second_half_index = first_half_index + 1

first_half = test_string[:first_half_index]

second_half = test_string[second_half_index:]

return first_half == second_half[::-1]

def main():

print(isPalindrome(“123321”))

print(isPalindrome(“1234321”))

if __name__ == “__main__”:

main()

Thoughts

Similarities

What I found was the same was how easily it was to implement the same solution in three different languages.  Each one did it in their own idiom but it was done.  Each had the ability to figure out the length of the string, split the string by an index and reverse the string.  I am a sucker for slices so Groovy and Python were more fun for me.

Differences

I wasn’t aware of this before I started hitting the blogosphere but one of the complaints of Java is that it is too wordy.  Doing this exercise really exposed me to the wordiness of Java.  Groovy did the same work without having to use a StringBuilder to do a string reverse.  The only reason I did a main on the Python solution is because that is a way to be able to use a file as a library and be a standalone script (It is a Python “idiom” I once read.).  The concept of slices really made the code look neat and tidy for me.  Did I mention I am a sucker for slices? The Groovy reverse extension to Strings was really nice.  Being able to reverse the string with a slice was really cool in Python.

Conclusion

In this post, I compared three languages implementing the same solution.  While the problem was simple, each language succeeded in its own way.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s