Perl Christmas Quiz

Matt Lawrence matt.lawrence at virgin.net
Mon Dec 15 13:44:55 GMT 2008


Avleen Vig 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:
>
> a = ['m', 'n', 'o', 'o', 'p', 'p', 'q']
> b = ['n', 'p', 'q', 'r', 'r', 's']
>
> def FindSetMatches(list1, list2):
>   for i in set(list1).intersection(set(list2)):
>     print '%s min(%s, %s)' % (i, list1.count(i), list2.count(i))
>   
Doesn't that output 'p min(1, 1)' instead of just 'p', etc.

Expected output would be p, q, n. If there was another p in list b, it 
should be p, p, q, n.

Matt



More information about the london.pm mailing list