Monday, July 27, 2009

Puzzle - $1 vanished ?

Three men go to a cheap motel, and the desk clerk charges them a sum of $30.00 for the night. The three of them split the cost ten dollars each. Later the manager comes over and tells the desk clerk that he overcharged the men, since the actual cost should have been $25.00. The manager gives the bellboy $5.00 and tells him to give it to the men. The bellboy, however, decides to cheat the men and pockets $2.00, giving each of the men only one dollar.
Now each man has paid $9.00 to stay for the night, and 3 x $9.00 = $27.00. The bellboy has pocketed $2.00. But $27.00 + $2.00 = $29.00. Where is the missing $1.00? WTF?


Solution:
Total Customer effectively paid =27
Total amnt the hotel got = 25
The difference is in bellboy's pocket = 27-25 =2

Puzzle -Daughter's Age Problem

A census taker came to a house where a man lived with
three daughters. "What are your daughters' ages?" he asked.
The man replied, "The product of their ages is 72, and the
sum of their ages is my house number."
"But that's not enough information," the census taker insisted.
"All right," answered the farmer, "the oldest loves chocolate.

What are the daughters' ages?

Solution :

We can look at all the possibilities this way - the oldest may be the same age as the middle, and the middle may be the same age as the youngest:
OLD MID YOUNG SUM
72 1 1 74
36 2 1 39
24 3 1 28
18 4 1 23
18 2 2 22
12 3 2 17
12 6 1 19
9 8 1 18
9 4 2 15
8 3 3 14
6 6 2 14
6 4 3 13
These are all the possibilities, since if we give the oldest a lower age, there is no way the product can equal 72.We know their ages add up to the farmer's house number. The sum column gives all possibilities for the house number. When the farmer gave the census person the information about the product of their ages being 72 and the sum of their ages being his house number the census person said this was not enough information. So there must have been at least two different possibilities that were still viable options for the census taker to choose from. This means the house number must have had two sums equal it. So the daughters are:
8 3 3 14
6 6 2 14
since they both add up to 14, the only number that appeared twice in the sum of the ages. The fact that the farmer had an oldest daughter says that the daughters must in fact be ages 8, 3, and 3.

Test For Palindrome - Java

package palindrome;
import java.util.*;

public class TestPalindrome
{
public TestPalindrome()
{
}

public static boolean isPalindrome(String word) {
int left = 0; // index of leftmost unchecked char
int right = word.length() -1; // index of the rightmost

while (left < right) { // continue until they reach center
if (word.charAt(left) != word.charAt(right)) {
return false; // if chars are different, finished
}
left++; // move left index toward the center
right--; // move right index toward the center
}
return true; // if finished, all chars were same
}

public static void main(String[] args)
{
TestPalindrome testPalindrome = new TestPalindrome();
String s = "Was it a rat I saw?" ;
s = s.replaceAll("[\\s\\p{Punct}]", "").toLowerCase();
boolean flag = isPalindrome(s);
System.out.println(s + " PALINDROME ? " + flag);

}

}

Wednesday, July 1, 2009

Extract Jar from Blob Column through Java code

I used this code to extract jar file which was stored in BLOB column of the database.
/**
*
* This code extracts jar file from cz_archives table from the BLOB column.
*
*/

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class ExtractBlob
{
public ExtractBlob()
{
}
final static int bBufLen = 4 * 8192;
static String query;
static String outFile;
static Connection conn;

public static void main(String[] args) throws FileNotFoundException, IOException, SQLException
{
String baseFolder = "C:/CZArchives";
String folderName = "";
String fileName = "";

String query = "SELECT 'TEST-Archive' FOLDER_NAME, "
+ "CZArch.ARCHIVE_URL DOCNAME,CZArch.ARCHIVE_BLOB "
+ "FROM CZ_ARCHIVES CZArch, CZ_RP_ENTRIES CZRp "
+ "WHERE CZRp.OBJECT_ID = CZArch.ARCHIVE_ID "
+ "AND CZArch.deleted_flag = '0' "
+ " AND CZRp.OBJECT_TYPE = 'ARC' "
+ "and CZArch.name = 'nameofyourjarfile' ";
//replace nameofyourjarfile with your jar file name

conn = getConnection();
conn.setAutoCommit(false);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
Blob blob = null;
while (rs.next())
{
folderName = rs.getString(1);
fileName = rs.getString(2);
// String created = rs.getString(4);
// String updated = rs.getString(5);
blob = rs.getBlob(3);
System.out.println("Got " + folderName);
System.out.println("Got " + fileName);
// System.out.println("Created " + created);
// System.out.println("Updated " + updated);

outFile = baseFolder + "/" + folderName;

outFile = baseFolder;
long wrote = 0;
File file = new File(outFile);
file.mkdir();
outFile = outFile + "/" + fileName;

OutputStream fwriter = new FileOutputStream(outFile);
wrote = readFromBlob(blob, fwriter);
fwriter.close();
System.out.println("Wrote " + wrote + " bytes to file " + outFile);
}
rs.close();
stmt.close();
conn.close();
}

public static Connection getConnection()
{
Connection con = null;

String dsn = "::";
String uname = "apps";
String passwd = "pwd";
try
{
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@" + dsn;
Class.forName(driver);
con = DriverManager.getConnection(url, uname, passwd);
}
catch (Exception ee)
{
ee.printStackTrace();
}

return con;
}

public static long readFromBlob(Blob blob, OutputStream out) throws SQLException, IOException
{
InputStream in = blob.getBinaryStream();


int length = -1;
long read = 0;
byte[] buf = new byte[bBufLen];
while ((length = in.read(buf)) != -1)
{
out.write(buf, 0, length);
read += length;
}
in.close();
return read;
}

}