diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2024-08-19 15:56:54 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2024-08-19 15:56:54 +0100 |
| commit | e05fac705999006a4443829cb18acca756add415 (patch) | |
| tree | f6b9b536b3ab6d300af91b6f4bfc2b03454585a3 | |
| parent | b10ba796259add666f09f0b9ad886a495b162c0e (diff) | |
| download | perlweeklychallenge-club-e05fac705999006a4443829cb18acca756add415.tar.gz perlweeklychallenge-club-e05fac705999006a4443829cb18acca756add415.tar.bz2 perlweeklychallenge-club-e05fac705999006a4443829cb18acca756add415.zip | |
Add Perl solution to challenge 229
| -rw-r--r-- | challenge-229/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-229/paulo-custodio/perl/ch-1.pl | 37 | ||||
| -rw-r--r-- | challenge-229/paulo-custodio/perl/ch-2.pl | 40 | ||||
| -rw-r--r-- | challenge-229/paulo-custodio/t/test-1.yaml | 10 | ||||
| -rw-r--r-- | challenge-229/paulo-custodio/t/test-2.yaml | 10 |
5 files changed, 99 insertions, 0 deletions
diff --git a/challenge-229/paulo-custodio/Makefile b/challenge-229/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-229/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-229/paulo-custodio/perl/ch-1.pl b/challenge-229/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..e85cb9ceb3 --- /dev/null +++ b/challenge-229/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,37 @@ +#!/usr/bin/env perl + +# Challenge 229 +# +# Task 1: Lexicographic Order +# Submitted by: Mohammad S Anwar +# +# You are given an array of strings. +# +# Write a script to delete element which is not lexicographically sorted +# (forwards or backwards) and return the count of deletions. +# Example 1 +# +# Input: @str = ("abc", "bce", "cae") +# Output: 1 +# +# In the given array "cae" is the only element which is not lexicographically sorted. +# +# Example 2 +# +# Input: @str = ("yxz", "cba", "mon") +# Output: 2 +# +# In the given array "yxz" and "mon" are not lexicographically sorted. + +use Modern::Perl; + +my @words = @ARGV; +my $count = scalar grep {!is_sorted($_)} @words; +say $count; + +sub is_sorted { + my($str) = @_; + my $forwards = join '', sort split //, $str; + my $backwards = join '', reverse sort split //, $str; + return $str eq $forwards || $str eq $backwards; +} diff --git a/challenge-229/paulo-custodio/perl/ch-2.pl b/challenge-229/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..cd44877653 --- /dev/null +++ b/challenge-229/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,40 @@ +#!/usr/bin/env perl + +# Challenge 229 +# +# Task 2: Two out of Three +# Submitted by: Mohammad S Anwar +# +# You are given three array of integers. +# +# Write a script to return all the elements that are present in at least 2 out +# of 3 given arrays. +# Example 1 +# +# Input: @array1 = (1, 1, 2, 4) +# @array2 = (2, 4) +# @array3 = (4) +# Ouput: (2, 4) +# +# Example 2 +# +# Input: @array1 = (4, 1) +# @array2 = (2, 4) +# @array3 = (1, 2) +# Ouput: (1, 2, 4) + +use Modern::Perl; +use List::Util 'uniq'; + +my @arrays = map {[uniq split ' ', $_]} split /,/, "@ARGV"; +my %count; +for (@arrays) { + for (@$_) { + $count{$_}++; + } +} +say join ' ', + map {$_->[0]} + grep {$_->[1] >= 2} + map {[$_, $count{$_}]} + sort {$a <=> $b} keys %count; diff --git a/challenge-229/paulo-custodio/t/test-1.yaml b/challenge-229/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..6b90fc348b --- /dev/null +++ b/challenge-229/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: abc bce cae + input: + output: 1 +- setup: + cleanup: + args: yxz cba mon + input: + output: 2 diff --git a/challenge-229/paulo-custodio/t/test-2.yaml b/challenge-229/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..cfe4751d47 --- /dev/null +++ b/challenge-229/paulo-custodio/t/test-2.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 1 1 2 4 , 2 4 , 4 + input: + output: 2 4 +- setup: + cleanup: + args: 4 1 , 2 4 , 1 2 + input: + output: 1 2 4 |
