aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-05-19 02:07:27 +0100
committerGitHub <noreply@github.com>2020-05-19 02:07:27 +0100
commita4c8ef6e093f0a192f6196b5bb747ba8cb78ce5d (patch)
treed69ecf1b05cb04206cef0adf0566293bac3b89de
parent4e132800212e13c2ea547262129e8e19e2ea1c35 (diff)
parent275405106754628b8fb68f10603821ea8d50737d (diff)
downloadperlweeklychallenge-club-a4c8ef6e093f0a192f6196b5bb747ba8cb78ce5d.tar.gz
perlweeklychallenge-club-a4c8ef6e093f0a192f6196b5bb747ba8cb78ce5d.tar.bz2
perlweeklychallenge-club-a4c8ef6e093f0a192f6196b5bb747ba8cb78ce5d.zip
Merge pull request #1736 from choroba/ech-061
Solve 061 by E. Choroba
-rwxr-xr-xchallenge-061/e-choroba/perl/ch-1.pl29
-rwxr-xr-xchallenge-061/e-choroba/perl/ch-2.pl42
2 files changed, 71 insertions, 0 deletions
diff --git a/challenge-061/e-choroba/perl/ch-1.pl b/challenge-061/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..9ee0447f3f
--- /dev/null
+++ b/challenge-061/e-choroba/perl/ch-1.pl
@@ -0,0 +1,29 @@
+#! /usr/bin/perl
+use warnings;
+use strict;
+
+# use List::Util qw{ product };
+
+sub product {
+ my @list = @_;
+ my $p = 1;
+ $p *= $_ for @list;
+ return $p
+}
+
+sub max_prod {
+ my ($list) = @_;
+ my $max = $list->[0];
+ for my $i (0 .. $#$list) {
+ for my $j ($i .. $#$list) {
+ my $p = product(@$list[$i .. $j]);
+ $max = $p if $p > $max;
+ }
+ }
+ return $max
+}
+
+use Test::More tests => 1;
+is max_prod([2, 5, -1, 3]), 10;
+
+
diff --git a/challenge-061/e-choroba/perl/ch-2.pl b/challenge-061/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..f0460389d2
--- /dev/null
+++ b/challenge-061/e-choroba/perl/ch-2.pl
@@ -0,0 +1,42 @@
+#! /usr/bin/perl
+use warnings;
+use strict;
+
+sub _partition {
+ my ($string) = @_;
+ my @p;
+ for my $pos (1 .. 3) {
+ next if $pos > length $string;
+
+ my $prefix = substr $string, 0, $pos;
+ next if $prefix > 255 || (
+ 1 < length $prefix
+ && 0 == index $prefix, '0');
+
+ if ($pos == length $string) {
+ push @p, [$prefix];
+ } else {
+ push @p, grep @$_ <= 4,
+ map [$prefix, @$_],
+ _partition(substr $string, $pos);
+ }
+ }
+ return @p
+}
+
+sub partition {
+ my ($string) = @_;
+ [ map { join '.', @$_ } grep 4 == @$_, _partition($string) ]
+}
+
+use Test::More;
+use Test::Deep;
+
+cmp_deeply partition('25525511135'),
+ bag(qw( 255.255.11.135 255.255.111.35 ));
+
+cmp_deeply partition('1234'), bag(qw( 1.2.3.4 ));
+cmp_deeply partition('123405'),
+ bag(qw( 1.23.40.5 1.234.0.5 12.3.40.5 12.34.0.5 123.4.0.5 ));
+
+done_testing();