diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-08-15 11:21:12 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-15 11:21:12 +0100 |
| commit | 65f90be5ecb22a4c737add99484643cd31438882 (patch) | |
| tree | 38ad3d83a16cac322ab4525190e0eee0580e5b23 | |
| parent | 520e1f1a9b3e9de905b07333dc4aeceab4fc19c6 (diff) | |
| parent | 03d3d64790298ccfbcc165ae4ddd92447eac5a5a (diff) | |
| download | perlweeklychallenge-club-65f90be5ecb22a4c737add99484643cd31438882.tar.gz perlweeklychallenge-club-65f90be5ecb22a4c737add99484643cd31438882.tar.bz2 perlweeklychallenge-club-65f90be5ecb22a4c737add99484643cd31438882.zip | |
Merge pull request #10622 from pauloscustodio/master
Add Perl solution to challenge 277
| -rw-r--r-- | challenge-277/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-277/paulo-custodio/perl/ch-1.pl | 43 | ||||
| -rw-r--r-- | challenge-277/paulo-custodio/perl/ch-2.pl | 40 | ||||
| -rw-r--r-- | challenge-277/paulo-custodio/t/test-1.yaml | 15 | ||||
| -rw-r--r-- | challenge-277/paulo-custodio/t/test-2.yaml | 10 |
5 files changed, 110 insertions, 0 deletions
diff --git a/challenge-277/paulo-custodio/Makefile b/challenge-277/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-277/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-277/paulo-custodio/perl/ch-1.pl b/challenge-277/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..d2bc267287 --- /dev/null +++ b/challenge-277/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,43 @@ +#!/usr/bin/env perl + +# Challenge 277 +# +# Task 1: Count Common +# Submitted by: Mohammad Sajid Anwar +# 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 + +use Modern::Perl; + +my $words = "@ARGV"; +my @words = split /,/, $words; + +my $count = []; +for my $i (0..1) { + for my $word (split ' ', $words[$i]) { + $count->[$i]{$word}++; + } +} + +my $count_same = 0; +for my $word (split ' ', @words[0..1]) { + $count_same++ if ($count->[0]{$word}//0)==1 && ($count->[1]{$word}//0)==1; +} + +say $count_same; diff --git a/challenge-277/paulo-custodio/perl/ch-2.pl b/challenge-277/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..da33b27d94 --- /dev/null +++ b/challenge-277/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,40 @@ +#!/usr/bin/env perl + +# Challenge 277 +# +# Task 2: Strong Pair +# Submitted by: Mohammad Sajid Anwar +# 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) + +use Modern::Perl; +use List::Util 'min', 'uniq'; + +my @ints = uniq @ARGV; +my $count = 0; +for my $i (0..$#ints-1) { + for my $j ($i+1..$#ints) { + $count++ if is_strong_pair($ints[$i], $ints[$j]); + } +} + +say $count; + +sub is_strong_pair { + my($a, $b) = @_; + return 0 < abs($a-$b) && abs($a-$b) < min($a,$b); +} diff --git a/challenge-277/paulo-custodio/t/test-1.yaml b/challenge-277/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..03040ce356 --- /dev/null +++ b/challenge-277/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: Perl is my friend,Perl and Raku are friend + input: + output: 2 +- setup: + cleanup: + args: Perl and Python are very similar,Python is top in guest languages + input: + output: 1 +- setup: + cleanup: + args: Perl is imperative Lisp is functional,Crystal is similar to Ruby + input: + output: 0 diff --git a/challenge-277/paulo-custodio/t/test-2.yaml b/challenge-277/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..6b7d7a47dd --- /dev/null +++ b/challenge-277/paulo-custodio/t/test-2.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 1 2 3 4 5 + input: + output: 4 +- setup: + cleanup: + args: 5 7 1 7 + input: + output: 1 |
