Perl Christmas Quiz

Piers Cawley pdcawley at gmail.com
Fri Dec 12 13:28:20 GMT 2008


On Fri, Dec 12, 2008 at 12:10 PM, Paul Makepeace <paulm at paulm.com> wrote:
> 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,

Is there a Bag::Scalar?

If so, s/Set/Bag/ in the above fragment would do the job. Implementing
Bag::Scalar is left as an exercise for the interested reader.


More information about the london.pm mailing list