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.
Wednesday, July 29, 2009
Puzzle 45 - Solution
There are many solutions to the puzzle and I've have tried to broadly classify the solution into variuos groups.
These don't necessary cover all the solution...
1. Run the program by passing two arguments to it.
First time both the arguments are same, the next time both the arguments are different
someType1 & someType2 = int
someValue1 = Integer.parseInt(args[0]);
someValue2 = Integer.parseInt(args[1]);
First Run: java com.twisters Equal_Unequal 1 1
Second Run: java com.twisters Equal_Unequal 1 2
2. Run the program by passing just one parameter.
Depending on a java principal get the program to print true and false.
a. Use the caching property of the wrapper classes for values below 127.
someType1 & someType2 = Float
someValue1 = Float.parseFloat(args[0]);
someValue2 = Float.parseFloat(args[0]);
First Run: java com.twisters Equal_Unequal 1
Second Run: java com.twisters Equal_Unequal 1024
b. Use the NAN property of floats
someType1 & someType2 = float
someValue1 = Float.parseFloat(args[0])/Float.parseFloat(args[0]);
someValue2 = someValue1;
First Run: java com.twisters Equal_Unequal 1.0
Second Run: java com.twisters Equal_Unequal 0.0
3. Change an External condition.
a. Create or delete a file on the file system
someType1 & someType2 = boolean
someValue1 = isFileExists(c:\test1.txt)
someValue2 = isFileExists(c:\test2.txt)
First Run: java com.twisters Equal_Unequal /*Create both the files so someValue1 & someValue2 are both true*/
Second Run: java com.twisters Equal_Unequal /*Create just one files so only one of someValue1 or someValue2 is true*/
b. Change an environment variable
c. change the time of the machine.
4. Use a Random generator in the code.
There are many ways to generate random results. Though technically not absolutely accurate - one of the runs in bound to generate a true first and a false the second time.
5. Byte code manipulation.
You can't recompile the code but you can always go ahead and change the byte code ;)
This should cover up most of the solutions - though I would suggest having a look at the comments for a real wide varity of solutions.
I would post the scores for this question as a separate post this weekend - watch out for it!!
These don't necessary cover all the solution...
1. Run the program by passing two arguments to it.
First time both the arguments are same, the next time both the arguments are different
someType1 & someType2 = int
someValue1 = Integer.parseInt(args[0]);
someValue2 = Integer.parseInt(args[1]);
First Run: java com.twisters Equal_Unequal 1 1
Second Run: java com.twisters Equal_Unequal 1 2
2. Run the program by passing just one parameter.
Depending on a java principal get the program to print true and false.
a. Use the caching property of the wrapper classes for values below 127.
someType1 & someType2 = Float
someValue1 = Float.parseFloat(args[0]);
someValue2 = Float.parseFloat(args[0]);
First Run: java com.twisters Equal_Unequal 1
Second Run: java com.twisters Equal_Unequal 1024
b. Use the NAN property of floats
someType1 & someType2 = float
someValue1 = Float.parseFloat(args[0])/Float.parseFloat(args[0]);
someValue2 = someValue1;
First Run: java com.twisters Equal_Unequal 1.0
Second Run: java com.twisters Equal_Unequal 0.0
3. Change an External condition.
a. Create or delete a file on the file system
someType1 & someType2 = boolean
someValue1 = isFileExists(c:\test1.txt)
someValue2 = isFileExists(c:\test2.txt)
First Run: java com.twisters Equal_Unequal /*Create both the files so someValue1 & someValue2 are both true*/
Second Run: java com.twisters Equal_Unequal /*Create just one files so only one of someValue1 or someValue2 is true*/
b. Change an environment variable
c. change the time of the machine.
4. Use a Random generator in the code.
There are many ways to generate random results. Though technically not absolutely accurate - one of the runs in bound to generate a true first and a false the second time.
5. Byte code manipulation.
You can't recompile the code but you can always go ahead and change the byte code ;)
This should cover up most of the solutions - though I would suggest having a look at the comments for a real wide varity of solutions.
I would post the scores for this question as a separate post this weekend - watch out for it!!
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!
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!!
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!!
Puzzle 44 – Solution
The point to note here was that the argument type was same for all the constructor calls – namely String. Java 1.5 adds the variable arguments language feature makes it possible to call a method with a variable number of arguments. More information can be got at http://today.java.net/pub/a/today/2004/04/19/varargs.html
Declaring the constructor as ExtraLoad(String... s){} takes care that all the constructor have a definition!!
Declaring the constructor as ExtraLoad(String... s){} takes care that all the constructor have a definition!!
Subscribe to:
Posts (Atom)