Perl Christmas Quiz
Paul Makepeace
paulm at paulm.com
Fri Dec 12 12:10:19 GMT 2008
SPOILERS contd
On Fri, Dec 12, 2008 at 11:37 AM, Joel Bernstein <joel at fysh.org> wrote:
> 2008/12/12 Chris Jack <chris_jack at msn.com>:
>> 3) Write a Perl function that takes two references to arrays and returns the intersect of them. If an entry appears n times in array 1 and m times in array 2, the output should list that entry min(n,m) times. Bonus mark for one line solutions.
>
> use Set::Scalar;
> sub intersect (\@\@) {
> my ($a1, $a2) = map { Set::Scalar->new(@$_) } @_;
> my $intersection = $a1 * $a2;
> return $intersection->elements;
> }
This isn't a set question though. Sets have unique membership,
stix:~$ perl -MSet::Scalar -le '$s = Set::Scalar->new(1,1); print $s->elements'
1
Something like,
sub intersect {
my @s1 = sort @{$_[0]};
my @s2 = sort @{$_[1]};
my @s3;
while (my $e = $s1[0]) {
if ($e == $s2[0]) {
push @s3, $e;
shift @s2;
shift @s1;
} elsif ($e > $s2[0]) {
shift @s2;
} else {
shift @s1;
}
}
@s3;
}
There's probably some clever answer using List::MoreUtils.
P
>
> Surely you didn't want a wheel-reinvention answer? ;-)
>
>> 4) How many different variable types are there in Perl? Be as sensibly voluminous in your answer as you are able.
>
> SCALAR ARRAY HASH CODE
> FileHandle DirHandle Regexp
>
> But clearly we distinguish between numeric and string scalar values,
> so I'm wondering how exhaustive that list is...
>
>
>> 5) What animal is on the front of the Perl Cookbook (bonus mark for knowing both the first and second edition)?
>
> A horned sheep of some kind. Don't think I've ever seen the first ed though...
>
>> 7) What does the L in Randal L Schwartz stand for?
>
> Loser.
>
>> 8) Name a Perl module Leon (Brocard) has written (bonus mark if you've used it).
>
> Devel::ebug
>
>> 9) When will Perl 6 be released?
>
> Never.
>
> Bored now.
>
> /joel
>
More information about the london.pm
mailing list