aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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
-rw-r--r--challenge-062/lubos-kolouch/perl/ch-1.pl30
-rw-r--r--challenge-062/lubos-kolouch/perl/ch-2.pl81
-rw-r--r--challenge-062/lubos-kolouch/python/ch-1.py37
-rw-r--r--challenge-062/lubos-kolouch/python/ch-2.py61
-rw-r--r--challenge-063/lubos-kolouch/perl/ch-1.pl20
-rw-r--r--challenge-063/lubos-kolouch/perl/ch-2.pl21
-rw-r--r--challenge-063/lubos-kolouch/python/ch-1.py39
-rw-r--r--challenge-063/lubos-kolouch/python/ch-2.py21
12 files changed, 431 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)
diff --git a/challenge-062/lubos-kolouch/perl/ch-1.pl b/challenge-062/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..6b98986301
--- /dev/null
+++ b/challenge-062/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,30 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use Getopt::Long;
+
+my $unique = 0;
+GetOptions('u' => \$unique);
+
+my %seen;
+my @emails;
+
+while (my $email = <>) {
+ chomp $email;
+ next if $unique && $seen{$email}++;
+ push @emails, $email;
+}
+
+sub email_sort {
+ my ($mailbox_a, $domain_a) = split '@', $a;
+ my ($mailbox_b, $domain_b) = split '@', $b;
+
+ lc($domain_a) cmp lc($domain_b) || $mailbox_a cmp $mailbox_b;
+}
+
+my @sorted_emails = sort email_sort @emails;
+
+for my $email (@sorted_emails) {
+ print "$email\n";
+}
+
diff --git a/challenge-062/lubos-kolouch/perl/ch-2.pl b/challenge-062/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..9e9b48b29e
--- /dev/null
+++ b/challenge-062/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,81 @@
+use strict;
+use warnings;
+
+sub is_safe {
+ my ($cube, $x, $y, $z, $n) = @_;
+
+ # Check row and column
+ for my $i (0 .. $n-1) {
+ return 0 if $cube->[$x][$i][$z] || $cube->[$i][$y][$z];
+ }
+
+ # Check all layers
+ for my $i (0 .. $n-1) {
+ return 0 if $cube->[$x][$y][$i];
+ }
+
+ # Check diagonals
+ for my $i (0 .. $n-1) {
+ for my $j (0 .. $n-1) {
+ for my $k (0 .. $n-1) {
+ if (($i + $j == $x + $y) || ($i + $k == $x + $z) || ($j + $k == $y + $z) ||
+ ($i - $j == $x - $y) || ($i - $k == $x - $z) || ($j - $k == $y - $z)) {
+ return 0 if $cube->[$i][$j][$k];
+ }
+ }
+ }
+ }
+
+ return 1;
+}
+
+sub solve_n_queens_3d {
+ my ($cube, $col, $n) = @_;
+
+ return 1 if $col >= $n;
+
+ for my $i (0 .. $n-1) {
+ for my $j (0 .. $n-1) {
+ if (is_safe($cube, $i, $j, $col, $n)) {
+ $cube->[$i][$j][$col] = 1;
+ return 1 if solve_n_queens_3d($cube, $col + 1, $n);
+ $cube->[$i][$j][$col] = 0;
+ }
+ }
+ }
+
+ return 0;
+}
+
+sub n_queens_3d {
+ my ($n) = @_;
+ my $cube = [];
+ for my $i (0 .. $n-1) {
+ for my $j (0 .. $n-1) {
+ for my $k (0 .. $n-1) {
+ $cube->[$i][$j][$k] = 0;
+ }
+ }
+ }
+
+ if (!solve_n_queens_3d($cube, 0, $n)) {
+ print "No solution exists\n";
+ return;
+ }
+
+ return $cube;
+}
+
+my $n = 2;
+my $solution = n_queens_3d($n);
+
+if ($solution) {
+ for my $layer (@$solution) {
+ print "Layer:\n";
+ for my $row (@$layer) {
+ print join(" ", @$row), "\n";
+ }
+ print "\n";
+ }
+}
+
diff --git a/challenge-062/lubos-kolouch/python/ch-1.py b/challenge-062/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..eec451cd69
--- /dev/null
+++ b/challenge-062/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,37 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import sys
+import argparse
+from typing import List
+
+
+def email_sort_key(email: str) -> tuple:
+ mailbox, domain = email.split('@')
+ return (domain.lower(), mailbox)
+
+
+def main(emails: List[str], unique: bool) -> None:
+ if unique:
+ emails = list(set(emails))
+
+ sorted_emails = sorted(emails, key=email_sort_key)
+
+ for email in sorted_emails:
+ print(email)
+
+
+if __name__ == "__main__":
+ parser = argparse.ArgumentParser()
+ parser.add_argument('-u',
+ '--unique',
+ action='store_true',
+ help='Only include unique email addresses')
+ parser.add_argument('files',
+ nargs='*',
+ type=argparse.FileType('r'),
+ default=[sys.stdin])
+ args = parser.parse_args()
+
+ all_emails = [line.strip() for file in args.files for line in file]
+ main(all_emails, args.unique)
diff --git a/challenge-062/lubos-kolouch/python/ch-2.py b/challenge-062/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..6cc388eaf5
--- /dev/null
+++ b/challenge-062/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,61 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+
+def is_safe(cube, x, y, z, n):
+ # Check row and column
+ for i in range(n):
+ if cube[x][i][z] or cube[i][y][z]:
+ return False
+
+ # Check all layers
+ for i in range(n):
+ if cube[x][y][i]:
+ return False
+
+ # Check diagonals
+ for i in range(n):
+ for j in range(n):
+ for k in range(n):
+ if (i + j == x + y) or (i + k == x + z) or (j + k == y + z) or \
+ (i - j == x - y) or (i - k == x - z) or (j - k == y - z):
+ if cube[i][j][k]:
+ return False
+
+ return True
+
+
+def solve_n_queens_3d(cube, col, n):
+ if col >= n:
+ return True
+
+ for i in range(n):
+ for j in range(n):
+ if is_safe(cube, i, j, col, n):
+ cube[i][j][col] = 1
+ if solve_n_queens_3d(cube, col + 1, n):
+ return True
+ cube[i][j][col] = 0
+
+ return False
+
+
+def n_queens_3d(n):
+ cube = [[[0 for _ in range(n)] for _ in range(n)] for _ in range(n)]
+
+ if not solve_n_queens_3d(cube, 0, n):
+ print("No solution exists")
+ return None
+
+ return cube
+
+
+if __name__ == "__main__":
+ n = 2
+ solution = n_queens_3d(n)
+ if solution:
+ for layer in solution:
+ print("Layer:")
+ for row in layer:
+ print(row)
+ print()
diff --git a/challenge-063/lubos-kolouch/perl/ch-1.pl b/challenge-063/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..7adf26d5ae
--- /dev/null
+++ b/challenge-063/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,20 @@
+use strict;
+use warnings;
+
+sub last_word {
+ my ($string, $regexp) = @_;
+ my @words = $string =~ /(\S+)/g;
+ my $last_word;
+
+ for my $word (@words) {
+ $last_word = $word if $word =~ $regexp;
+ }
+
+ return $last_word;
+}
+
+print last_word(' hello world', qr/[ea]l/) || "undef", "\n"; # 'hello'
+print last_word("Don't match too much, Chet!", qr/ch.t/i) || "undef", "\n"; # 'Chet!'
+print last_word("spaces in regexp won't match", qr/in re/) || "undef", "\n"; # undef
+print last_word( join(' ', 1..1e6), qr/^(3.*?){3}/) || "undef", "\n"; # '399933'
+
diff --git a/challenge-063/lubos-kolouch/perl/ch-2.pl b/challenge-063/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..c70019eefe
--- /dev/null
+++ b/challenge-063/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,21 @@
+use strict;
+use warnings;
+
+sub min_rotations {
+ my ($word) = @_;
+ my $original_word = $word;
+ my $rotation_count = 1;
+
+ while (1) {
+ my $rotation = $rotation_count % length($word);
+ $word = substr($word, $rotation) . substr($word, 0, $rotation);
+ last if $word eq $original_word;
+ $rotation_count += 1;
+ }
+
+ return $rotation_count;
+}
+
+my $word = 'xyxx';
+print "Minimum non-zero rotations for '$word': ", min_rotations($word), "\n"; # Output: 7
+
diff --git a/challenge-063/lubos-kolouch/python/ch-1.py b/challenge-063/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..56bf91bbc0
--- /dev/null
+++ b/challenge-063/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,39 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import re
+from typing import Optional, Pattern
+
+
+def last_word(string: str, regexp: Pattern) -> Optional[str]:
+ """
+ Returns the last word matching the given regular expression in the input string.
+
+ Args:
+ string (str): The input string to search for matching words.
+ regexp (Pattern): The regular expression pattern to match words.
+
+ Returns:
+ Optional[str]: The last word matching the regular expression, or None if not found.
+ """
+ words = re.findall(r'\S+', string)
+ last_word = None
+
+ for word in words:
+ if regexp.search(word):
+ last_word = word
+
+ return last_word
+
+
+print(last_word(' hello world', re.compile(r'[ea]l')) or "None") # 'hello'
+print(
+ last_word("Don't match too much, Chet!", re.compile(
+ r'ch.t', re.IGNORECASE)) or "None") # 'Chet!'
+print(
+ last_word("spaces in regexp won't match", re.compile(r'in re'))
+ or "None") # None
+print(
+ last_word(' '.join(str(i) for i in range(1,
+ int(1e6) + 1)),
+ re.compile(r'^(3.*?){3}')) or "None") # '399933'
diff --git a/challenge-063/lubos-kolouch/python/ch-2.py b/challenge-063/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..7ce2e59916
--- /dev/null
+++ b/challenge-063/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,21 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+
+def min_rotations(word: str) -> int:
+ original_word = word
+ rotation_count = 1
+
+ while True:
+ rotation = rotation_count % len(word)
+ word = word[rotation:] + word[:rotation]
+ if word == original_word:
+ break
+ rotation_count += 1
+
+ return rotation_count
+
+
+word = 'xyxx'
+print(f"Minimum non-zero rotations for '{word}': {min_rotations(word)}"
+ ) # Output: 7