diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2024-02-12 17:27:39 -0500 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2024-02-12 17:27:39 -0500 |
| commit | 2809164660cdda2eef832ff83ce8a553a656f954 (patch) | |
| tree | 9487bcccbf4e9467afcda3a5ee94a69fdca2a67c | |
| parent | 3f3e0798a68401ce1d67a5e1534f69de16856e82 (diff) | |
| download | perlweeklychallenge-club-2809164660cdda2eef832ff83ce8a553a656f954.tar.gz perlweeklychallenge-club-2809164660cdda2eef832ff83ce8a553a656f954.tar.bz2 perlweeklychallenge-club-2809164660cdda2eef832ff83ce8a553a656f954.zip | |
256 DAJ
| -rw-r--r-- | challenge-256/dave-jacoby/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-256/dave-jacoby/perl/ch-1.pl | 31 | ||||
| -rw-r--r-- | challenge-256/dave-jacoby/perl/ch-2.pl | 33 |
3 files changed, 65 insertions, 0 deletions
diff --git a/challenge-256/dave-jacoby/blog.txt b/challenge-256/dave-jacoby/blog.txt new file mode 100644 index 0000000000..af43fabf88 --- /dev/null +++ b/challenge-256/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2024/02/12/a-perfect-square-perfectly-squared-weekly-challenge-256.html diff --git a/challenge-256/dave-jacoby/perl/ch-1.pl b/challenge-256/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..9c0167f853 --- /dev/null +++ b/challenge-256/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,31 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +my @examples = ( + + [ "ab", "de", "ed", "bc" ], + [ "aa", "ba", "cd", "ed" ], + [ "uv", "qp", "st", "vu", "mn", "pq" ], +); + +for my $example (@examples) { + my $input = join ', ', map { qq {"$_"} } $example->@*; + my $output = maximum_pairs( $example->@* ); + + say <<~"END"; + Input: \@words = ($input) + Output: $output + END +} + +sub maximum_pairs (@input) { + my %hash; + map { + my $munge = join '', sort split //, $_; + $hash{$munge}++ + } @input; + return scalar grep { $_ > 1 } values %hash; +} diff --git a/challenge-256/dave-jacoby/perl/ch-2.pl b/challenge-256/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..0ca524d0ce --- /dev/null +++ b/challenge-256/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +my @examples = ( + + [ "abcd", "1234" ], + [ "abc", "12345" ], + [ "abcde", "123" ], +); + +for my $example (@examples) { + my $output = merge_strings( $example->@* ); + my $p = $example->[0]; + my $w = $example->[1]; + + say <<~"END"; + Input: \$str1 = "$p", \$str2 = "$w" + Output: "$output" + END +} + +sub merge_strings ( $str1, $str2 ) { + my $output; + while ( length $str1 && length $str2 ) { + $output .= substr( $str1, 0, 1 ) . substr( $str2, 0, 1 ); + substr( $str1, 0, 1 ) = ''; + substr( $str2, 0, 1 ) = ''; + } + return $output . $str1 . $str2; +} |
