Showing posts with label Puzzle. Show all posts
Showing posts with label Puzzle. Show all posts

Sunday, December 13, 2009

Java Puzzler - on Code-o-matic

I don't have a puzzle of my own today - I've covered up most things I know & I really don't want to end up repeating myself! So for now I am going to point you to a puzzle I had come across some time ago - http://code-o-matic.blogspot.com/2009/02/crazy-java-puzzler.html

I really don't know the answer to this puzzle - but then again I've never been good with Generics or hardcore Java - you folks might know an answer. Give it a shot - and do let us know too if you got an answer!

Sunday, November 8, 2009

Puzzle 58 – Simple Upgrade.

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

Here is the puzzle straight and simple. A piece of code was written which had a read() method with the signature below. Thing change and instead of using integers – it was now required to use Floats instead of Integers.

To cut a long story short what is the minimum change considering additions/deletions (each char added or deleted count as 1) to get the code below to compile. (I think it can be done in less than 10 characters)

package com.twister;

import java.util.ArrayList;
import java.util.List;

public class Gener {
public void read(List<Integer> x){}

public static void main(String[] args) {
new Gener().read(new ArrayList<Float>());
}
}


Got an answer? Leave one here

Sunday, November 1, 2009

Puzzle 57 – Hello World - Again

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

It's been two weeks since I wrote a puzzle out here – so I end up typing the simplest program I could think of in Java.

class X{
public static void main(String[] a){
System.out.print(
"Hello World");
}
}

That's the smallest program that I could in java that prints hello world (72 characters excluding all the white spaces). Hope you folks noticed the clever use of variable names and print instead of println.

Well here is the really simple challenge. Write some code that does exactly what the above code does – just use less number of characters. Remember the code has got to compile cleanly and run cleanly and produce the same output as the snippet above. Easy!

Looking for more Java Puzzles? Check out these sites.

Got an answer? Leave one here.

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

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

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.

Sunday, September 20, 2009

Puzzle 53 – Statically Speaking.

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

This puzzle is interesting because it's based on a real life incident that happened with a colleague of mine. To give you a gist of what happened, here is the code (well this isn't the real code – just the important part)

package com.twister;
class MyData{
String author
= com.twister.Constants.AUTHOR;
//lot of other stuff comes here…

public static void main(String[] args) {
MyData mydata
= new MyData();
System.out.println(mydata.author);
}
}

package com.twister;
class Constants{
public static final String AUTHOR = "SAM"; //Me of course

}

Here is what happened. This was some code that I had written – see my name in the Constants file. Well my colleague picked up my code and just modified the String Author to his, recompiled the Constants file and went on to show a demo to the manager. Ops something went wrong here, which got him into a lot of trouble. Can you figure out what went wrong? (Assume he codes using Notepad). What are the possible solutions to this problem?

Got an answer? Do leave it here.

Sunday, September 13, 2009

Puzzle 52 – "Satyameva Jayate" : Truth shall always prevail.

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

What is the minimum change (additions only – no deleting or commenting out code) that you need to make to the code so that the program prints true?

This should be easy – but it does highlight another peculiarity to look out for.

package com.twister; public class MyTruth { public static void main(String[] args) { boolean b = Boolean.getBoolean("false"); System.out.println(b); //This should print true } }

Got an answer? Do leave it
here.

Sunday, September 6, 2009

Puzzle 51 – This and That!

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

Improving upon last week’s code, I came up with this:

package com.twister; public class Area { //should initialize to 0 - formula mentioned for documentation int area = length*width; //Instance variables get initialized to 0 int length; int width; public static void main(String[] args) { Area a = new Area(); //Do whatever needs to be done in main } }


Now while this is much better than the previous code (no hard coded values, self documenting), I seem to be having a problem with the code. As you might have already guessed this code does not compile!!!

Your challenge is pretty simple – get this code to compile by adding minimum number of characters to this code. Also a few additional conditions that you need to meet are -

a. You may only add code to get this to work – no deletes, no moving about the code, no commenting out any code! Additions only!

b. Obviously as before, declaring length and width as static would do the trick – but then that is so obvious that it can’t be the right solution, can it?
(So I am looking for something that solves the problem in less than 12 characters).

Got an answer? Do leave it here.

Sunday, August 30, 2009

Puzzle 50 – Before & After

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

Do you remember the first time you wrote some java code and were stuck on a really silly thing for hours? Well this puzzle is right up that alley,

package com.twister; public class Area { int length = 10; int area = length*breath; int breath = 20; public static void main(String[] args) { Area a = new Area(); System.out.println(a.area); } }

Apart for being a really silly piece of code with all values ‘hard coded’ – as you might have already guessed this code fails to compile.
The puzzle - what is the minimum code addition (no deletes, no moving the code – plain addition only) that is needed to make this code compile and run to give the expected output – 200?

Got an answer? Do leave it here.

Sunday, August 23, 2009

Puzzle 49 – Stop me if you can ...

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

After a long week here is the next puzzle,

package com.twister;
public class StopTheLoop {
static StopTheLoop looper;

public static void main(String[] args) {
looper
= new StopTheLoop();
looper
= null;
do{
System.out.println(
"Infinite Loop");;
}
while(looper==null);
}
}

This program keeps printing the string "Infinite Loop" an infinite number of times. Without making any changes to the main method (no additions, deletions, modifications, redefining main) - convert this program so that the loop runs a finite number of times.
Note : the loop must run at least one time.

Got an answer? Do leave it here.

Sunday, August 16, 2009

Puzzle 48 - Fruity Tale

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

I have been keeping pretty busy these days with lots of parallel stuff going on. Unfortunately this means that I will have to reduce some of the stuff I do and twister is one of them. Of course I still going to keep posting puzzles but at a reduced frequency - of one puzzle a week at least for some time!

On the other hand, this should give me some time to get Quiz4J up in shape which would also explore features of Java in a fun filled manner - keep watching this space for more information on that.

Coming back to Today's question, it is not really a puzzle and depending on your comfort level with Java this might just be easy. It does bring out an interesting feature though so give it a shot ...

<modifier> Fruits{
apples(
"red"), oranges("orange"), grapes("green");
String color
= null;
Fruits(String color){
this.color = color;
}
}

What does this code do? Can you complete this code and explain what it does or give an example of how it can be used?

Got a solution? Do leave it here.

Wednesday, August 12, 2009

Puzzle 47 - IsEqual Unequal?

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

It's been a long break since we had a puzzle here, so without much ado here is the next one.

package com.twister;
public class Mystery {

public static boolean isEqual(<DataType1> f1, <DataType2>f2){
return f1.equals(f2);
}

public static void main(String[] args) {
<DataType3> f1= <value1>
<DataType4> f2 = <value2>
System.out.println(
"f1 is equals to f2 : "+ (f1==f2)); //prints true
System.out.println("f1 is equals to f2 : "+ isEqual(f1,f2)); //prints false
}
}


Everything that you need to solve this is present in the code snippet above. All you need to do is replace everything in brackets namely - DataType1 - DataType4 & Value-1 - Value-2 with appropriate constructs, so that the prints happen as per the comments. No other changes to be done to this code!

You might want to watch out for hints and more restrictions @ Twitter.

Got a solution. Do leave it here.

Treasure Hunt Results to be updated next week.

Wednesday, July 29, 2009

Puzzle 46 - Just a Sort.

Language – Java Type – Concept Last date 2-Aug-2009 9:00 p.m. IST Points 5

Its been a long time since we had a programming question out here. So here is one where you get to punch in some real code.

Print the maximum number of a list of ten inputted numbers.

Easy? Well here are some clauses that should make this harder.
1. No classes may be used except java.lang.Object. So no collection frameworks, none of the inbuilt stuff to sort the numbers :).
2. No arrays may be used in the entire code.
3. Should compile and run with Java 1.4

Here is what the pseudo code for the program should look like;

int num1 = [scan the number at runntime]
.
.
.
int num9 = [scan the number at runntime]
int num10 = [scan the number at runntime]

printMax(num1,num2,num3,....num10)

If you find yourself more than 20 to 25 lines of code ... think again!!

Got a solution. Do leave it here.

Sunday, July 26, 2009

Puzzle 45 – Equally Unequal - II

Language – Java | Type – Concept | Last date 29-Jul-2009 9:00 p.m. IST | Points 2 + Bonus

The last puzzle seems to have got a lot of flake for being simple – It also got the maximum participation – so it looks like you folks like simplicity but don’t like simplicity :)

So I decided to try something different this week! There are lots of new folks who have joined in and I would like, you to climb the point ladder quickly – here is the perfect opportunity!

package com.twisters;
public class Equal_Unequal {
public static void main(java.lang.String[] args) {
<someType1> x = <someValue1>;
<someType2> y = <someValue2>;
System.out.println(
"Values are equal : "+(x==y));
}
}

Steps that you need to follow:

1. Fill in a value for both the someType and someValue. They could be same or different.
2. You may not add any other code or delete any of the existing code.
3. Run the program. It should print Values are equal : true
4. Run the program. It should print Values are equal : false

P.S – As you can see you cannot compile again between the two runs – in case it wasn’t obvious enough.

Pretty simple up too now so here is the catch – For every different logical solution that you offer you get two bonus points. Now for a solution to be logically different it must satisfy two criteria.
a. sometype must be different from the other solutions.
b. It must exhibit a different Java principle (or different logical reason) from the other solutions that you have posted.
In case you are not sure if its logically different I would suggest to post the solution!

While it is not necessary to explain what you consider your solution as the guiding principle of your solution - it would be great if you do give a short explanation.

I can think of three – probably four different logical solutions to this puzzle (& frankly more than 20 if you remove the logical clause).

Looking for hints and more bonuses? You might want to watch out for an additional clause on twitter.

As usual looking forward to your solutions!!

Wednesday, July 22, 2009

Puzzle 44 – The One!

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

From where I come from everything is driven by statistic. Its no wonder that I am so much attached to number & tracking. I track the number of visitors on the blog - Google Analytics, Feed Subscribers, Twitter followers even my donate widget is one which shows how much progress I have made - at 0 right now :(!!
I sure would love any help to improve on any of these numbers ;)

The number that's pretty interesting though is the question rating. Its a pointer to the type of questions that you folks find interesting. I try to align questions to what you folks are looking for - in terms of type and difficulty though not always immediately.

Breaking out from my rant and getting to the puzzle at hand -

Have you ever had to code to silly standards. Well here is a typical construct!

package com.twisters;
public class ExtraLoad{
public static void main(String[] args) {
ExtraLoad ex
= new ExtraLoad();
ExtraLoad ex1
= new ExtraLoad("Hello");
ExtraLoad ex2
= new ExtraLoad("Hello", "World");
ExtraLoad ex3
= new ExtraLoad("Hello", "World", "Wide Web");
}
}


Get this code to compile – don't comment or delete any code.

Standard says -
You may not have more than one constructor in the code - Silly isn't it!

Sure you could rename the method to Extraload1(String), ExtraLoad2(String,String) but that would not be an interesting solution, would it? So here another clause - No changes to any part of main() method.

As usual looking forward to your solutions!!

Sunday, July 19, 2009

Puzzle 43 – Run a Thread.

Language – Java | Type – Concept | Last date 22-Jul-2009 9:00 p.m. IST | Points 3

Contrary to what the title says – this puzzle is about stopping a rouge thread form running.
The challenge is pretty simple – all you need to do is stop the code from printing “STOP ME IF YOU CAN”

package com.twisters;
class MultiThreader implements Runnable{
public void run(){
System.out.println(
"STOP ME IF YOU CAN");
}

public static void main(String[] args) {
MultiThreader m
= new MultiThreader();
Thread t
= new Thread(m);
t.start();
}
}

Rules:
No code may be deleted or commented – but you folks already know this!
No changes to or in the main method at all.
No code may be added to the body of the run() method!

One way to solve this is to rename the run() method to _run() and write an empty run() method. This needs a minimum 20 characters including the necessary white space. You may use a maximum of 18 characters including any white spaces. (Growing pretty obsessive with this 18 character limit – ain’t I)

Pretty simple puzzle to be honest!!

Got an answer? Leave your answer here.


Wednesday, July 15, 2009

Puzzle 42 - Equally Unequal.

Language - Java | Type - Concept | Last date 19-Jul-2009 12:00 p.m. IST | Points 3

This might be old puzzle, someone asked this question on Java Ranch. I think it's a pretty neat & tough puzzle why don't you folks give it a try!

class Equals {

public static void main(String args[]){

<sometype> x = <somevalue>;

System.out.println(x
== x) //This should print false

}

}


Replace the placeholders <sometype> and <somevalue> with any construct or value so that the System.out.println() prints out a false. No other changes/additions/deletions are permitted.

Looking for a hint? I think one might just show up on twitter soon.

Got an answer? Leave your answer here.

Sunday, July 12, 2009

Puzzle 41 – Hello World

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

Lately I have been reading up on groovy and the hello world program in groovy is just so groovy!

println ‘Hello World’

This brings me to the next questions!

Write a program that prints Hello World in Java. No Semicolons anywhere in the code. No, not even one!! Not even using a alternate notation like unicode!!

P.S – I know Groovy can be run as a Java program – but here I mean Java code, that compiles using the javac compiler!!

Got an answer? Do leave it here.

Puzzle 40 – Solution.

I like these optimization puzzles since it brings a variety of solutions and just shows how small changes to the algorithm could make a huge impact.

Here is the optimization that I had in mind. It makes the code run in less than 5 seconds on my machine. Throw in a couple of more optimizations like incrementing by 20 instead of 1 (see comments here) brings down the execution time to less than half a second!!

package com.sam.twisters.euler;
/* Problem 5 : Euler
* 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
* What is the smallest number that is evenly divisible (divisible without reminder) by all of the numbers from 1 to 20?
*/

public class prog5 {
/*Simple function to check if the divisor completely divides the divident*/
public static boolean isDivisble(int divident, int divisor) {
if(divident%divisor == 0){
return true;
}
else{
return false;
}
}

public static void main(String[] args)
{
long startTime = System.nanoTime();
boolean va = true;

/* Start looping though all the numbers and see if we can find one divisible by all numbers
* from 1 to 20.
*/
int i = 1;
do{
va
= true; //Assume that i is a valid answer to the puzzle
//Optimization -->for(int d=1;d<=20;d++){
/*
(1 char, start from 11 and not 1).
This works since atleast one of the numbers from 11 to 20 has each of the numbers 1-10 as a factor,
so for example when we test if a number is divisble by 12 - we also indirectly test that the
number is divisible by 2,3,4 and 6
*/
for(int d=11;d<=20;d++){
if (!isDivisble(i, d)){ //Yikes my assumption was wrong!
va = false;
//Optimization -->break; (6 char)
break;
}
}
i
++;
}
while(!va);
System.out.println(i);
long estimatedTime = System.nanoTime() - startTime;
System.out.println((
float)estimatedTime/1000000000);
}
}

http://www.blogtrog.com/code.aspx?id=7617cc6e-c724-4122-b5c5-bf6eb703a700

Best solutions for the week?

@TheMalkolm - I'll pick TheMalkolm solution - its a pretty good optimization to the existing code.

@Sebastian - Yep your second solution is pretty neat (took some time figuring out how it works!!) though as you pointed out it uses a completely different algorithm. Neat nonetheless!!