Perl Golf Part 2

| 13 Comments
Before submitting, please see my correction above

Earlier this week I posted about perl golf at my work: Well my deadline has passed so I will pass this onto the rest of you. If you want to put your code in the comments below I will judge them.

The goal of course is to accomplish this using the fewest characters possible in your source code.

The rules:

*) The program must compile on perl 5.8.5 (to accommodate the test env)
*) The program must accept an arbitrary number of command-line arguments, which will all be numbers that your program should sort
*) The program must produce a list of the sorted inputs, sorted from lowest to highest
*) Any program that crashes (itself or the computer) loses -- even if it produces the right output
*) After 3 minutes of execution the running of the program will be terminated. (I will consider load on the host computer for this requirement).
*) No other executables may be used (no system calls or backticks, etc)
*) You may NOT use any additional Perl modules
*) The program exit code doesn't matter
*) You should not print anything to STDERR, only STDOUT
*) Your program should not be dependent on filename (it will be run from inside a file)
*) Team entries can be submitted. Please list all members of the team in the e-mail containing the code.
*) I will post my solutions on the 30th. Submissions accepted untill 8:00 AM CDT July 30th
*) I will post the results of the competition on the August 8th time permitting.
*) The shortest solution in bytes will win, so feel free to strip out the whitespace.
*) The tiebreaker will be the number of distinct characters used. For example: if 2 programs clock in at 70 characters, but one uses 12 distinct characters and the other 15, the one using 12 distinct characters will win.

13 Comments

The program must accept an arbitrary number of command-line arguments, which will all be numbers that your program should sort

Integers or more? .2, .9e-10, 0xf etc. come to mind.

... If I've understand, that could be valid ?

$ echo "9 2 7 1 4 8 3 13 35 16 32 24" | \
    perl -lane 'print join" ",sort{$a<=>$b}@F;'
1 2 3 4 7 8 9 13 16 24 32 35

In a file instead of onliner:

$ cat sortnumbers.pl 
#!/usr/bin/perl
print join" ",sort{$a<=>$b}@ARGV;
$ 

Sure I haven't get all the points.

I think I'm missing something.

perl -e '$,=$";print sort{$a<=>$b}@ARGV'

Testing:

perl -e '$,=$";print sort{$a<=>$b}@ARGV' $( seq 1 0.1 10000 | sort -R )
perl -e '$,=$";print sort{$a<=>$b}@ARGV' 1 2 3 4 9 8 7 6 10 101 0.2 5e6

# All appear to work.


You can save yourself two characters by using - instead of <=>.

Please define "a list of the sorted inputs". Is that "1,2,3" or "1\n2\n3\n".

I am not using anything tricky besides using - instead of <=>, -l, and a + to avoid duplicating a space (see the last rule). This makes me certain there is something I am not thinking of.

#!perl -l
print for+sort{$a-$b}@ARGV

I initially tried $a - $b, but it doesn't work.


perl -e '$,=$";print sort {$a-$b} @ARGV' -- $( seq -100 0.1 100 | sort -R )

-99.4 -99.9 -99.7 -99.8 -99.6 -98.4

It looks like floating point math is breaking it. Some of those subtractions are leading to numbers very close to 0 instead of 0.

Yeah, sort appears to treat -1

Fail:

perl -E '$,=$";print sort { ( $a $b ) * 0.9999999 } @ARGV' -- $( seq -100 1 100 | sort -R )

Success:

perl -E '$,=$";print sort { ( $a $b ) * 1.9999999 } @ARGV' -- $( seq -100 1 100 | sort -R )

Bah. I Hate HTML sometimes. I mean, it treats values between negative one and one as all being 0.

and thats $a <=> $b in those examples.

Your current blogs normally have got a decent amount of really up to date info. Where do you come up with this? Just stating you are very inspiring. Thanks again

About this Entry

This page contains a single entry by leonard published on July 23, 2010 1:20 PM.

perl golf was the previous entry in this blog.

Perl Golf Part 2.1 is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.