diff options
| -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'; |
