diff options
| -rw-r--r-- | challenge-064/lubos-kolouch/perl/ch-1.pl | 38 | ||||
| -rw-r--r-- | challenge-064/lubos-kolouch/perl/ch-2.pl | 31 | ||||
| -rw-r--r-- | challenge-064/lubos-kolouch/python/ch-1.py | 26 | ||||
| -rw-r--r-- | challenge-064/lubos-kolouch/python/ch-2.py | 30 | ||||
| -rw-r--r-- | challenge-065/lubos-kolouch/perl/ch-1.pl | 29 | ||||
| -rw-r--r-- | challenge-065/lubos-kolouch/perl/ch-2.pl | 41 | ||||
| -rw-r--r-- | challenge-065/lubos-kolouch/python/ch-1.py | 27 | ||||
| -rw-r--r-- | challenge-065/lubos-kolouch/python/ch-2.py | 35 | ||||
| -rw-r--r-- | challenge-066/lubos-kolouch/perl/ch-1.pl | 29 | ||||
| -rw-r--r-- | challenge-066/lubos-kolouch/perl/ch-2.pl | 27 | ||||
| -rw-r--r-- | challenge-066/lubos-kolouch/python/ch-1.py | 23 | ||||
| -rw-r--r-- | challenge-066/lubos-kolouch/python/ch-2.py | 23 | ||||
| -rw-r--r-- | challenge-067/lubos-kolouch/perl/ch-1.pl | 28 | ||||
| -rw-r--r-- | challenge-067/lubos-kolouch/perl/ch-2.pl | 36 | ||||
| -rw-r--r-- | challenge-067/lubos-kolouch/python/ch-1.py | 23 | ||||
| -rw-r--r-- | challenge-067/lubos-kolouch/python/ch-2.py | 33 |
16 files changed, 479 insertions, 0 deletions
diff --git a/challenge-064/lubos-kolouch/perl/ch-1.pl b/challenge-064/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..2b6420eecc --- /dev/null +++ b/challenge-064/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,38 @@ +use strict; +use warnings; + +sub min_sum_path { + my ($matrix) = @_; + my $rows = @$matrix; + my $columns = @{$matrix->[0]}; + + for my $i (1 .. $rows - 1) { + $matrix->[$i][0] += $matrix->[$i - 1][0]; + } + + for my $j (1 .. $columns - 1) { + $matrix->[0][$j] += $matrix->[0][$j - 1]; + } + + for my $i (1 .. $rows - 1) { + for my $j (1 .. $columns - 1) { + $matrix->[$i][$j] += min($matrix->[$i - 1][$j], $matrix->[$i][$j - 1]); + } + } + + return $matrix->[-1][-1]; +} + +sub min { + my ($a, $b) = @_; + return $a < $b ? $a : $b; +} + +my $matrix = [ + [1, 2, 3], + [4, 5, 6], + [7, 8, 9] +]; + +print "Minimum sum path: ", min_sum_path($matrix), "\n"; # Output: 21 + diff --git a/challenge-064/lubos-kolouch/perl/ch-2.pl b/challenge-064/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..9dc2a98517 --- /dev/null +++ b/challenge-064/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,31 @@ +use strict; +use warnings; + +sub word_sequence { + my ($S, $W) = @_; + return helper($S, $W, []); +} + +sub helper { + my ($s, $W, $words) = @_; + return $words unless $s; + + for my $word (@$W) { + if (index($s, $word) == 0) { + my $result = helper(substr($s, length($word)), $W, [@$words, $word]); + return $result if @$result; + } + } + + return []; +} + +my $S1 = "perlweeklychallenge"; +my $W1 = ["weekly", "challenge", "perl"]; + +my $S2 = "perlandraku"; +my $W2 = ["python", "ruby", "haskell"]; + +print join(', ', @{word_sequence($S1, $W1)}), "\n"; # Output: perl, weekly, challenge +print join(', ', @{word_sequence($S2, $W2)}), "\n"; # Output: 0 + diff --git a/challenge-064/lubos-kolouch/python/ch-1.py b/challenge-064/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..1d513712a3 --- /dev/null +++ b/challenge-064/lubos-kolouch/python/ch-1.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from typing import List + + +def min_sum_path(matrix: List[List[int]]) -> int: + rows = len(matrix) + columns = len(matrix[0]) + + for i in range(1, rows): + matrix[i][0] += matrix[i - 1][0] + + for j in range(1, columns): + matrix[0][j] += matrix[0][j - 1] + + for i in range(1, rows): + for j in range(1, columns): + matrix[i][j] += min(matrix[i - 1][j], matrix[i][j - 1]) + + return matrix[-1][-1] + + +matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] + +print(f"Minimum sum path: {min_sum_path(matrix)}") # Output: 21 diff --git a/challenge-064/lubos-kolouch/python/ch-2.py b/challenge-064/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..df2d2041c3 --- /dev/null +++ b/challenge-064/lubos-kolouch/python/ch-2.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from typing import List + + +def word_sequence(S: str, W: List[str]) -> List[str]: + + def helper(s: str, words: List[str]) -> List[str]: + if not s: + return words + for word in W: + if s.startswith(word): + result = helper(s[len(word):], words + [word]) + if result: + return result + return [] + + result = helper(S, []) + return result if result else [0] + + +S1 = "perlweeklychallenge" +W1 = ["weekly", "challenge", "perl"] + +S2 = "perlandraku" +W2 = ["python", "ruby", "haskell"] + +print(word_sequence(S1, W1)) # Output: ['perl', 'weekly', 'challenge'] +print(word_sequence(S2, W2)) # Output: [0] diff --git a/challenge-065/lubos-kolouch/perl/ch-1.pl b/challenge-065/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..88d6b24fe7 --- /dev/null +++ b/challenge-065/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,29 @@ +use strict; +use warnings; + +sub numbers_with_sum_of_digits { + my ($N, $S) = @_; + return helper($N, $S, 0, $N); +} + +sub helper { + my ($n, $s, $current, $N) = @_; + return $s == 0 ? [$current] : [] if $n == 0; + + my @numbers; + for my $i (0..9) { + if ($n == $N && $i == 0) { # First digit can't be 0 + next; + } + if ($s - $i >= 0) { + push @numbers, @{helper($n - 1, $s - $i, $current * 10 + $i, $N)}; + } + } + return \@numbers; +} + +my $N = 2; +my $S = 4; + +print join(', ', @{numbers_with_sum_of_digits($N, $S)}), "\n"; # Output: 13, 22, 31, 40 + diff --git a/challenge-065/lubos-kolouch/perl/ch-2.pl b/challenge-065/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..ea30342c0e --- /dev/null +++ b/challenge-065/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,41 @@ +use strict; +use warnings; + +sub is_palindrome { + my $s = shift; + return $s eq reverse($s); +} + +sub palindrome_partitions { + my ($s, $start, $current_partition, $result) = @_; + if ($start == length($s)) { + push @$result, [@$current_partition]; + return; + } + + for my $end ($start + 1 .. length($s)) { + my $substr = substr($s, $start, $end - $start); + if (is_palindrome($substr)) { + push @$current_partition, $substr; + palindrome_partitions($s, $end, $current_partition, $result); + pop @$current_partition; + } + } +} + +my $S = 'aabaab'; +my @result; + +palindrome_partitions($S, 0, [], \@result); + +if (@result) { + print "There are ", scalar(@result), " possible solutions.\n"; + my $index = 1; + for my $partition (@result) { + print "$index) ", join(', ', @$partition), "\n"; + $index++; + } +} else { + print "-1\n"; +} + diff --git a/challenge-065/lubos-kolouch/python/ch-1.py b/challenge-065/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..6c41e62e1d --- /dev/null +++ b/challenge-065/lubos-kolouch/python/ch-1.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from typing import List + + +def numbers_with_sum_of_digits(N: int, S: int) -> List[int]: + + def helper(n: int, s: int, current: int) -> List[int]: + if n == 0: + return [current] if s == 0 else [] + + numbers = [] + for i in range(10): + if n == N and i == 0: # First digit can't be 0 + continue + if s - i >= 0: + numbers.extend(helper(n - 1, s - i, current * 10 + i)) + return numbers + + return helper(N, S, 0) + + +N = 2 +S = 4 + +print(numbers_with_sum_of_digits(N, S)) # Output: [13, 22, 31, 40] diff --git a/challenge-065/lubos-kolouch/python/ch-2.py b/challenge-065/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..2c28ad8791 --- /dev/null +++ b/challenge-065/lubos-kolouch/python/ch-2.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from typing import List + + +def is_palindrome(s: str) -> bool: + return s == s[::-1] + + +def palindrome_partitions(s: str, start: int, current_partition: List[str], + result: List[List[str]]) -> None: + if start == len(s): + result.append(current_partition[:]) + return + + for end in range(start + 1, len(s) + 1): + substr = s[start:end] + if is_palindrome(substr): + current_partition.append(substr) + palindrome_partitions(s, end, current_partition, result) + current_partition.pop() + + +S = 'aabaab' +result = [] + +palindrome_partitions(S, 0, [], result) + +if result: + print(f"There are {len(result)} possible solutions.") + for index, partition in enumerate(result, start=1): + print(f"{index}) {', '.join(partition)}") +else: + print("-1") diff --git a/challenge-066/lubos-kolouch/perl/ch-1.pl b/challenge-066/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..3f5de12d04 --- /dev/null +++ b/challenge-066/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,29 @@ +#!/usr/bin/perl +use strict; +use warnings; + +sub divide { + my ($M, $N) = @_; + + if ($N == 0) { + die "Division by zero is not allowed.\n"; + } + + my $sign = (($M < 0) ^ ($N < 0)) ? -1 : 1; + $M = abs($M); + $N = abs($N); + + my $result = 0; + while ($M >= $N) { + $M -= $N; + $result++; + } + + return $sign * $result; +} + +my $M = -5; +my $N = -2; +my $output = divide($M, $N); +print "$output\n"; + diff --git a/challenge-066/lubos-kolouch/perl/ch-2.pl b/challenge-066/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..2f9a3c27da --- /dev/null +++ b/challenge-066/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,27 @@ +#!/usr/bin/perl +use strict; +use warnings; + +sub find_expressions { + my ($N) = @_; + my @solutions; + + for my $m (2 .. sqrt($N)) { + for my $n (2 .. log($N)/log($m)) { + if ($m ** $n == $N) { + push @solutions, "$m^$n"; + } + } + } + + return @solutions ? \@solutions : 0; +} + +my $N = 64; +my $result = find_expressions($N); +if (ref $result eq 'ARRAY') { + print join(' or ', @$result), "\n"; +} else { + print "0\n"; +} + diff --git a/challenge-066/lubos-kolouch/python/ch-1.py b/challenge-066/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..e1454bead7 --- /dev/null +++ b/challenge-066/lubos-kolouch/python/ch-1.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + + +def divide(M: int, N: int) -> int: + if N == 0: + raise ValueError("Division by zero is not allowed.") + + sign = -1 if (M < 0) ^ (N < 0) else 1 + M, N = abs(M), abs(N) + + result = 0 + while M >= N: + M -= N + result += 1 + + return sign * result + + +M = -5 +N = -2 +output = divide(M, N) +print(output) diff --git a/challenge-066/lubos-kolouch/python/ch-2.py b/challenge-066/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..fbacadb555 --- /dev/null +++ b/challenge-066/lubos-kolouch/python/ch-2.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from math import sqrt, log + + +def find_expressions(N: int): + solutions = [] + + for m in range(2, int(sqrt(N)) + 1): + for n in range(2, int(log(N) / log(m)) + 1): + if m**n == N: + solutions.append(f"{m}^{n}") + + return solutions or 0 + + +N = 64 +result = find_expressions(N) +if isinstance(result, list): + print(' or '.join(result)) +else: + print("0") diff --git a/challenge-067/lubos-kolouch/perl/ch-1.pl b/challenge-067/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..ffea2a4846 --- /dev/null +++ b/challenge-067/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl +use strict; +use warnings; + +sub combinations { + my ($m, $n) = @_; + my @result; + + my $rec_comb; + $rec_comb = sub { + my ($start, $depth, @comb) = @_; + if ($depth == 0) { + push @result, [@comb]; + return; + } + for my $i ($start..$m) { + $rec_comb->($i + 1, $depth - 1, @comb, $i); + } + }; + + $rec_comb->(1, $n); + return \@result; +} + +my ($m, $n) = (5, 2); +my $combinations = combinations($m, $n); +print "[ ", join(", ", map { "[" . join(",", @$_) . "]" } @$combinations), " ]\n"; + diff --git a/challenge-067/lubos-kolouch/perl/ch-2.pl b/challenge-067/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..5ec9178b4a --- /dev/null +++ b/challenge-067/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,36 @@ +#!/usr/bin/perl +use strict; +use warnings; + +sub letter_combinations { + my ($s) = @_; + my %phone = ( + '2' => ['a', 'b', 'c'], + '3' => ['d', 'e', 'f'], + '4' => ['g', 'h', 'i'], + '5' => ['j', 'k', 'l'], + '6' => ['m', 'n', 'o'], + '7' => ['p', 'q', 'r', 's'], + '8' => ['t', 'u', 'v'], + '9' => ['w', 'x', 'y', 'z'], + ); + + my @result = (''); + + for my $digit (split //, $s) { + my @new_result; + for my $res (@result) { + for my $letter (@{$phone{$digit}}) { + push @new_result, $res . $letter; + } + } + @result = @new_result; + } + + return \@result; +} + +my $s = '35'; +my $combinations = letter_combinations($s); +print "[", join(", ", map { "\"$_\"" } @$combinations), "]\n"; + diff --git a/challenge-067/lubos-kolouch/python/ch-1.py b/challenge-067/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..d62637d118 --- /dev/null +++ b/challenge-067/lubos-kolouch/python/ch-1.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from typing import List + + +def combinations(m: int, n: int) -> List[List[int]]: + result = [] + + def rec_comb(start: int, depth: int, comb: List[int]) -> None: + if depth == 0: + result.append(comb) + return + for i in range(start, m + 1): + rec_comb(i + 1, depth - 1, comb + [i]) + + rec_comb(1, n, []) + return result + + +m, n = 5, 2 +combinations_list = combinations(m, n) +print("[", ", ".join(str(comb) for comb in combinations_list), "]") diff --git a/challenge-067/lubos-kolouch/python/ch-2.py b/challenge-067/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..31be7b2047 --- /dev/null +++ b/challenge-067/lubos-kolouch/python/ch-2.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from typing import List + + +def letter_combinations(s: str) -> List[str]: + phone = { + '2': ['a', 'b', 'c'], + '3': ['d', 'e', 'f'], + '4': ['g', 'h', 'i'], + '5': ['j', 'k', 'l'], + '6': ['m', 'n', 'o'], + '7': ['p', 'q', 'r', 's'], + '8': ['t', 'u', 'v'], + '9': ['w', 'x', 'y', 'z'], + } + + result = [''] + + for digit in s: + new_result = [] + for res in result: + for letter in phone[digit]: + new_result.append(res + letter) + result = new_result + + return result + + +s = '35' +combinations_list = letter_combinations(s) +print("[", ", ".join(f'"{comb}"' for comb in combinations_list), "]") |
