Perl Christmas Quiz 2009

Abigail abigail at abigail.be
Tue Dec 1 11:26:36 GMT 2009


On Mon, Nov 30, 2009 at 11:40:04PM +0000, Chris Jack wrote:
> 
> 
> Abigail abigail at abigail.be wrote
> > 
> > On Mon, Nov 30, 2009 at 06:24:12PM +0000, Chris Jack wrote:
> 
> > > 7) Write a one line program that takes a non-negative integer as an argument
> > > and prints the square root when the answer's an integer.
> > > 
> > > Restrictions: the perl line should be a regular expression.
> > 
> > Just a regular expression? Regular expressions don't print, so that would
> > be impossible.
> 
> 
> Pedant. Perl regular expressions allow execution of arbitrary code blocks - which is why I put restrictions on which "ordinary" functions you were allowed to use. The actual square root algorithm, however, should only use the "normal" regular expression bestiary.


Do you mean by the latter that given a square number in unary, you want
a regexp determining the square root of that number, _without_ the use
of (?{ }) or (??{ })?

> 
> I was going to point you towards my talk on Perl one-liners - which shows the basic idea behind prime number checking and solving linear equations - but I can't find any of the talks on the London Perl Mongers website... The principal behind doing square root is similar but different.

Well, I guess you're looking for something like:

   /^(1+){"length($1)-1"}$/

which would be 'similar' but 'different' to my prime number checker. 
I'd be very interested to see how to do the C<< {"length($1)-1"} >>
without (?{ })/(??{ }).



Abigail


More information about the london.pm mailing list