aboutsummaryrefslogtreecommitdiff
path: root/challenge-061
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2022-04-14 15:30:39 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2022-04-14 15:30:39 +0100
commit39a7fcb6379132cefe84c40a8f01829263b80f68 (patch)
treec7c78403f89de295cf9dc7e837051ba141f4a776 /challenge-061
parent8831c5149d8584bb38fd6c8df46e884d26e99abe (diff)
downloadperlweeklychallenge-club-39a7fcb6379132cefe84c40a8f01829263b80f68.tar.gz
perlweeklychallenge-club-39a7fcb6379132cefe84c40a8f01829263b80f68.tar.bz2
perlweeklychallenge-club-39a7fcb6379132cefe84c40a8f01829263b80f68.zip
Add Perl solution to challenge 061
Diffstat (limited to 'challenge-061')
-rw-r--r--challenge-061/paulo-custodio/Makefile2
-rw-r--r--challenge-061/paulo-custodio/README1
-rw-r--r--challenge-061/paulo-custodio/perl/ch-1.pl33
-rw-r--r--challenge-061/paulo-custodio/perl/ch-2.pl47
-rw-r--r--challenge-061/paulo-custodio/t/test-1.yaml5
-rw-r--r--challenge-061/paulo-custodio/t/test-2.yaml7
6 files changed, 95 insertions, 0 deletions
diff --git a/challenge-061/paulo-custodio/Makefile b/challenge-061/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-061/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-061/paulo-custodio/README b/challenge-061/paulo-custodio/README
new file mode 100644
index 0000000000..87dc0b2fbd
--- /dev/null
+++ b/challenge-061/paulo-custodio/README
@@ -0,0 +1 @@
+Solution by Paulo Custodio
diff --git a/challenge-061/paulo-custodio/perl/ch-1.pl b/challenge-061/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..77ef59545a
--- /dev/null
+++ b/challenge-061/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,33 @@
+#!/usr/bin/env perl
+
+# Challenge 060
+#
+# TASK #1 › Product SubArray
+# Reviewed by: Ryan Thompson
+# Given a list of 4 or more numbers, write a script to find the contiguous
+# sublist that has the maximum product. The length of the sublist is irrelevant;
+# your job is to maximize the product.
+#
+# Example
+# Input: [ 2, 5, -1, 3 ]
+#
+# Output: [ 2, 5 ] which gives maximum product 10.
+
+use Modern::Perl;
+use List::Util qw( product );
+
+my @n = @ARGV;
+my @max_sublist;
+my $max_product;
+
+for my $i (0 .. $#n) {
+ for my $j ($i .. $#n) {
+ my @sublist = @n[$i .. $j];
+ my $product = product(@sublist);
+ if (!defined($max_product) || $product > $max_product) {
+ $max_product = $product;
+ @max_sublist = @sublist;
+ }
+ }
+}
+say join(", ", @max_sublist);
diff --git a/challenge-061/paulo-custodio/perl/ch-2.pl b/challenge-061/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..ba38ef766b
--- /dev/null
+++ b/challenge-061/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,47 @@
+#!/usr/bin/env perl
+
+# Challenge 060
+#
+# TASK #2 › IPv4 Partition
+# Reviewed by: Ryan Thompson
+# You are given a string containing only digits (0..9). The string should have
+# between 4 and 12 digits.
+#
+# Write a script to print every possible valid IPv4 address that can be made by
+# partitioning the input string.
+#
+# For the purpose of this challenge, a valid IPv4 address consists of four
+# “octets” i.e. A, B, C and D, separated by dots (.).
+#
+# Each octet must be between 0 and 255, and must not have any leading zeroes.
+# (e.g., 0 is OK, but 01 is not.)
+#
+# Example
+# Input: 25525511135,
+#
+# Output:
+#
+# 255.255.11.135
+# 255.255.111.35
+
+use Modern::Perl;
+
+my $digits = shift;
+partition("", $digits);
+
+
+sub partition {
+ my($prefix, $digits) = @_;
+ if ($prefix =~ /^(\d+\.){4}$/ && $digits eq '') {
+ $prefix =~ s/\.$//;
+ say $prefix;
+ }
+ else {
+ for my $len (1..3) {
+ next if $len > length($digits);
+ my $part = substr($digits, 0, $len);
+ next if $part > 255;
+ partition($prefix.$part.".", substr($digits, $len));
+ }
+ }
+}
diff --git a/challenge-061/paulo-custodio/t/test-1.yaml b/challenge-061/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..09753f7b2d
--- /dev/null
+++ b/challenge-061/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,5 @@
+- setup:
+ cleanup:
+ args: 2 5 -1 3
+ input:
+ output: 2, 5
diff --git a/challenge-061/paulo-custodio/t/test-2.yaml b/challenge-061/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..e96e21c02b
--- /dev/null
+++ b/challenge-061/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,7 @@
+- setup:
+ cleanup:
+ args: 25525511135
+ input:
+ output: |
+ |255.255.11.135
+ |255.255.111.35