Wednesday, July 18, 2012

String Palindrome Algorithm

The last of these old school algorithms. Funny how trivial they should be after all these years but they still trip me up. Hence these posts and practices!

class Pal {



 public static void main(String args[]) {

  //System.out.println(findPalIter(args[0]));
  System.out.println(findPalRec(args[0]));


 }



 public static boolean findPalIter(String str) {

  char[] orig = str.toCharArray();
  char[] backward = new char[orig.length];

  int j=0;
  for( int i=orig.length-1; i >=0; i-- ) {


   backward[j++] = orig[i];



  }

  if( str.equals( new String(backward)))
   return true;


  return false;




 }


 private static boolean findPalRec(String str) {

  char[] chars = str.toCharArray();

  if( chars.length == 0 || chars.length == 1 ) 
   return true;

  if( chars[0] == chars[chars.length-1])
   return findPalRec( str.substring( 1, chars.length-1 ) );
  else
   return false;

 }







}


https://github.com/asharif/StringPalindrome

String Anagram Algorithm

As a continuation of the last post, here is an algorithm for finding anagrams from a string
public class Anag {



        public static void main(String args[]){

                char[] anag = new char[args[0].length()];
                findAnag(args[0], new String(anag), 0);




        }



        public static void findAnag(String orig, String anag, int index) {
                if ( orig.toCharArray().length == 0 ) {

                        System.out.println( anag );
                        return;


                }

                char[] origChars = orig.toCharArray();
                char[] anagChars = anag.toCharArray();

                for( int i=0; i < origChars.length; i++ ) {

                        anagChars[index] = origChars[i];
                        index++;
                        String strStillLeft = orig.substring(0, i) + orig.substring(i+1, orig.length());
                        findAnag( strStillLeft, new String(anagChars), index);
                        index--;

                }




        }











}

https://github.com/asharif/StringAnag

String Combination Algorithm

Just needed to brush up some old but important algorithms so I thought I'd post them here and on github for my own future reference and anyone elses. This example I did in Java:

class Combo {



        public static void main ( String args[] ) {

                char[] combos = new char[args[0].length()];
                findCombos ( args[0], new String(combos), 0 );

        }



        private static void findCombos( String str, String combos, int index ) {

                char[] comboChars = combos.toCharArray();


                if ( index == str.length()) {

                        System.out.println( new String(comboChars) );

                        return;

                }

                for ( int i = 0; i < str.length(); i++ ) {
                        char[] strChars = str.toCharArray();
                        comboChars[index] = strChars[i];
                        index++;
                        findCombos(str, new String(comboChars), index);
                        index--;

                }




        }


}

Here it is on github: https://github.com/asharif/StringCombo

Monday, July 16, 2012

packit4me.com (2D/3D Bin Packing)

(UPDATE 06/04/2013) This project is now available @ http://www.packit4me.com/
3D/2D bin packing. Given multiple bins and items to be packed into the bins, the algorithm attempts to find a good solutions of how to pack. I got so many requests for this that I made it into a real life project. Check out the site. It has comprehensive documentation on how to call the API with all the popular languages (Java, php, C#, ruby, python). I hope it works for you! If not just email me or make some posts here. I'll try to setup some ticket system soon to take requests.