diff options
| author | Mariano Ortega <mgo1977@gmail.com> | 2024-07-09 19:35:04 -0300 |
|---|---|---|
| committer | Mariano Ortega <mgo1977@gmail.com> | 2024-07-09 19:35:04 -0300 |
| commit | 89457ce1f31251d7a2a3372aee4c6ef8fc2656d2 (patch) | |
| tree | 75c5e3e11516539795b3d0f92449c3d841d25bdd | |
| parent | 96580cd62594876fa534d57eb553637433c8ba04 (diff) | |
| download | perlweeklychallenge-club-89457ce1f31251d7a2a3372aee4c6ef8fc2656d2.tar.gz perlweeklychallenge-club-89457ce1f31251d7a2a3372aee4c6ef8fc2656d2.tar.bz2 perlweeklychallenge-club-89457ce1f31251d7a2a3372aee4c6ef8fc2656d2.zip | |
mgo1977 - PWC 277
| -rw-r--r-- | challenge-277/mgo1977/perl/ch-1.pl | 87 | ||||
| -rw-r--r-- | challenge-277/mgo1977/perl/ch-2.pl | 86 |
2 files changed, 173 insertions, 0 deletions
diff --git a/challenge-277/mgo1977/perl/ch-1.pl b/challenge-277/mgo1977/perl/ch-1.pl new file mode 100644 index 0000000000..cd242255b3 --- /dev/null +++ b/challenge-277/mgo1977/perl/ch-1.pl @@ -0,0 +1,87 @@ +#!/bin/perl -w + + +use Data::Dump qw(dump); + + +# Task 1: 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. + +# 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 + + +testMe(\&process, 'Example1', ["Perl", "is", "my", "friend"], ["Perl", "and", "Raku", "are", "friend"], 2); +testMe(\&process, 'Example2', ["Perl", "and", "Python", "are", "very", "similar"], ["Python", "is", "top", "in", "guest", "languages"], 1); +testMe(\&process, 'Example3', ["Perl", "is", "imperative", "Lisp", "is", "functional"], ["Crystal", "is", "similar", "to", "Ruby"], 0); + +sub testMe { + my $processor = shift; + my $name = shift; + my $input1 = shift; + my $input2 = shift; + my $expectedValue = shift; + + my $got = &$processor($input1, $input2); + + if ( $got==$expectedValue ) { + print "[OK ] $name\n"; + } + else { + print "[ERR] $name :: got=$got, expectedValue=$expectedValue\n"; + } + +} + + +sub process { + my $input1 = shift; + my $input2 = shift; + + my $total = 0; + + my $countMap1 = countWords($input1); + my $countMap2 = countWords($input2); + + foreach my $word ( keys %$countMap1 ) { + + if ( + exists $countMap1->{$word} && $countMap1->{$word}==1 && + exists $countMap2->{$word} && $countMap2->{$word}==1 + ) { + ++$total; + } + + } + + return $total; + +} + + +sub countWords { + my $wordList = shift; + + my $map = {}; + + if ( defined $wordList ) { + $map->{$_}++ foreach ( @$wordList ); + } + + return $map; +} + diff --git a/challenge-277/mgo1977/perl/ch-2.pl b/challenge-277/mgo1977/perl/ch-2.pl new file mode 100644 index 0000000000..f8d4d23441 --- /dev/null +++ b/challenge-277/mgo1977/perl/ch-2.pl @@ -0,0 +1,86 @@ +#!/bin/perl -w + +use feature 'signatures'; + +# Task 2: Strong Pair + +# You are given an array of integers, @ints. + +# Write a script to return the count of all strong pairs in the given array. + +# A pair of integers x and y is called strong pair if it satisfies: 0 < |x - y| < min(x, y). + +# Example 1 +# Input: @ints = (1, 2, 3, 4, 5) +# Ouput: 4 +# Strong Pairs: (2, 3), (3, 4), (3, 5), (4, 5) + +# Example 2 +# Input: @ints = (5, 7, 1, 7) +# Ouput: 1 + +# Strong Pairs: (5, 7) + +testMe(\&process, 'Example1', [1, 2, 3, 4, 5], 4); +testMe(\&process, 'Example2', [5, 7, 1, 7], 1); + +sub testMe { + my $processor = shift; + my $name = shift; + my $input = shift; + my $expectedValue = shift; + + my $got = &$processor(@$input); + + if ( $got==$expectedValue ) { + print "[OK ] $name\n"; + } + else { + print "[ERR] $name :: got=$got, expectedValue=$expectedValue\n"; + } + +} + +sub process { + my @input = @_; + + my $len = @input; + + my $result = 0; + + my $alreadyProcessedPair = {}; + + for(my $i=0; $i<$len; ++$i) { + + for(my $j=$i; $j<$len; ++$j) { + + my $v1 = $input[$i]; + my $v2 = $input[$j]; + + my $pairName = $v1<$v2 ? "${v1}_$v2" : "${v2}_$v1"; + + next if ( exists $alreadyProcessedPair->{$pairName} ); + + $alreadyProcessedPair->{$pairName} = 1; + + my $modValue = abs($v1-$v2); + + if ( $modValue > 0 ) { + + my $minValue = $v1<$v2 ? $v1 : $v2; + + if ( $modValue < $minValue ) { + ++$result; + } + + } + + } + + } + + return $result; +} + + + |
