aboutsummaryrefslogtreecommitdiff
path: root/challenge-061/lubos-kolouch/python
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-061/lubos-kolouch/python')
-rw-r--r--challenge-061/lubos-kolouch/python/ch-1.py26
-rw-r--r--challenge-061/lubos-kolouch/python/ch-2.py30
2 files changed, 56 insertions, 0 deletions
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)