Saturday, November 7, 2009

The 12 balls puzzle

A solution to solve the 12 ball puzzle.
Fact: you know that 11 balls are equally weighted and 1 of them is not
Constraint: you can only use the weighing machine at most 3 times to determine the heaviest ball

import java.util.Random;

public class TheHeaviestBall {

private int count = 0;
private int indexHeaviestBall = 0;
private Random myRand = new Random();
private int[] balls = {1,1,1,1,1,1,1,1,1,1,1,1};
private int[] arrangeBalls(){
balls[myRand.nextInt(12)] = 2;
for (int aBall : balls) {
System.out.print(aBall + " ");
}
return balls;
}

public void weighingCount(){
count++;
if (count > 3) {
System.out.println("Your algorithm fails");
}
}

public int theBALL(){
int myBalls[] = arrangeBalls();
weighingCount();
if ((myBalls[0]+myBalls[1]+myBalls[2]+myBalls[3]) > (myBalls[4]+myBalls[5]+myBalls[6]+myBalls[7])) {
weighingCount();
if ((myBalls[0]+myBalls[1]) > (myBalls[2]+myBalls[3])) {
weighingCount();
if (myBalls[0] > myBalls[1]) {
indexHeaviestBall = 0;
return myBalls[0];
} else {
indexHeaviestBall = 1;
return myBalls[1];
}
} else {
weighingCount();
if (myBalls[2] > myBalls[3]) {
indexHeaviestBall = 2;
return myBalls[2];
} else {
indexHeaviestBall = 3;
return myBalls[3];
}
}
}
else if ((myBalls[4]+myBalls[5]+myBalls[6]+myBalls[7]) > (myBalls[0]+myBalls[1]+myBalls[2]+myBalls[3])) {
weighingCount();
if ((myBalls[4]+myBalls[5]) > (myBalls[6]+myBalls[7])) {
weighingCount();
if (myBalls[4] > myBalls[5]) {
indexHeaviestBall = 4;
return myBalls[4];
} else {
indexHeaviestBall = 5;
return myBalls[5];
}
} else {
weighingCount();
if (myBalls[6] > myBalls[7]) {
indexHeaviestBall = 6;
return myBalls[6];
} else {
indexHeaviestBall = 7;
return myBalls[7];
}
}
}
else {
weighingCount();
if ((myBalls[8]+myBalls[9]) > (myBalls[10]+myBalls[11])) {
weighingCount();
if (myBalls[8] > myBalls[9]) {
indexHeaviestBall = 8;
return myBalls[8];
} else {
indexHeaviestBall = 9;
return myBalls[9];
}
} else {
weighingCount();
if (myBalls[10] > myBalls[11]) {
indexHeaviestBall = 10;
return myBalls[10];
} else {
indexHeaviestBall = 11;
return myBalls[11];
}
}
}
}

public static void getMyHeaviestBall(){
TheHeaviestBall hb = new TheHeaviestBall();
//Algorithm validation
if (hb.count > 3) {
System.out.println("You can do better");
} else {
System.out.println("\nThe heaviest ball [ " + hb.theBALL() + " ] is the " + (hb.indexHeaviestBall + 1) + "th ball from the left");
}
}

public static void main(String[] args) {
getMyHeaviestBall();
}
}
Update: Click here to view the solution to solve the 8 balls puzzle

Friday, November 6, 2009

The 8 balls puzzle

Here is an interesting puzzle about selecting the heaviest ball among 7 other equally weighted balls. For example a collection of the 8 balls would look like this [2,1,1,1,1,1,1,1].

The goal is to choose the heaviest ball by performing only two weighings.
Fact: You know that all 7 other balls are equally weighted but you could not figure out the heaviest ball.
Solution: In order to do so, you are using the weighing machine but as a challenge you can only use the weighing machine at most twice

import java.util.Random;

public class TheHeaviestBall {

private int count = 0;
private int indexHeaviestBall = 0;
private Random myRand = new Random();
private int[] balls = {1,1,1,1,1,1,1,1};
private int[] arrangeBalls(){
balls[myRand.nextInt(8)] = 2;
for (int aBall : balls) {
System.out.print(aBall + " ");
}
return balls;
}

public void weighingCount(){
count++;
if (count > 2) {
System.out.println("Your algorithm fails");
}
}

public int theBALL(){
int myBalls[] = arrangeBalls();
weighingCount();
if ((myBalls[0]+myBalls[1]+myBalls[2]) > (myBalls[3]+myBalls[4]+myBalls[5])) {
weighingCount();
if (myBalls[1] == myBalls[2]) {
indexHeaviestBall = 0;
return myBalls[0];
} else {
if (myBalls[1] >= myBalls[2]) {
indexHeaviestBall = 1;
return myBalls[1];
} else {
indexHeaviestBall = 2;
return myBalls[2];
}
}
}
else if ((myBalls[3]+myBalls[4]+myBalls[5]) > (myBalls[0]+myBalls[1]+myBalls[2])) {
weighingCount();
if (myBalls[4] == myBalls[5]) {
return myBalls[3];
} else {
if (myBalls[4] >= myBalls[5]) {
indexHeaviestBall = 4;
return myBalls[4];
}
else {
indexHeaviestBall = 5;
return myBalls[5];
}
}
}
else {
weighingCount();
if (myBalls[6] >= myBalls[7]) {
indexHeaviestBall = 6;
return myBalls[6];
}
else {
indexHeaviestBall = 7;
return myBalls[7];
}
}
}

public static void getMyHeaviestBall(){
TheHeaviestBall hb = new TheHeaviestBall();
//Algorithm validation
if (hb.count > 2) {
System.out.println("You can do better");
} else {
System.out.println("\nThe heaviest ball [ " + hb.theBALL() + " ] is the " + (hb.indexHeaviestBall + 1) + "th ball from the left");
}
}

public static void main(String[] args) {
getMyHeaviestBall();
}
}
Update: Click here to view the solution to solve the 12 balls puzzle

Quizzie - my first Android application

Behold ... my very first fully functional Android application. The development process is very short. I always have this philosophy that if you know what you want to do and achieve, you will know what to do and how to achieve it. Therefore the design process is shortened.

This game is all about Mathematics. Seemingly simple mathematical expressions but good enough to induce carelessness to choose the wrong answer.

Wednesday, November 4, 2009

Splunk in Movie Characters Timeline

These are the graphs that could be possibly (fictitiously) generated using Splunk to search for the characters in movies. Take a look at this:Click here to view the original source of the picture from xkcd.

Saying that something is wrong to someone

This is a hilarious way to express to someone if something is wrong. By the way if Cartman thinks that it is wrong, it must be really really wrong. :))

Monday, November 2, 2009

Sound Management in Android Application

This is a suggestion from myself about adjusting the volume of the sound effects or background music in your Android Activity(ies). The sample code is as below:onKeyDown method is called when a button is pressed and onKeyUp is called when the button is released. In this example specifically, we are raising and lowering the volumes when the volume keys are pressed and retain the values when the buttons are released.

Adding Vibration to your Android Application

Below are the simple steps to enable the vibration feature for your Android application. There are two things you will need to take care of - the AndroidManifest.xml file and the Vibrate class.

[1] Add this line in manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
. . .
. . .
. . .
<uses-permission android:name="android.permission.VIBRATE">permission>
</manifest>

[2] Add these two lines in your class definition.