aboutsummaryrefslogtreecommitdiff
path: root/challenge-061
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2023-05-05 10:37:19 +0200
committerLubos Kolouch <lubos@kolouch.net>2023-05-05 10:37:19 +0200
commit08273fd32230463ff6eb2f5ffea7f8c8f9cee4c2 (patch)
treef5cd977c1e2a28ae30eb3ac0ccbf927ca22915d6 /challenge-061
parent30c20702df85ea5c5c9470634fedbeb954360884 (diff)
downloadperlweeklychallenge-club-08273fd32230463ff6eb2f5ffea7f8c8f9cee4c2.tar.gz
perlweeklychallenge-club-08273fd32230463ff6eb2f5ffea7f8c8f9cee4c2.tar.bz2
perlweeklychallenge-club-08273fd32230463ff6eb2f5ffea7f8c8f9cee4c2.zip
Challenges 061 062 063 LK Perl Python
Diffstat (limited to 'challenge-061')
-rw-r--r--challenge-061/lubos-kolouch/perl/ch-1.pl28
-rw-r--r--challenge-061/lubos-kolouch/perl/ch-2.pl37
-rw-r--r--challenge-061/lubos-kolouch/python/ch-1.py26
-rw-r--r--challenge-061/lubos-kolouch/python/ch-2.py30
4 files changed, 121 insertions, 0 deletions
diff --git a/challenge-061/lubos-kolouch/perl/ch-1.pl b/challenge-061/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..cadd325c15
--- /dev/null
+++ b/challenge-061/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,28 @@
+use strict;
+use warnings;
+use List::Util qw/min max/;
+
+sub max_product_sublist {
+ my @nums = @_;
+ my $max_product = $nums[0];
+ my $min_product = $nums[0];
+ my $result = $nums[0];
+
+ for my $i (1 .. $#nums) {
+ if ($nums[$i] < 0) {
+ ($max_product, $min_product) = ($min_product, $max_product);
+ }
+
+ $max_product = max($nums[$i], $nums[$i] * $max_product);
+ $min_product = min($nums[$i], $nums[$i] * $min_product);
+
+ $result = max($result, $max_product);
+ }
+
+ return $result;
+}
+
+my @input = (2, 5, -1, 3);
+my $max_product = max_product_sublist(@input);
+print "Maximum product: $max_product\n"; # Output: Maximum product: 10
+
diff --git a/challenge-061/lubos-kolouch/perl/ch-2.pl b/challenge-061/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..4cef93f7c2
--- /dev/null
+++ b/challenge-061/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,37 @@
+use strict;
+use warnings;
+
+sub is_valid_octet {
+ my $s = shift;
+ return 0 <= $s && $s <= 255 && $s == int($s);
+}
+
+sub restore_ip_addresses {
+ my $s = shift;
+ my @result;
+ my $n = length($s);
+
+ for my $i (1 .. 3) {
+ for my $j ($i + 1 .. $i + 3) {
+ for my $k ($j + 1 .. $j + 3) {
+ if ($k < $n) {
+ my $s1 = substr($s, 0, $i);
+ my $s2 = substr($s, $i, $j - $i);
+ my $s3 = substr($s, $j, $k - $j);
+ my $s4 = substr($s, $k);
+ if (is_valid_octet($s1) && is_valid_octet($s2) && is_valid_octet($s3) && is_valid_octet($s4)) {
+ push @result, "$s1.$s2.$s3.$s4";
+ }
+ }
+ }
+ }
+ }
+ return @result;
+}
+
+my $input_str = "25525511135";
+my @ipv4_addresses = restore_ip_addresses($input_str);
+for my $address (@ipv4_addresses) {
+ print "$address\n";
+}
+
diff --git a/challenge-061/lubos-kolouch/python/ch-1.py b/challenge-061/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..ead27c91bf
--- /dev/null
+++ b/challenge-061/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,26 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from typing import List
+
+
+def max_product_sublist(nums: List[int]) -> int:
+ max_product = nums[0]
+ min_product = nums[0]
+ result = nums[0]
+
+ for i in range(1, len(nums)):
+ if nums[i] < 0:
+ max_product, min_product = min_product, max_product
+
+ max_product = max(nums[i], nums[i] * max_product)
+ min_product = min(nums[i], nums[i] * min_product)
+
+ result = max(result, max_product)
+
+ return result
+
+
+input_nums = [2, 5, -1, 3]
+max_product = max_product_sublist(input_nums)
+print(f"Maximum product: {max_product}") # Output: Maximum product: 10
diff --git a/challenge-061/lubos-kolouch/python/ch-2.py b/challenge-061/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..c14c08758d
--- /dev/null
+++ b/challenge-061/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,30 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from typing import List
+
+
+def is_valid_octet(s: str) -> bool:
+ return 0 <= int(s) <= 255 and str(int(s)) == s
+
+
+def restore_ip_addresses(s: str) -> List[str]:
+ result = []
+ n = len(s)
+
+ for i in range(1, 4):
+ for j in range(i + 1, i + 4):
+ for k in range(j + 1, j + 4):
+ if k < n:
+ s1, s2, s3, s4 = s[:i], s[i:j], s[j:k], s[k:]
+ if is_valid_octet(s1) and is_valid_octet(
+ s2) and is_valid_octet(s3) and is_valid_octet(s4):
+ result.append(f"{s1}.{s2}.{s3}.{s4}")
+
+ return result
+
+
+input_str = "25525511135"
+ipv4_addresses = restore_ip_addresses(input_str)
+for address in ipv4_addresses:
+ print(address)