Computer Science Canada

Test Your Skills: Smalltalk Edition

Author:  wtd [ Mon May 21, 2007 11:19 am ]
Post subject:  Test Your Skills: Smalltalk Edition

Write the shortest possible (non-obfuscated) code which takes an array of integers "foo" and finds the first element which is a multiple of both 7 and 2. It should store this element in a variable "bar". If no such element is found, zero should be stored in "bar" and the following message printed as a side-effect.

Quote:
Nothing found, assuming default value.


A Java implementation is provided.

code:
int[] foo = new int[] { 3, 5, 2, 8, 7, 9, 14 };
int result = 0;
boolean resultFound = false;

for (int index = 0; index < foo.length; index++) {
   if (foo[index] % 14 == 0) {
      result = foo[index];
      resultFound = true;
      break;
   }
}

if (!resultFound) {
   System.out.println("Nothing found, assuming default value.");
}


: