Peter
Peter Campbell Smith

Commons and pairs

Weekly challenge 277 — 8 July 2024

Week 277: 8 Jul 2024

Task 1

Task — Count common

You are given two array of strings, @words1 and @words2. Write a script to return the count of words that appears in both arrays exactly once.

Examples


Example 1
Input: @words1 = ('Perl', 'is', 'my', 'friend')
       @words2 = ('Perl', 'and', 'Raku', 'are', 'friend')
Output: 2
The words 'Perl' and 'friend' appear once in each array.

Example 2
Input: @words1 = ('Perl', 'and', 'Python', 'are', 'very',
                  'similar')
       @words2 = ('Python', 'is', 'top', 'in', 'guest', 
                  'languages')
Output: 1

Example 3
Input: @words1 = ('Perl', 'is', 'imperative', 'Lisp',
                  'is', 'functional')
       @words2 = ('Crystal', 'is', 'similar', 'to', 
                  'Ruby')
Output: 0

Analysis

This boils down to counting the frequency of each word in each array and then finding the words that occur once in each.

That's one loop over each array and then another loop over the combined set of words and their frequencies. It's hard (for me) to see a quicker (or shorter) answer, but probably someone else will.

Try it 

Try running the script with any input:



example: cows eat grass



example: cats eat fish

Script


#!/usr/bin/perl

# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge

use v5.26;    # The Weekly Challenge - 2024-07-08
use utf8;     # Week 277 - task 1 - Count common
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';

count_common([qw(the fat cat sat on the mat)], [qw(the loud dog ran on the grass)]);
count_common([qw(You are given two arrays of strings)], [qw(Write a script to return the count)]);
count_common([qw(all good things come to an end)], [qw(end an to come things good all)]);
count_common([qw(one three)], [qw(ONE TWO)]);

sub count_common {
    
    my (@arrays, $j, @singles, %count, $word, $output);
    
    @arrays = @_;
    $output = 0;
    
    # count word frequency  
    for $j (0 .. 1) {
        $count{$_}[$j] ++ for @{$arrays[$j]};
    }
    
    # find ones that occur once in each sentence
    for $word (keys %count) {
        $output ++ if ($count{$word}[0] or 0) == 1 and ($count{$word}[1] or 0) == 1;
    }
    
    printf(qq[\nInput:  \@words1 = '%s'\n        \@words2 = '%s'\n], 
        join(qq[', '], @{$arrays[0]}), join(qq[', '], @{$arrays[1]}));
    say qq[Output: $output];
}

Output


Input:  @words1 = 'the', 'fat', 'cat', 'sat', 'on', 'the',
                  'mat'
        @words2 = 'the', 'loud', 'dog', 'ran', 'on',
                  'the', 'grass'
Output: 1

Input:  @words1 = 'You', 'are', 'given', 'two', 'arrays',
                  'of', 'strings'
        @words2 = 'Write', 'a', 'script', 'to', 'return',
                  'the', 'count'
Output: 0

Input:  @words1 = 'all', 'good', 'things', 'come', 'to',
                  'an', 'end'
        @words2 = 'end', 'an', 'to', 'come', 'things', 
                  'good', 'all'
Output: 7

Input:  @words1 = 'one', 'three'
        @words2 = 'ONE', 'TWO'
Output: 0

 

Any content of this website which has been created by Peter Campbell Smith is in the public domain