aboutsummaryrefslogtreecommitdiff
path: root/challenge-277/kjetillll/perl/ch-1.pl
blob: c1e4604e84253e900f7a383754440cf1cbe60008 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
use strict; use warnings;

sub count_common {
    my @lists = @_;
    my %count;
    for my $i ( 0 .. @lists-1 ) {
        for my $word ( @{ $lists[$i] } ) {
            $count{$word}{$i}++
        }
    }
    my $onestring = join ' ', (1) x @_;
    0 + grep join(' ',values %{ $count{$_} }) eq $onestring, sort keys %count;
}

#---------------------------------------- tests
use Test::More tests=>3;
is count_common( @$_{'words1','words2'} ), $$_{output}
    for { words1 => ["Perl", "is", "my", "friend"],
          words2 => ["Perl", "and", "Raku", "are", "friend"],
          output => 2 }, # "Perl" and "friend" appear once in each array.
        { words1 => ["Perl", "and", "Python", "are", "very", "similar"],
          words2 => ["Python", "is", "top", "in", "guest", "languages"],
          output => 1 }, # "Python" appear once in each array
        { words1 => ["Perl", "is", "imperative", "Lisp", "is", "functional"],
          words2 => ["Crystal", "is", "similar", "to", "Ruby"],
          output => 0 }; # "is" appear in both arrays but twice in word1