Code Golf 3: Googler

Code Golf. As with the real sport the goal is to reduce the number of strokes that it takes to complete a particular objective, although with Code Golf “strokes” refers to keystrokes rather than swings of a golf club.

Write code to return the url’s of the first page of search results for the query ‘perl’ on google.com

There’ll be two categories, one for using perl’s Socket module only, and for those using external libraries.

Good luck!

Code Golf 2: Line Sort

Code Golf. As with the real sport the goal is to reduce the number of strokes that it takes to complete a particular objective, although with Code Golf “strokes” refers to keystrokes rather than swings of a golf club.

Create an algorithm that reads lines from the stdin until it hits an EOF and prints those lines sorted to the stdout

  • Don’t use the language’s native sorting capabilities, write your own sorting algorithm. (print for sort<> would be too easy.)
  • Any language is allowed, except for those that myseriously implement a 1 byte command that does exactly this.

Example implementation using inverted bubble sort:

@a = <>;
for ($i = 0; $i <= $#a; $i++) {
 for ($j = $i; $j <= $#a; $j++) {
  if (@a[$i] ge @a[$j]) {
   $t = @a[$j];
   @a[$j] = @a[$i];
   @a[$i] = $t;
  }
 }
}
foreach(@a) {
 print;
}

This implementation is very large and inefficient, and just an example.

Good luck!

Code Golf 1: Fibonacci

Code Golf. As with the real sport the goal is to reduce the number of strokes that it takes to complete a particular objective, although with Code Golf “strokes” refers to keystrokes rather than swings of a golf club.

You may compete using any language.1

Create an algorithm that prints the 30 first fibonacci number to the screen, each followed by a newline.

An algorithm which could do this would be (148 bytes):

my $a = 1;
my $b = 0;
my $c;

my $limit = 30;
my $i = 0;

while ($i < $limit) {
        $i += 1;
        $c = $a + $b;
        $a = $b;
        $b = $c;
        print $c . "\n";
}

But that can be written way shorter.

My first:

$a=1;for((0..29)){$c=$a+$b;$a=$b;$b=$c;print"$c\n"}

My second:
$a=1;$c=$a+$b,$a=$b,$b=$c,print"$c\n"for 0..29

Noud’s reponse:
$i=1;print$i+=$a,"\n",$a+=$i,"\n"while$i<317811

My revenge:
$a=1;$c=$a+$b,$a=$b,$b=$c,print"$c\n"for 0..29

Noud was 30 seconds later with:
$a=1;print$a+=$b,"\n",$b+=$a,"\n"for 1..15

My latest one: partially by Bram too
print$a+=$b,"\n",$b+=$a,"\n"for$a++..14

Update:Twan:
print$a+=$b,$/,$b+=$a,$/for$a++..14

1. a language that implements fib returning mysteriously the first 30 fibonacci numbers isn’t allowed.