Wednesday, July 30, 2008

Tiff to PDF conversion Using Java

Recently I had converted tiff to pdf using java and IText API.Both the jar as well
as source code can be found in the following urls:
http://www.lowagie.com/iText/download.html
http://itextdocs.lowagie.com/tutorial/objects/images/tiff/index.php

Thursday, July 10, 2008

Solution to a coding puzzle .

Recently I came across a coding puzzle posted in Cedric Beust's weblog(http://beust.com/weblog/).The puzzle is:

Write a counter function that counts from 1 to max but only returns numbers whose digits don't repeat.

For example, part of the output would be:

8, 9, 10, 12 (11 is not valid)
98, 102, 103 (99, 100 and 101 are not valid)
5432, 5436, 5437 (5433, 5434 and 5435 are not valid)
Also:
Display the biggest jump (in the sequences above, it's 4: 98 -> 102)
Display the total count of numbers
Give these two values for max=10000

Though I know that my solution is not that good because I am converting the numbers to string for solving the problem, but still its working....Please post a better approach if you have any......


public class Puzzle
{
//Method for comparing digits of a number with each other
private boolean compareDigits(int number)
{
String digitString=null; //holds string representation of numbers.
digitString=new Integer(number).toString();
boolean flag=true;
for(int strcount=0;strcount<digitString.length();strcount++)
{
for(int temp=strcount+1;temp<digitString.length();temp++)
{
if(digitString.substring(strcount,strcount+1).equalsIgnoreCase(digitString.substring(temp,temp+1)))
{
flag=false;
}
}
}
return flag;
}


public static void main(String []args)
{
int max=new Integer(args[0]).intValue();//holds the maximum number
int counter=0; //holds the count of numbers printed
int biggestJump=0;
int prevInteger=0;
Puzzle puzzleObj=new Puzzle();
for(int num=1;num<=max;num++)
{
boolean isPrintable=puzzleObj.compareDigits(num); //isPrintable is true if the number doesn't have any repeating digit
if(isPrintable)
{
if(biggestJump<(num-prevInteger))
{
biggestJump=num-prevInteger;
}
prevInteger=num;
System.out.println(num);
counter++;
}


}
System.out.println("Total count of numbers is:"+counter);
System.out.println("Biggest Jump="+biggestJump);

}

}