aboutsummaryrefslogtreecommitdiff
path: root/challenge-061
diff options
context:
space:
mode:
authorDave Jacoby <jacoby.david@gmail.com>2024-09-18 19:56:42 -0400
committerDave Jacoby <jacoby.david@gmail.com>2024-09-18 19:56:42 -0400
commitf86f5e2fec16020c1d86f9028fb0f61cfeac106e (patch)
tree0fd388a696b51ffde5a7bfe8519a74e1caf42461 /challenge-061
parentff8719c86653d5ad3121955e9494a0010527c2b9 (diff)
parent0052ec63ca70eaa6d9ffb1926c294dbfd85f8c05 (diff)
downloadperlweeklychallenge-club-f86f5e2fec16020c1d86f9028fb0f61cfeac106e.tar.gz
perlweeklychallenge-club-f86f5e2fec16020c1d86f9028fb0f61cfeac106e.tar.bz2
perlweeklychallenge-club-f86f5e2fec16020c1d86f9028fb0f61cfeac106e.zip
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-061')
-rw-r--r--challenge-061/paulo-custodio/perl/ch-1.pl2
-rw-r--r--challenge-061/paulo-custodio/perl/ch-2.pl4
-rw-r--r--challenge-061/paulo-custodio/python/ch-1.py36
-rw-r--r--challenge-061/paulo-custodio/python/ch-2.py46
4 files changed, 85 insertions, 3 deletions
diff --git a/challenge-061/paulo-custodio/perl/ch-1.pl b/challenge-061/paulo-custodio/perl/ch-1.pl
index a8a9e2faaf..fcfcd54dd7 100644
--- a/challenge-061/paulo-custodio/perl/ch-1.pl
+++ b/challenge-061/paulo-custodio/perl/ch-1.pl
@@ -2,7 +2,7 @@
# Challenge 061
#
-# TASK #1 › Product SubArray
+# 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;
diff --git a/challenge-061/paulo-custodio/perl/ch-2.pl b/challenge-061/paulo-custodio/perl/ch-2.pl
index f7a8e53653..5a2801f33b 100644
--- a/challenge-061/paulo-custodio/perl/ch-2.pl
+++ b/challenge-061/paulo-custodio/perl/ch-2.pl
@@ -2,7 +2,7 @@
# Challenge 061
#
-# TASK #2 › IPv4 Partition
+# 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.
@@ -11,7 +11,7 @@
# 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 (.).
+# "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.)
diff --git a/challenge-061/paulo-custodio/python/ch-1.py b/challenge-061/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..47e232e6aa
--- /dev/null
+++ b/challenge-061/paulo-custodio/python/ch-1.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env perl
+
+# Challenge 061
+#
+# 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.
+
+import sys
+
+def product(nums):
+ prod = 1
+ for x in nums:
+ prod *= x
+ return x
+
+
+n = list(map(int, sys.argv[1:]))
+max_sublist = []
+max_prod = int(-1e6)
+
+for i in range(0, len(n)):
+ for j in range(i, len(n)):
+ sublist = n[i:j+1]
+ prod = product(sublist)
+ if prod > max_prod:
+ max_sublist, max_prod = sublist, prod
+
+print(", ".join(list(map(str, max_sublist))))
diff --git a/challenge-061/paulo-custodio/python/ch-2.py b/challenge-061/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..0cbd7cd9c9
--- /dev/null
+++ b/challenge-061/paulo-custodio/python/ch-2.py
@@ -0,0 +1,46 @@
+#!/usr/bin/env python3
+
+# Challenge 061
+#
+# 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
+
+import re
+import sys
+
+def partition1(prefix, digits):
+ if re.search(r'^(\d+\.){4}$', prefix):
+ if digits == "":
+ prefix = prefix[:-1]
+ print(prefix)
+ else:
+ for l in range(1, 4):
+ if l <= len(digits):
+ part = digits[:l]
+ if int(part) <= 255:
+ partition1(prefix+part+".", digits[l:])
+
+def partition(digits):
+ partition1("", digits)
+
+digits = sys.argv[1]
+partition(digits)