diff options
| author | boblied <boblied@gmail.com> | 2023-03-13 21:28:45 -0500 |
|---|---|---|
| committer | boblied <boblied@gmail.com> | 2023-03-13 21:28:45 -0500 |
| commit | d7b80c00f371f8b78e9b8b2a68c8a47e08667f52 (patch) | |
| tree | 0bee80eb17b89408f012b9520faa0d85c21b7099 | |
| parent | 0ba97a2f9d9cad2e9392a567d979b63210251e10 (diff) | |
| download | perlweeklychallenge-club-d7b80c00f371f8b78e9b8b2a68c8a47e08667f52.tar.gz perlweeklychallenge-club-d7b80c00f371f8b78e9b8b2a68c8a47e08667f52.tar.bz2 perlweeklychallenge-club-d7b80c00f371f8b78e9b8b2a68c8a47e08667f52.zip | |
Week 208 task 1
| -rw-r--r-- | challenge-208/bob-lied/perl/ch-1.pl | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/challenge-208/bob-lied/perl/ch-1.pl b/challenge-208/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..d2b5351331 --- /dev/null +++ b/challenge-208/bob-lied/perl/ch-1.pl @@ -0,0 +1,101 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-1.pl Perl Weekly Challenge Week 208 Task 1 Minimum Index Sum +#============================================================================= +# Copyright (c) 2023, Bob Lied +#============================================================================= +# You are given two arrays of strings. +# Write a script to find out all common strings in the given two arrays with +# minimum index sum. If no common strings found returns an empty list. +# Example 1 Input: @list1 = ("Perl", "Raku", "Love") +# @list2 = ("Raku", "Perl", "Hate") +# Output: ("Perl", "Raku") +# There are two common strings "Perl" and "Raku". +# Index sum of "Perl": 0 + 1 = 1 +# Index sum of "Raku": 1 + 0 = 1 +# Example 2 Input: @list1 = ("A", "B", "C") +# @list2 = ("D", "E", "F") +# Output: () +# No common string found, so no result. +# Example 3 Input: @list1 = ("A", "B", "C") +# @list2 = ("C", "A", "B") +# Output: ("A") +# There are three common strings "A", "B" and "C". +# Index sum of "A": 0 + 1 = 1 +# Index sum of "B": 1 + 2 = 3 +# Index sum of "C": 2 + 0 = 2 +#============================================================================= + +use v5.36; + +use List::Util qw/min/; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +######### +# Command line +######### +use feature 'try'; no warnings "experimental::try"; +my @list1 = (); +my @list2 = (); +try +{ + @list1 = defined $ARGV[0] && split(/[ ,]/, $ARGV[0]); + @list2 = defined $ARGV[1] && split(/[ ,]/, $ARGV[1]); +} +catch ($e) { + say "Caught: ", $e; +} + +say "LIST1: [@list1]\nLIST2: [@list2]" if $Verbose; + +say "(", join(",", map { qq("$_") } minIndexSum(\@list1, \@list2)->@*), ")"; + +# Convert a list to an index lookup hash. Example: +# [ a, b ] becomes { a => 0, b => 1 } +sub asHash($list) +{ + my %h = map { $list->[$_] => $_ } 0 .. $#{$list}; + return \%h; +} + +sub minIndexSum($list1, $list2) +{ + my @result; + + my ($h1, $h2) = ( asHash($list1), asHash($list2) ); + + my %indexSum = map { $_ => ( $h1->{$_} + $h2->{$_} ) } + grep { exists $h2->{$_} } keys %$h1; + + my $min = min(values %indexSum); + + @result = grep { $indexSum{$_} == $min } keys %indexSum; + + return \@result; +} + +sub runTest +{ + use Test2::V0; + + is( minIndexSum( [ qw(Perl Raku Love) ], [ qw(Raku Perl Hate) ] ), + [ qw(Perl Raku) ], "Example 1"); + + is( minIndexSum( [ qw(A B C) ], [ qw(D E F) ] ), [], "Example 2"); + + is( minIndexSum( [ qw(A B C) ], [ qw(C A B) ] ), [ "A" ], "Example 3"); + + is( minIndexSum( [ ], [ qw(A B C) ] ), [], "list 1 empty"); + is( minIndexSum( [ qw(A B C) ], [ ] ), [], "list 2 empty"); + is( minIndexSum( [ ], [ ] ), [], "both lists empty"); + + done_testing; +} + |
