An odd question
There are always surprises when marking. But one thing caught both Clintons and my eyes. The number of different ways to determine an odd number. One of the exam question involved printign only the odd numbers in an array of integers. We just assumed people would use the remainder operator in a simple one line operation. Instead we got some very strange answers. We talked about it and agreed that many of the answers were in fact not correct since the students should have known about the remainder operator, but we would give them the points anyway for being able to come up with such creative solutions (on the condition that they actualy worked).
What follows is our collection of the top 8 interesting way to calculate odd nnumbers in Java. The order is simply the order we found them in, not an indication of how good we thought !
they were. Some code has been altered, and names have been removed to protect the innocent
(Sorry about some of the funny formatting. The damn blog engine is striping all the square brackets from the array references, so I had to put extra spaces in. Note to self: work on code formatting module SOON!)
<pre>
<br />class TestForOdd
<br />{
<br /> public static void main(String [] args)
<br /> {
<br /> /*
<br /> * from a question to determine the presence
<br /> * of odd numbers in an array of ints...
<br /> */
<br /> int[] odds = {1,2,3,4,5,6};//setup some numbers to test
<br />
<br /> /*
<br /> * first, the answer we were looking for...
<br /> */
<br /> System.out.println("ideal answer");
<br /> for(int i=0; i {
<br /> if(odds[ i ]%2==1)
<br /> System.out.println(odds[ i ]);
<br /> }
<br /> System.out.println();
<br />
<br /> /*
<br /> * One interesting solution...
!
<br /> * Exploiting integer divisions
<br /> * truncating effect.
<br /> */
<br /> System.out.println("Interesting solution MkI");
<br /> for(int i=0; i {
<br /> double first = (double)odds[ i ]/2;
<br /> int second = odds[ i ]/2;
<br /> if(first-second > 0.001)
<br /> System.out.println(odds[ i ]);
<br /> }
<br /> System.out.println();
<br />
<br /> /*
<br /> * Another solution...
<br /> * Counting in 2's
<br /> */
<br /> System.out.println("Interesting solution MkII");
<br /> for(int i=0; i {
<br /> int num = 1;
<br /> while(num {
<br /> if(num == odds[ i ])
<br /> System.out.println(odds[ i ]);
<br /> num+=2;
<br /> }
<br /> }
<br /> System.out.println();
<br />
<br /> /*
<br /> * A variation on MkI...
<br /> */
<br /> System.out.println("Interesting solution MkIII");
<br /> for(int i=0; i {
<br /> if(odds[ i ] - 2*(odds[ i ]/2)&gt!
;0)
<br /> System.out.println(odds[ i ]);
<br /> }
<br /> System.out.println();
<br />
<br /> /*
<br /> * Don't know what to say about this one.
<br /> * But it's cute.
<br /> */
<br /> System.out.println("Interesting solution mkIV");
<br /> for(int i=0; i {
<br /> int x = 1;
<br /> for(int j=0; j x = x*-1;
<br />
<br /> if(x System.out.println(odds[ i ]);
<br /> }
<br /> System.out.println();
<br />
<br /> /*
<br /> * Similar to MkII, but counting backwards in 2's
<br /> */
<br /> System.out.println("Interesting solution mkV");
<br /> for(int i=0; i {
<br /> int val = odds[ i ];
<br /> while(val>2) val-=2;
<br /> if(val==1)
<br /> System.out.println(odds[ i ]);
<br /> }
<br /> System.out.println();
<br />
<br /> /*
<br /> * Variation on MkI
<br /> */
<br /> System.out.println(&qu!
ot;Interesting solution mkVI");
<br /> for(int i=0; i {
<br /> double one = odds[ i ]/2.0;
<br /> int two = (int)one;
<br /> if(one!=two)
<br /> System.out.println(odds[ i ]);
<br /> }
<br /> System.out.println();
<br />
<br /> /*
<br /> * All odd numbers have least significant bit set
<br /> * This uses the bitwise AND (&) to mask value with 1.
<br /> * Nice ![]()
<br /> */
<br /> System.out.println("Interesting solution mkVII");
<br /> for(int i=0; i {
<br /> if((odds[ i ] & 1)==1)
<br /> System.out.println(odds[ i ]);
<br /> }
<br /> System.out.println();
<br />
<br /> /*
<br /> * And for those who like a more mathmatical approach...
<br /> */
<br /> System.out.println("Interesting solution mkVIII");
<br /> for(int i=0; i {
<br /> if((1 - Math.pow(-1, odds[ i ]) != 0))
<br /> System.out.println(odds[ i ]);
<br /> }
<br /> System.out.println();
<br /> }
<br />}
<br /></pre>
There were some others, but they were just variations on one of the above themes.]]>
]]>