On Sun, Dec 14, 2008 at 3:15 PM, Avleen Vig <avl...@gmail.com> wrote: > On Fri, Dec 12, 2008 at 9:50 AM, Chris Jack <chris_j...@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)) > > > :-) >
Using other languages is really cheating. PHP has a function called array_intersect ( http://uk.php.net/array_intersect -- yes, you can just put a function name and mod_rewrite will help find the docs, it would be nice to have something similar for perl ). This works if you don't care about the multiples