From: Chris Devers <[EMAIL PROTECTED]>
Date: Tue, 8 Oct 2002 23:18:33 -0400 (EDT)
On a different note, last weekend on NPR there was a puzzle that it seems
to me could be solved pretty neatly by a Perl script, and I'm curious
what solutions people would try for it. Consider the following string:
1 2 3 4 5 6 7 8 9 = 2002
The problem is to add any number of addition & multiplication operations
wherever you'd like on the left such that in the end you have a valid
equation. So for example if it gets you to a solution you can have:
12 * 345 + 6 ...
if that works as part of your solution [it's much too big: 4146].
Removing obvious fat from my first solution, I get:
#!/usr/bin/perl
eval($_)==2002 && print "$_\n"
for build(qw(1 2 3 4 5 6 7 8 9));
sub build {
$n = shift;
return $n unless @_;
return map {("$n$_","$n+$_","$n*$_")} build(@_);
}
Which doesn't quite fit on one line:
perl -e 'eval$_==2002&&print"$_\n"for b(1,2,3,4,5,6,7,8,9);\
sub b{my$n=shift;@_?map{("$n$_","$n+$_","$n*$_")}b(@_):$n}'
A more compact way to do the three alternatives inside the map?
A way to eliminate "my$n=shift"?
--kag
_______________________________________________
Boston-pm mailing list
[EMAIL PROTECTED]
http://mail.pm.org/mailman/listinfo/boston-pm