Perl Christmas Quiz

Hakim Cassimally hakim.cassimally at gmail.com
Mon Dec 15 17:57:04 GMT 2008


On 15/12/2008, Hakim Cassimally <hakim.cassimally at gmail.com> wrote:
> On 15/12/2008, Avleen Vig <avleen at gmail.com> wrote:
>  > On Dec 15, 2008, at 10:04, "James Laver" <james.laver at gmail.com> wrote:
>  > > On Sun, Dec 14, 2008 at 3:15 PM, Avleen Vig <avleen at gmail.com> wrote:
>  > > > On Fri, Dec 12, 2008 at 9:50 AM, Chris Jack <chris_jack at msn.com> wrote:
>  > > > > 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.
>  > > > >
>  > > > In the spirit of sharing, I offer this solution, from your neighbours
>  > > > in the Python community:
>
> <snip>
>
> In the same spirit (and once pndc had corrected me on the spec...
>  d'oh!) I attempted in Haskell... but pretty much failed at a compact
>  solution:

Aha!  Haskell has an 'intersect' function... though it doesn't quite
do the min(n,m) thing, as it shows *all* the repetitions in the LHS.
Still, if you look at its source, you can get the following one-liner
(which is possibly less efficient than my original?)

  minIntersect xs ys = let g=group.sort in [ zipWith const x y | x <-
g xs, y <- g
                  ys, head x == head y ]

osfameron
--


More information about the london.pm mailing list