aboutsummaryrefslogtreecommitdiff
path: root/challenge-335/bob-lied/perl/ch-1.pl
blob: 23d1d365b6bdc7ef38451230b1ac396b22937ebe (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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env perl
# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu:
#=============================================================================
# Copyright (c) 2025, Bob Lied
#=============================================================================
# ch-1.pl Perl Weekly Challenge 335 Task 1  Common Characters
#=============================================================================
# You are given an array of words. Write a script to return all characters
# that are in every word in the given array including duplicates.
# Example 1 Input: @words = ("bella", "label", "roller")
#           Output: ("e", "l", "l")
# Example 2 Input: @words = ("cool", "lock", "cook")
#           Output: ("c", "o")
# Example 3 Input: @words = ("hello", "world", "pole")
#           Output: ("l", "o")
# Example 4 Input: @words = ("abc", "def", "ghi")
#           Output: ()
# Example 5 Input: @words = ("aab", "aac", "aaa")
#           Output: ("a", "a")
#=============================================================================

use v5.42;
use feature 'keyword_all'; no warnings 'experimental::keyword_all';
use List::Util qw/min/;


use Getopt::Long;
my $Verbose = false;
my $DoTest  = false;
my $Benchmark = 0;

GetOptions("test" => \$DoTest, "verbose" => \$Verbose, "benchmark:i" => \$Benchmark);
my $logger;
{
    use Log::Log4perl qw(:easy);
    Log::Log4perl->easy_init({ level => ($Verbose ? $DEBUG : $INFO ),
            layout => "%d{HH:mm:ss.SSS} %p{1} %m%n" });
    $logger = Log::Log4perl->get_logger();
}
#=============================================================================

exit(!runTest()) if $DoTest;
exit( runBenchmark($Benchmark) ) if $Benchmark;

say '("', join('", "', common(\@ARGV)->@*), '")';

#=============================================================================
sub common($word)
{
    my @common;
    for my $c ( split(//, shift @$word) )
    {
        if ( scalar(@$word) == grep /$c/, @$word )
        {
            push @common, $c;
            $_ =~ s/$c// for @$word;
        }
    }
    return \@common;
}

sub commonIndex($word)
{
    my @common;
    for my $c ( split(//, shift @$word) )
    {
        if ( all { index($_, $c) != -1 } @$word )
        {
            push @common, $c;
            $_ =~ s/$c// for @$word;
        }
    }
    return \@common;
}

sub commonFreq($word)
{
    my %freq;
    my @common;
    for my $w ( $word->@* )
    {
        $freq{$w}{$_}++ for split(//, $w);
    }

    for my $letter ( keys $freq{$word->[0]}->%* )
    {
        my $count = min(map { $_->{$letter} // 0 } values %freq);
        push @common, ($letter) x $count;
    }
    return [ sort @common ];
}

sub runTest
{
    use Test2::V0;

    is( common( [qw(bella label roller)] ) , [ qw(e l l) ], "Example 1");
    is( common( [qw(cool lock cook    )] ), [ qw(c o)   ], "Example 2");
    is( common( [qw(hello world pole  )] ) , [ qw(l o)   ], "Example 3");
    is( common( [qw(abd def ghi       )] ) , [           ], "Example 4");
    is( common( [qw(aab aac aaa       )] ), [ qw(a a)   ], "Example 5");

    is( commonIndex( [qw(bella label roller)] ), [ qw(e l l) ], "Example 1");
    is( commonIndex( [qw(cool lock cook    )] ), [ qw(c o)   ], "Example 2");
    is( commonIndex( [qw(hello world pole  )] ), [ qw(l o)   ], "Example 3");
    is( commonIndex( [qw(abd def ghi       )] ), [           ], "Example 4");
    is( commonIndex( [qw(aab aac aaa       )] ), [ qw(a a)   ], "Example 5");

    is( commonFreq( [qw(bella label roller)] ), [ qw(e l l) ], "Example 1");
    is( commonFreq( [qw(cool lock cook    )] ), [ qw(c o)   ], "Example 2");
    is( commonFreq( [qw(hello world pole  )] ), [ qw(l o)   ], "Example 3");
    is( commonFreq( [qw(abd def ghi       )] ), [           ], "Example 4");
    is( commonFreq( [qw(aab aac aaa       )] ), [ qw(a a)   ], "Example 5");

    done_testing;
}

sub runBenchmark($repeat)
{
    use Benchmark qw/cmpthese/;

    say 'With all common';
    cmpthese($repeat, {
            grep  => sub { common( [('xy') x 244] ) },
            index => sub { commonIndex( [('xy') x 244] ) },
            freq  => sub { commonFreq( [('xy') x 244] ) }
        });

    say 'With no common';
    cmpthese($repeat, {
            grep  => sub { common( [('aa' .. 'zz')] ) },
            index => sub { commonIndex( [('aa' .. 'zz')] ) },
            freq  => sub { commonFreq( [('aa' .. 'zz')] ) }
        });

    say 'With long strings';
    my @word = ( 'a' x 1000 );
    foreach ( 'a' .. 'z' ) { push @word, ($_ x 999) . 'a' }
    cmpthese($repeat, {
            grep  => sub { common( [ @word ] ) },
            index => sub { commonIndex( [ @word ] ) },
            freq  => sub { commonFreq( [ @word ] ) }
        });
}