diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2024-08-25 15:25:01 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2024-08-25 15:25:01 +0100 |
| commit | cdd0687ba802f916cb6a5927e542937478acbb9d (patch) | |
| tree | ffc90b0dd22741e3cf4c59caee74c1410199864d | |
| parent | a7834b8a5407e4db6608c73f22ded2db0ca3f746 (diff) | |
| download | perlweeklychallenge-club-cdd0687ba802f916cb6a5927e542937478acbb9d.tar.gz perlweeklychallenge-club-cdd0687ba802f916cb6a5927e542937478acbb9d.tar.bz2 perlweeklychallenge-club-cdd0687ba802f916cb6a5927e542937478acbb9d.zip | |
Add Perl solution to challenge 256
| -rw-r--r-- | challenge-256/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-256/paulo-custodio/perl/ch-1.pl | 39 | ||||
| -rw-r--r-- | challenge-256/paulo-custodio/perl/ch-2.pl | 30 | ||||
| -rw-r--r-- | challenge-256/paulo-custodio/t/test-1.yaml | 15 | ||||
| -rw-r--r-- | challenge-256/paulo-custodio/t/test-2.yaml | 15 |
5 files changed, 101 insertions, 0 deletions
diff --git a/challenge-256/paulo-custodio/Makefile b/challenge-256/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-256/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-256/paulo-custodio/perl/ch-1.pl b/challenge-256/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..d2a0fef0f2 --- /dev/null +++ b/challenge-256/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,39 @@ +#!/usr/bin/env perl + +# Challenge 256 +# +# Task 1: Maximum Pairs +# Submitted by: Mohammad Sajid Anwar +# You are given an array of distinct words, @words. +# +# Write a script to find the maximum pairs in the given array. The words +# $words[i] and $words[j] can be a pair one is reverse of the other. +# +# Example 1 +# Input: @words = ("ab", "de", "ed", "bc") +# Output: 1 +# +# There is one pair in the given array: "de" and "ed" +# Example 2 +# Input: @words = ("aa", "ba", "cd", "ed") +# Output: 0 +# Example 3 +# Input: @words = ("uv", "qp", "st", "vu", "mn", "pq") +# Output: 2 + +use Modern::Perl; + +my @words = @ARGV; +my $count = 0; +for my $i (0 .. $#words-1) { + for my $j ($i+1 .. $#words) { + $count++ if is_pair($words[$i], $words[$j]); + } +} +say $count; + + +sub is_pair { + my($a, $b) = @_; + return $a eq join '', reverse split //, $b; +} diff --git a/challenge-256/paulo-custodio/perl/ch-2.pl b/challenge-256/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..b564539172 --- /dev/null +++ b/challenge-256/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,30 @@ +#!/usr/bin/env perl + +# Challenge 256 +# +# Task 2: Merge Strings +# Submitted by: Mohammad Sajid Anwar +# You are given two strings, $str1 and $str2. +# +# Write a script to merge the given strings by adding in alternative order +# starting with the first string. If a string is longer than the other then +# append the remaining at the end. +# +# Example 1 +# Input: $str1 = "abcd", $str2 = "1234" +# Output: "a1b2c3d4" +# Example 2 +# Input: $str1 = "abc", $str2 = "12345" +# Output: "a1b2c345" +# Example 3 +# Input: $str1 = "abcde", $str2 = "123" +# Output: "a1b2c3de" + +use Modern::Perl; +use List::Util 'zip'; + +my($a, $b) = @ARGV; +my @a = split //, $a; +my @b = split //, $b; +my @merge = map {($_->[0]//'').($_->[1]//'')} zip \@a, \@b; +say join '', @merge; diff --git a/challenge-256/paulo-custodio/t/test-1.yaml b/challenge-256/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..c921aeb3a7 --- /dev/null +++ b/challenge-256/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: ab de ed bc + input: + output: 1 +- setup: + cleanup: + args: aa ba cd ed + input: + output: 0 +- setup: + cleanup: + args: uv qp st vu mn pq + input: + output: 2 diff --git a/challenge-256/paulo-custodio/t/test-2.yaml b/challenge-256/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..8e36fce9c2 --- /dev/null +++ b/challenge-256/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: abcd 1234 + input: + output: a1b2c3d4 +- setup: + cleanup: + args: abc 12345 + input: + output: a1b2c345 +- setup: + cleanup: + args: abcde 123 + input: + output: a1b2c3de |
