aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-248/lubos-kolouch/blog.txt1
-rw-r--r--challenge-248/lubos-kolouch/perl/ch-1.pl19
-rw-r--r--challenge-248/lubos-kolouch/perl/ch-2.pl29
-rw-r--r--challenge-248/lubos-kolouch/python/ch-1.py21
-rw-r--r--challenge-248/lubos-kolouch/python/ch-2.py34
-rw-r--r--challenge-248/lubos-kolouch/raku/ch-1.raku12
-rw-r--r--challenge-248/lubos-kolouch/raku/ch-2.raku20
7 files changed, 136 insertions, 0 deletions
diff --git a/challenge-248/lubos-kolouch/blog.txt b/challenge-248/lubos-kolouch/blog.txt
new file mode 100644
index 0000000000..9eafded748
--- /dev/null
+++ b/challenge-248/lubos-kolouch/blog.txt
@@ -0,0 +1 @@
+https://egroup.kolouch.org/nextcloud/sites/lubos/2023-12-18_Weekly_challenge_248
diff --git a/challenge-248/lubos-kolouch/perl/ch-1.pl b/challenge-248/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..a21f8ac0d7
--- /dev/null
+++ b/challenge-248/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,19 @@
+use strict;
+use warnings;
+use Test::More tests => 2;
+
+sub shortest_distance {
+ my ( $s, $char ) = @_;
+ my @positions = grep { substr( $s, $_, 1 ) eq $char } 0 .. length($s) - 1;
+ return map {
+ my $i = $_;
+ my $min = List::Util::min map { abs( $i - $_ ) } @positions
+ } 0 .. length($s) - 1;
+}
+
+# Tests
+is_deeply(
+ [ shortest_distance( "loveleetcode", "e" ) ],
+ [ 3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0 ]
+);
+is_deeply( [ shortest_distance( "aaab", "b" ) ], [ 3, 2, 1, 0 ] );
diff --git a/challenge-248/lubos-kolouch/perl/ch-2.pl b/challenge-248/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..51081010a7
--- /dev/null
+++ b/challenge-248/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,29 @@
+use strict;
+use warnings;
+use Test::More tests => 2;
+
+sub construct_matrix {
+ my ($a) = @_;
+ my @b;
+
+ for my $i ( 0 .. @$a - 2 ) {
+ for my $k ( 0 .. @{ $a->[$i] } - 2 ) {
+ $b[$i][$k] =
+ $a->[$i][$k] +
+ $a->[$i][ $k + 1 ] +
+ $a->[ $i + 1 ][$k] +
+ $a->[ $i + 1 ][ $k + 1 ];
+ }
+ }
+
+ return \@b;
+}
+
+# Test Cases
+my $test1 =
+ construct_matrix( [ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ] ] );
+is_deeply( $test1, [ [ 14, 18, 22 ], [ 30, 34, 38 ] ], 'Test Case 1' );
+
+my $test2 = construct_matrix(
+ [ [ 1, 0, 0, 0 ], [ 0, 1, 0, 0 ], [ 0, 0, 1, 0 ], [ 0, 0, 0, 1 ] ] );
+is_deeply( $test2, [ [ 2, 1, 0 ], [ 1, 2, 1 ], [ 0, 1, 2 ] ], 'Test Case 2' );
diff --git a/challenge-248/lubos-kolouch/python/ch-1.py b/challenge-248/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..b4ba06c2e5
--- /dev/null
+++ b/challenge-248/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,21 @@
+#!/usr/bin/env python
+
+
+def shortest_distance(s: str, char: str) -> list[int]:
+ """
+ Calculate the shortest distance to the specified character for each character in the string.
+
+ Args:
+ s (str): The input string.
+ char (str): The character to find the distance to.
+
+ Returns:
+ list[int]: A list of distances for each character in the string.
+ """
+ positions = [i for i, c in enumerate(s) if c == char]
+ return [min(abs(i - pos) for pos in positions) for i in range(len(s))]
+
+
+# Tests
+assert shortest_distance("loveleetcode", "e") == [3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0]
+assert shortest_distance("aaab", "b") == [3, 2, 1, 0]
diff --git a/challenge-248/lubos-kolouch/python/ch-2.py b/challenge-248/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..897c887bef
--- /dev/null
+++ b/challenge-248/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,34 @@
+#!/usr/bin/env python
+
+import unittest
+from typing import List
+
+
+def construct_matrix(a: list[list[int]]) -> list[list[int]]:
+ n, m = len(a), len(a[0])
+ b = [
+ [
+ sum(a[i + di][j + dj] for di in range(2) for dj in range(2))
+ for j in range(m - 1)
+ ]
+ for i in range(n - 1)
+ ]
+ return b
+
+
+class TestConstructMatrix(unittest.TestCase):
+ def test_case_1(self):
+ self.assertEqual(
+ construct_matrix([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]),
+ [[14, 18, 22], [30, 34, 38]],
+ )
+
+ def test_case_2(self):
+ self.assertEqual(
+ construct_matrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]),
+ [[2, 1, 0], [1, 2, 1], [0, 1, 2]],
+ )
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/challenge-248/lubos-kolouch/raku/ch-1.raku b/challenge-248/lubos-kolouch/raku/ch-1.raku
new file mode 100644
index 0000000000..5f64343bb2
--- /dev/null
+++ b/challenge-248/lubos-kolouch/raku/ch-1.raku
@@ -0,0 +1,12 @@
+use Test;
+
+sub shortest-distance(Str $s, Str $char) {
+ my @positions = $s.comb.indices($char);
+ return $s.chars.map({ my $i = $_; @positions.map({ abs($i - $_) }).min });
+}
+
+# Tests
+is-deeply shortest-distance("loveleetcode", "e"), [3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0];
+is-deeply shortest-distance("aaab", "b"), [3, 2, 1, 0];
+
+done-testing;
diff --git a/challenge-248/lubos-kolouch/raku/ch-2.raku b/challenge-248/lubos-kolouch/raku/ch-2.raku
new file mode 100644
index 0000000000..8b78dff3f6
--- /dev/null
+++ b/challenge-248/lubos-kolouch/raku/ch-2.raku
@@ -0,0 +1,20 @@
+use Test;
+
+sub construct-matrix(@a) {
+ my @b;
+ for 0..@a.end - 1 -> $i {
+ for 0..@a[$i].end - 1 -> $k {
+ @b[$i][$k] = @a[$i][$k] + @a[$i][$k + 1] + @a[$i + 1][$k] + @a[$i + 1][$k + 1];
+ }
+ }
+ return @b;
+}
+
+# Test Cases
+is-deeply construct-matrix([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]),
+ [[14, 18, 22], [30, 34, 38]], 'Test Case 1';
+
+is-deeply construct-matrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]),
+ [[2, 1, 0], [1, 2, 1], [0, 1, 2]], 'Test Case 2';
+
+done-testing();