From 42583b076195a2114e9635352d77898bf8345ba2 Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Mon, 12 Oct 2020 23:54:55 +0200 Subject: Add solutions to 082 by E. Choroba --- challenge-082/e-choroba/perl5/ch-1.pl | 23 +++++++++++++++++++ challenge-082/e-choroba/perl5/ch-2.pl | 42 +++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100755 challenge-082/e-choroba/perl5/ch-1.pl create mode 100755 challenge-082/e-choroba/perl5/ch-2.pl diff --git a/challenge-082/e-choroba/perl5/ch-1.pl b/challenge-082/e-choroba/perl5/ch-1.pl new file mode 100755 index 0000000000..bac0ff5500 --- /dev/null +++ b/challenge-082/e-choroba/perl5/ch-1.pl @@ -0,0 +1,23 @@ +#!/usr/bin/perl +use warnings; +use strict; + +sub common_factors { + my ($m, $n) = @_; + ($m, $n) = ($n, $m) if $n < $m; + my @r; + for my $i (1 .. $m) { + push @r, $i if 0 == $m % $i + && 0 == $n % $i; + } + return \@r +} + +use Test::More; + +is_deeply common_factors(12, 18), [1, 2, 3, 6], 'Example 1'; +is_deeply common_factors(18, 23), [1], 'Example 2'; +is_deeply common_factors(123_456_789, 987_654_321), [1, 3, 9], 'large numbers'; + +done_testing(); + diff --git a/challenge-082/e-choroba/perl5/ch-2.pl b/challenge-082/e-choroba/perl5/ch-2.pl new file mode 100755 index 0000000000..e61bbe3dd4 --- /dev/null +++ b/challenge-082/e-choroba/perl5/ch-2.pl @@ -0,0 +1,42 @@ +#!/usr/bin/perl +use warnings; +use strict; + +sub interleave_string { + my ($x, $y, $z) = @_; + return 0 if length($z) != length($x) + length($y); + + return 1 if "" eq $z; + + my ($first_x, $first_y, $first_z) = map { substr $_, 0, 1 } $x, $y, $z; + + my $maybe_x = $first_x eq $first_z; + my $maybe_y = $first_y eq $first_z; + + my $rest_x = $maybe_x ? substr $x, 1 : ""; + my $rest_y = $maybe_y ? substr $y, 1 : ""; + my $rest_z = substr $z, 1; + + if ($maybe_x && $maybe_y) { + return interleave_string($rest_x, $y, $rest_z) + || interleave_string($x, $rest_y, $rest_z) + } + + return interleave_string($rest_x, $y, $rest_z) if $maybe_x; + + return interleave_string($x, $rest_y, $rest_z) if $maybe_y; + + return 0 +} + +use Test::More; + +is interleave_string('XY', 'X', 'XXY'), 1, 'Example 1'; +is interleave_string('XXY', 'XXZ', 'XXXXZY'), 1, 'Example 2'; +is interleave_string('YX', 'X', 'XXY'), 0, 'Example 3'; + +is interleave_string('ABC', 'BD', $_), 1, $_ + for qw( ABDBC ABCBD ABBCD ABBDC BABCD BABDC BADBC BDABC ); + + +done_testing(); -- cgit