diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-02-13 01:01:54 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-02-13 01:01:54 +0000 |
| commit | 38e27dd8b3d10f4669dd88c2b2a32bae28ee91ed (patch) | |
| tree | 6e0558163f2f408b789a81b4bdd6dcf6f2102c2f /challenge-256 | |
| parent | 4d05c81145f47c8f0f0da13b94eced07c9adf931 (diff) | |
| parent | 84f04c10ff60be25a038042b74780cff9cfb3c0a (diff) | |
| download | perlweeklychallenge-club-38e27dd8b3d10f4669dd88c2b2a32bae28ee91ed.tar.gz perlweeklychallenge-club-38e27dd8b3d10f4669dd88c2b2a32bae28ee91ed.tar.bz2 perlweeklychallenge-club-38e27dd8b3d10f4669dd88c2b2a32bae28ee91ed.zip | |
Merge pull request #9566 from choroba/ech256
Add solutions to 256: Maximum Pairs & Merge Strings by E. Choroba
Diffstat (limited to 'challenge-256')
| -rwxr-xr-x | challenge-256/e-choroba/perl/ch-1.pl | 22 | ||||
| -rwxr-xr-x | challenge-256/e-choroba/perl/ch-2.pl | 20 |
2 files changed, 42 insertions, 0 deletions
diff --git a/challenge-256/e-choroba/perl/ch-1.pl b/challenge-256/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..416f9771ac --- /dev/null +++ b/challenge-256/e-choroba/perl/ch-1.pl @@ -0,0 +1,22 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub maximum_pairs(@words) { + my $pair_tally = 0; + my %seen; + for my $word (@words) { + # By checking before booking we don't have to special case + # palindromes. + ++$pair_tally if exists $seen{ reverse $word }; + undef $seen{$word}; + } + return $pair_tally +} + +use Test::More tests => 3; + +is maximum_pairs('ab', 'de', 'ed', 'bc'), 1, 'Example 1'; +is maximum_pairs('aa', 'ba', 'cd', 'ed'), 0, 'Example 2'; +is maximum_pairs('uv', 'qp', 'st', 'vu', 'mn', 'pq'), 2, 'Example 3'; diff --git a/challenge-256/e-choroba/perl/ch-2.pl b/challenge-256/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..36461f8f21 --- /dev/null +++ b/challenge-256/e-choroba/perl/ch-2.pl @@ -0,0 +1,20 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( bitwise signatures ); + +sub merge_strings($str1, $str2) { + my $s = ""; + for my $i (0 .. length($str1 |. $str2)) { + no warnings 'substr'; + $s .= (substr($_, $i, 1) // "") for $str1, $str2; + } + return $s +} + +use Test::More tests => 3 + 1; + +is merge_strings('abcd', '1234'), 'a1b2c3d4', 'Example 1'; +is merge_strings('abc', '12345'), 'a1b2c345', 'Example 2'; +is merge_strings('abcde', '123'), 'a1b2c3de', 'Example 3'; +is merge_strings("", ""), "", 'Empty'; |
