From 4895c63c67294452dcf9fa4e8f3f47e4a3f1c39f Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Tue, 14 Mar 2023 16:22:28 +0000 Subject: Add Perl solution --- challenge-208/paulo-custodio/Makefile | 2 + challenge-208/paulo-custodio/README | 1 + challenge-208/paulo-custodio/perl/ch-1.pl | 74 ++++++++++++++++++++++++++++++ challenge-208/paulo-custodio/perl/ch-2.pl | 57 +++++++++++++++++++++++ challenge-208/paulo-custodio/t/test-1.yaml | 15 ++++++ challenge-208/paulo-custodio/t/test-2.yaml | 15 ++++++ 6 files changed, 164 insertions(+) create mode 100644 challenge-208/paulo-custodio/Makefile create mode 100644 challenge-208/paulo-custodio/README create mode 100644 challenge-208/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-208/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-208/paulo-custodio/t/test-1.yaml create mode 100644 challenge-208/paulo-custodio/t/test-2.yaml diff --git a/challenge-208/paulo-custodio/Makefile b/challenge-208/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-208/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-208/paulo-custodio/README b/challenge-208/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-208/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-208/paulo-custodio/perl/ch-1.pl b/challenge-208/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..c11b91e5f4 --- /dev/null +++ b/challenge-208/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,74 @@ +#!/usr/bin/perl + +# Challenge 208 +# +# Task 1: Minimum Index Sum +# Submitted by: Mohammad S Anwar +# +# 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 Modern::Perl; + +sub common_strings { + my($list1, $list2) = @_; + my @list1 = @$list1; + my @list2 = @$list2; + my $min_index = @list1 + @list2; + my @common; + + for my $i (0 .. $#list1) { + for my $j (0 .. $#list2) { + if ($list1[$i] eq $list2[$j]) { + if ($i+$j < $min_index) { + $min_index = $i+$j; + @common = $list1[$i]; + } + elsif ($i+$j == $min_index) { + push @common, $list1[$i]; + } + } + } + } + + return @common; +} + +my @list1 = @ARGV[0..@ARGV/2-1]; +my @list2 = @ARGV[@ARGV/2..$#ARGV]; + +my @common = common_strings(\@list1, \@list2); +say @common ? "@common" : "()"; diff --git a/challenge-208/paulo-custodio/perl/ch-2.pl b/challenge-208/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..527c589197 --- /dev/null +++ b/challenge-208/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,57 @@ +#!/usr/bin/perl + +# Challenge 208 +# +# Task 2: Duplicate and Missing +# Submitted by: Mohammad S Anwar +# +# You are given an array of integers in sequence with one missing and one duplicate. +# +# Write a script to find the duplicate and missing integer in the given array. +# Return -1 if none found. +# +# For the sake of this task, let us assume the array contains no more than one +# duplicate and missing. +# +# Example 1: +# +# Input: @nums = (1,2,2,4) +# Output: (2,3) +# +# Duplicate is 2 and Missing is 3. +# +# Example 2: +# +# Input: @nums = (1,2,3,4) +# Output: -1 +# +# No duplicate and missing found. +# +# Example 3: +# +# Input: @nums = (1,2,3,3) +# Output: (3,4) +# +# Duplicate is 3 and Missing is 4. + +use Modern::Perl; + +sub dup_and_missing { + my(@in) = @_; + my @dup; + my @missing; + my %in; $in{$_}++ for @in; + for (1 .. @in) { + push @dup, $_ if exists $in{$_} && $in{$_}>1; + push @missing, $_ if !exists $in{$_}; + } + if (@dup+@missing == 0) { + return -1; + } + else { + return (@dup, @missing); + } +} + +my @in = @ARGV; +say join(" ", dup_and_missing(@in)); diff --git a/challenge-208/paulo-custodio/t/test-1.yaml b/challenge-208/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..4295422433 --- /dev/null +++ b/challenge-208/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: Perl Raku Love Raku Perl Hate + input: + output: Perl Raku +- setup: + cleanup: + args: A B C D E F + input: + output: () +- setup: + cleanup: + args: A B C C A B + input: + output: A diff --git a/challenge-208/paulo-custodio/t/test-2.yaml b/challenge-208/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..e6d696824c --- /dev/null +++ b/challenge-208/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 1 2 2 4 + input: + output: 2 3 +- setup: + cleanup: + args: 1 2 3 4 + input: + output: -1 +- setup: + cleanup: + args: 1 2 3 3 + input: + output: 3 4 -- cgit