NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

## All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.

#### New Scientist Enigmas5 Comments More | Login | Reply/

Full
Abbreviated
Hidden
More | Login | Reply
Loading... please wait.
• #### My solution(Score:1)

Here [dpaste.com] is my solution in Python... my Perl is not strong enough yet :)

There are three sets of 6 numbers which satisfy the conditions. The min and max of those three sets is the same for all three, which can be the only answer to the problem.

I didn't find the wording ambiguous at all.

• #### Re:(Score:1)

Cool! It's nice to compare strengths/weaknesses/(or just plain differences) of both languages. This syntax is terse and if it were perl (and the function were not clearly named), people would probably complain that it's confusing and unreadable:

reverse_num = lambda n: int(str(n)[::-1])

It's nice that you can slice a string (like substr() on steroids()), though not so nice that you have to cast the number as a string before you reverse it.

It's also nice that sets are built into the language, and "+" seem

• #### Re:(Score:1)

Here's my go:

#!/usr/bin/perl

use strict;
use warnings;
use List::Util qw(min max);

my @nlist = grep !( /0/ || /(.).*\1/ ), 123..987;

my %f;
for my \$n ( qw( 7 9 11 ) ) {
\$f{\$n} = sub {
my \$str = join('', @_);
my \$re = qr/[0\$str]/;
grep !( \$_ % \$n || reverse() % \$n || /\$re/ ), @nlist;
};
}

my (\$min, \$max) = (1000, 0);

for my \$d_7 ( \$f{7}->() ) {
for my \$d_9 ( \$f{9}->(\$d_7) ) {
for my \$d_11 ( \$f{11}->(\$d_7, \$d_9) ) {

• #### Re:(Score:1)

And just when I think I'm done...I realize the first part can be replaced with this:

my %f;
for my \$n ( qw( 7 9 11 ) ) {
my @nlist = grep !( \$_ % \$n || reverse() % \$n || /0/ || /(.).*\1/ ), 123..987;
\$f{\$n} = sub {
my \$str = join('', @_);
my \$re = qr/[0\$str]/;
grep !/\$re/, @nlist;
};
}
• #### Re:My solution(Score:2)

by pudge (1) on 2008.06.19 18:29 (#63472) Homepage Journal

Unsurprisingly, we used a lot of the same elements, including /(.).?\1/. I decided to recurse. Since these are all three-digit numbers, a simple (sort @nums)[0,-1] gives min and max, so as soon as we get a hit in 11 we have all the info we need. So yours could be shortened quite a bit further:

for my \$d_7 ( \$f{7}->() ) {
for my \$d_9 ( \$f{9}->(\$d_7) ) {
for my \$d_11 ( \$f{11}->(\$d_7, \$d_9) ) {
printf "min: %d, max: %d\n",
(sort(map { reverse(\$_)+0, \$_ } \$d_7, \$d_9, \$d_11))[0,-1];
exit;
}
}
}
Mine is here.

#!/usr/bin/perl
use warnings;
use strict;

my @six = map { \$_, reverse(\$_)+0 } foo([grep { !/0/ && !/(.).?\1/ } 123..987], [7, 9, 11]);
printf "7: %d/%d, 9: %d/%d, 11:%d/%d\nAnswer: %d : %d\n", @six, (sort @six)[0, -1];

sub foo {
my(\$d, \$n) = @_;
my \$x = shift @\$n;
for my \$i (@\$d) {
if (!(\$i % \$x) && !(reverse(\$i) % \$x)) {
if (@\$n) {
my @t = foo([grep !/[\$i]/, @\$d], [@\$n]);
return(\$i, @t) if @t;
} else {
return \$i;
}
}
}
return;
}