Sunday, October 25, 2009

Puzzles, Puzzles and more Puzzles.

The past couple of months I have been pretty much fascinated with programming puzzles. I have been on a lookout for really good puzzle sites - and participating in competitions whenever I get the chance.

It all started a couple of months back when I first heard of Google Code Jam. The idea really fascinated me and since then I been solving lots of puzzles around the net (with some attempts to write my own). Currently you find me on Al Zimmermann's Programming Contests playing around with the darts.

I have also been busy compiling of all puzzle/programming sites on the web - you can have a look at it here - http://www.quiz4j.com/contest.do. I am still working on the page and it might be a couple of weeks before my list is finally complete (if ever) - but it’s pretty huge already. Do have a look - I'm sure you will find something you like. If like what you see please share the link around!!!

There are no puzzles this week on twister - I'll be back with the regular java questions (and maybe a couple of tricky ones!!!) from next week.

Sunday, October 18, 2009

Puzzle 56 – Cube Traversal

Language – Java | Type – Concept | Last date 25-Oct-2009 12:00 p.m. IST | Points 5

We been dealing with Java concepts lately and its time to give concepts a break and get to some real world puzzles. Here is a small programming puzzle to get things started. Do let me know what you think of this question so I could think about adding more questions like these.





Submitting java code on scarky has few rules that need to be followed.
1. You can just have one class and it must be named Main. It should not be inside any package.
2. You need to read all input before you can start writing any output. (Use System.out.print() & System.out.println() for output)
3. For the exact format of input and output refer to the example test case.

You can find a standard template to read the input data here.

Your solution would be evaluated automatically by scarky.
In case of any issues you could reach me at admin@quiz4j.com

Puzzle 55 – Solution

The solution to last time puzzle was to use an instance initializer. An instance initalizer is a piece of code similar to a static block that gets called whenever an instance is created (the compiler inlines this code at the beginning of each constructor).

package com.twisters;
public class NoConstructor {

boolean isConstructor = false; //No changes permitted to this line
{isConstructor = true;}

/* No Code may be added or changed in main*/
public static void main(String[] args) {
NoConstructor noConstructor
= new NoConstructor();
System.out.println(noConstructor.isConstructor);
//Prints true
}
}

Monday, October 12, 2009

Puzzle 55 – The No constructor dilemma.

Language – Java | Type – Concept | Last date 18-Oct-2009 12:00 p.m. IST | Points 3

This week’s puzzle is pretty simple and the question self explanatory (I hope).

package com.twisters;
public class NoConstructor {

boolean isConstructor = false; //No changes permitted to this line

/* No Code may be added or changed in main*/
public static void main(String[] args) {
NoConstructor noConstructor
= new NoConstructor();
System.out.println(noConstructor.isConstructor);
//Prints true
}
}

The NoConstructor class must not have any explicit constructor (that is - don’t use the word NoConstructor any more times in your solution). That's all - everything else about the puzzle should be self explanatory from the comments!

Got an answer? Leave one here

Puzzle 54 – Solution

The principle that I wanted to bring out in this puzzle was that we can break out of any labeled block by using a break statement.

package com.twister;
public class NoIf {

public static void main(String[] args) {
if(true){}
System.out.println(
"Print This");
noPrint:{
if(true){break noPrint;}
System.out.println(
"Print This - Not!");
}
if(true){}
System.out.println(
"Print This");
}
}

The other popular solution was to wrap the code in a try-catch-finally block. As the catch block will not execute without any exception – the second print statement is skipped.

My favorite solution for this puzzle is the one by vector9x,
System.out.println("Print This - Not!".substring(0,10));

Scores to be updated next week!!

Sunday, October 4, 2009

Puzzle 54 – No More If’s

Language – Java | Type – Concept | Last date 11-Oct-2009 12:00 p.m. IST | Points 3

Today’s unearthly puzzle finds its roots in code written by a Jalvanian master. The question in today’s puzzle should be self explanatory.

package com.twister;
public class NoIf {

public static void main(String[] args) {
if(true){}
System.out.println(
"Print This");
if(true){}
System.out.println(
"Print This - Not!");
if(true){}
System.out.println(
"Print This");
}
}


It’s kind of pretty obvious what needs to be done – the code says it all.

You must of course follow the Three Jalvanian Laws.

1. You may only add but not delete nor comment out any of the code.

2. A real Jalvanian would never use an else so neither must you.

3. You may not use any more boolean variables (there are three in the code – which must remain unaltered) or conditions that evaluate to a boolean value. (So no true==false – since false is a boolean value etc).

That’s all folks!!

Got an answer? Do leave it here.