Thursday, September 18, 2014

Palindrome Algorithm

This is my personal attempt to identify if a word (or series of words) is a palindrome.
Written in java, it's an optimal solution.

public class PalindromeTest {

    public static void main(String[] args) {

        for (String input : args) {

            if (isValid(input)) {
                int mid = input.length()/2;
                String firstHalf = input.substring(0, mid);
                String secondHalf = input.substring(getProperSecondHalfMid(input.length()), input.length());

                System.out.println(input + " is a palindrome: " + areStringsEqual(firstHalf, getReverseString(secondHalf)));
            } else {
                System.out.println("Sorry, input: " + input + ", is not valid!");
            }

        }

    }

    private static int getProperSecondHalfMid(int length) {
        return length%2 == 0 ? length/2: length/2 + 1;
    }

    private static String getReverseString(String secondHalf) {
        return (new StringBuffer(secondHalf).reverse().toString());
    }

    private static String areStringsEqual(String firstHalf, String secondHalf) {
        return Boolean.toString(firstHalf.equalsIgnoreCase(secondHalf));
    }

    private static boolean isValid(String input) {
        return !input.isEmpty();
    }

}



Friday, September 12, 2014

Sample Template on Starting a Single Java Project Using Gradle

Skip to the steps

You always want to program or create your own implementation of anything in your mind or anything out there.
So you always do a simple project.
This article reveals my template on how I always start my simple projects.

For some, creating one file and running it is enough for just experimenting a functionality, like
$ python Starter.py
$ php Starter.php
$ java Starter

That's it, one class and their done.

But for me, I like to keep my projects on industry standard even if they are just small or simple. I create a template structure before I start my implementation classes. Well you just don't know when you want to scale up any of your projects. Plus, you know how it is today with companies they throw words around like clean code, coverage, automation and cloud repos (like github). With these you must have by default structure, unit tests, source control manager (scm) and a build tool in your project. Java language has always been my strongest suit, so i'll create this starter demo project using Java. Also, gradle for my build tool and git for my scm.

Just before we start, I just want to address why I chose gradle and git.

For build tool, maven and gradle are, hands down, the most recommended. But after seeing their dependency declarations

Maven
---------













Gradle
--------




It's kinda obvious now right? Readability and maintainability, gradle it is. I never went back to Maven.

For SCM. Distributed repository is the way to go. Keeping them locally in your computer gives you a lot more power and more efficiency in execution of usual tasks like branching and managing your commits and merges. I never wanted to go back to CVS or SVN. I got caught up with the git bandwagon so it's what I stuck into as the scm for distributed repository. There is also that github mania. Maybe later on I will try others like Mercurial.

Creating a project from an advance IDE like Eclipse, Netbeans or Intellij is fine, but don't you want to know how it is done under the hood? Also, do you notice there are a lot of files added to your project upon creation? You can keep those auto generated files to a minimum by creating your project manually from scratch.

Okay... let's start.
Should just take about 10 mins for Mac or Linux, without the downloads.
I made this through Mac, but the principle should be similar to any Linux and Windows OS.

1) If you don't have them yet, install your tools: java, gradle, git
- for java, use JEM, the Java Environment Manager to install and manage your java versions: https://www.npmjs.org/package/jem
For windows, just download and install a version you like.
- for gradle, I used a package manager called homebrew
$ brew install gradle
- for git
$ brew install git

2) Create a standard folder structure.

$ mkdir gradle-starter-project
$ cd gradle-starter-project
$ mkdir -p src/main/java
$ mkdir -p src/test/java
$ mkdir -p src/main/resources
$ mkdir -p src/test/resources

3) Create 'build.gradle' file in your root project directory, it's your simplified pom.xml.
$ touch build.gradle

4) Add the java plugin on build.gradle
$ vim build.gradle
apply plugin: 'java'

Then save your file.
:wq

(This plugin is all you need to clean, execute your test classes, compile, download dependencies and bundle your project into a jar file with one simple command:
'$ gradle build')

5) Lastly, activate source control management for your project.
$ git init

This transforms your project directory into a git repository.

And voila, you're done!
This is how I start whatever single project I have, even the simple ones.

This is how your project folder structure should end up:













There, the template where I start my simple java projects.