On topic: The Wednesday Challenge

Nigel Rantor wiggly at wiggly.org
Wed Aug 15 15:46:12 BST 2007


Andy Armstrong wrote:
> Given a 64 Mbyte file containing random binary data write a perl program 
> such that
> 
> $ perl myprog.pl < 64m.bin > 64m.sorted.bin
> 
> outputs a file 64m.sorted.bin with all the bytes sorted in ascending 
> order. Marks will be given for speed, brevity and parsimonious use of 
> memory.
> 
> --Andy Armstrong, hexten.net
> 
> 

#!/usr/bin/perl

use warnings;
use strict;

my @byte;

my $char;

while( read( STDIN, $char, 1 ) )
{
     $byte[ord($char)]++;
}

foreach my $ord ( 0..255 )
{
     next unless $byte[$ord];
     print chr($ord) x $byte[$ord];
}


More information about the london.pm mailing list