$10K coding deathmatch

Barry Roberts blr at robertsr.us
Fri Nov 3 08:07:32 MST 2006


On Fri, Nov 03, 2006 at 02:27:25PM -0700, Bryan Sant wrote:

> Alright, just for fun, here is a Java submission.
> 
> import java.io.*;
> public class Q1 {
>  public static void main(String[] args) throws Exception {
>   BufferedReader reader = new BufferedReader(new 
>   InputStreamReader(System.in));
>   String line;
>   while ((line = reader.readLine()) != null) {
>     boolean match = true;
>     String[] nums = line.split(" ");
>     int len = nums.length - 1;
>     for (int i = 0; i < len; i++) {
>       int n = Integer.parseInt(nums[i]);
>       int next = Integer.parseInt(nums[i + 1]);
>       int diff = Math.abs(n - next);
>       if (diff > len || diff == 0)
>         match = false;
>     }
>     if (match)
>       System.out.println("match");
>     else
>       System.out.println("not a match");
>   }
> }
> }

Java start-up time is big enough you're never gonna win the 5-line
test, but I can't resist suggesting this short-ciruit optimzation to
see how much difference it makes, since all the others do it.

Change:
     for (int i = 0; i < len; i++) {
to:
     for (int i = 0; i < len && !match; i++) {


Barry



More information about the PLUG mailing list