diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-02-13 01:09:15 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-02-13 01:09:15 +0000 |
| commit | c3ee94e77ddd1bed45905c1c98a12f0fc2fd2998 (patch) | |
| tree | bf1fb6e7399a8b4e93f2f259db910b67d47e0d90 | |
| parent | c1662f70bbd9d22643bba29df808fe350a5f4a22 (diff) | |
| parent | 4d9a203c75610c807352680e25d9b5d420b712e4 (diff) | |
| download | perlweeklychallenge-club-c3ee94e77ddd1bed45905c1c98a12f0fc2fd2998.tar.gz perlweeklychallenge-club-c3ee94e77ddd1bed45905c1c98a12f0fc2fd2998.tar.bz2 perlweeklychallenge-club-c3ee94e77ddd1bed45905c1c98a12f0fc2fd2998.zip | |
Merge pull request #9571 from pme/challenge-256
challenge-256
| -rwxr-xr-x | challenge-256/peter-meszaros/perl/ch-1.pl | 54 | ||||
| -rwxr-xr-x | challenge-256/peter-meszaros/perl/ch-2.pl | 56 |
2 files changed, 110 insertions, 0 deletions
diff --git a/challenge-256/peter-meszaros/perl/ch-1.pl b/challenge-256/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..2e89e311a4 --- /dev/null +++ b/challenge-256/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,54 @@ +#!/usr/bin/env perl +# +# 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 strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + ["ab", "de", "ed", "bc"], + ["aa", "ba", "cd", "ed"], + ["uv", "qp", "st", "vu", "mn", "pq"], +]; + +sub maximum_pairs +{ + my $pairs = shift; + + my %h = map { $_, 1 } @$pairs; + my $numpairs = 0; + for my $e (@$pairs) { + delete $h{$e}; + ++$numpairs if $h{reverse $e}; + } + return $numpairs; +} + +is(maximum_pairs($cases->[0]), 1, 'Example 1'); +is(maximum_pairs($cases->[1]), 0, 'Example 2'); +is(maximum_pairs($cases->[2]), 2, 'Example 3'); +done_testing(); + +exit 0; diff --git a/challenge-256/peter-meszaros/perl/ch-2.pl b/challenge-256/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..2bd243d1fa --- /dev/null +++ b/challenge-256/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,56 @@ +#!/usr/bin/env perl +# +# 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 strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [ "abcd", "1234"], + [ "abc", "12345"], + [ "abcde", "123"], +]; + +sub merge_strings +{ + my ($str1, $str2) = @_; + + my @arr1 = split //, $str1; + my @arr2 = split //, $str2; + my $len = @arr1 > @arr2 ? @arr1 : @arr2; + + my @res; + for my $i (0..$len) { + push @res, $arr1[$i] // '', $arr2[$i] // ''; + } + return join '', @res; +} + +is(merge_strings($cases->[0]->@*), "a1b2c3d4", 'Example 1'); +is(merge_strings($cases->[1]->@*), "a1b2c345", 'Example 2'); +is(merge_strings($cases->[2]->@*), "a1b2c3de", 'Example 3'); +done_testing(); + +exit 0; + |
