[kw-pm] Last night's meeting

John Macdonald john at perlwolf.com
Thu Jun 18 14:32:10 PDT 2009


The if statement modifier does not require parens around the condition,
which saves one more char (only the close paren is saved, the open paren
needs to be turned into a blank to separate "if" and "rand"):

$ cat alpha.html |perl -n -e'$s=$_ if rand()<1/++$n;END{print$s}'

Another shuffle save four more chars, by turning " if " into "?:1",
removing the divide, and removing resultant unneeded blanks:

$ cat alpha.html |perl -n -e'rand++$n<1?$s=$_:1;END{print$s}'

So, it's down to 31 chars.

An somewhat irrelevantly, the uncounted chars in front of the perl code can
be reduced too, first with removal of extraneous blanks:

$ cat alpha.html|perl -ne'rand++$n<1?$s=$_:1;END{print$s}'

and even more with removal of the extraneous command:

$ perl -ne'rand++$n<1?$s=$_:1;END{print$s}' <alpha.html

The XXX?YYY:1 looks wasteful, but unfortunately XXX&&YYY doesn't work
(the && is too low precedence to work with the assignment that follows),
while XXX andYYY costs an extra char (" and" vs "?:1", just as YYY if XXX
does).

On Thu, Jun 18, 2009 at 12:52:06PM -0400, Daniel R. Allen wrote:
> $ cat alpha.html |perl -n -e'$s=$_ if(rand()<1/++$n);END{print $s}'
> 
> Which, obviously, has an improvement, I don't know why I left that extra
> whitespace in there. For 35:
> 
> $ cat alpha.html |perl -n -e'$s=$_ if(rand()<1/++$n);END{print$s}'
> 
> 
> Thanks Abram for the excellent challenge.  There are nine other challenges
> on GolfChallenge in case we feel like doing this again.
> 
> -Daniel
> 
> On Thu, 18 Jun 2009, Abram Hindle wrote:
> 
> > On the topic of twitter I discussed tircd a tiny bit:
> >
> > http://github.com/abramhindle/tircd-plus-search/tree/master
> > http://code.google.com/p/tircd/
> >
> > We did a golf challenge from:
> > http://kw.pm.org/wiki/index.cgi?GolfChallenge
> >
> >     * Choose 1 uniformly randomly
> >
> > Write a perl script to select 1 element from a stream of unknown length
> > uniformly randomly. The algorithm usually is read an element in, choose
> > a number between 0 and 1, if it is less than 1/n then keep that new
> > element, otherwise keep your old one. So first element is 1/1 to keep,
> > second element is 1/2 to keep, third element is 1/3 to keep. Via
> > induction you can work it out that this algorithm is uniformly random.
> >
> > ^^ I "proved" this on the board via hand waving.
> >
> >
> > Here's my awk/bash version (bash seeds awk because gawk is bad at
> > seeding itself)
> > #!/bin/bash
> > awk "BEGIN {srand($RANDOM + $RANDOM)} {c = (rand() < (1.0 / FNR))?\$0:c}
> > END { print c }" $*
> >
> > Daniel wrote a tiny perl version (37 chars?) and max wrote a relatively
> > small (87 chars?) python version.
> >
> > If Daniel or Max could share their code with the list that'd be great.
> >
> > abram
> >
> >
> 
> _______________________________________________
> kw-pm mailing list
> kw-pm at pm.org
> http://mail.pm.org/mailman/listinfo/kw-pm


More information about the kw-pm mailing list