aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-01-26 14:21:06 +0000
committerGitHub <noreply@github.com>2024-01-26 14:21:06 +0000
commit6979eed45f057c5cd0557531a8aee5d94702f9b2 (patch)
tree808e59ed538e7a5daf58ec828cc81ab4d9e5bdf3
parent287953a08a0f36c30985e3d87c593c8b80b5987f (diff)
parent025c580ff2d2ad2c8649179ce264d1f1bb345087 (diff)
downloadperlweeklychallenge-club-6979eed45f057c5cd0557531a8aee5d94702f9b2.tar.gz
perlweeklychallenge-club-6979eed45f057c5cd0557531a8aee5d94702f9b2.tar.bz2
perlweeklychallenge-club-6979eed45f057c5cd0557531a8aee5d94702f9b2.zip
Merge pull request #9462 from LubosKolouch/master
feat(challenge-253/lubos-kolouch/perl,python,raku/): Challenge 253 LK Perl Python Raku
-rw-r--r--challenge-253/lubos-kolouch/perl/ch-1.pl24
-rw-r--r--challenge-253/lubos-kolouch/perl/ch-2.pl34
-rw-r--r--challenge-253/lubos-kolouch/python/ch-1.py32
-rw-r--r--challenge-253/lubos-kolouch/python/ch-2.py39
-rw-r--r--challenge-253/lubos-kolouch/raku/ch-1.raku13
-rw-r--r--challenge-253/lubos-kolouch/raku/ch-2.raku23
6 files changed, 165 insertions, 0 deletions
diff --git a/challenge-253/lubos-kolouch/perl/ch-1.pl b/challenge-253/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..7ff173d2ab
--- /dev/null
+++ b/challenge-253/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,24 @@
+use strict;
+use warnings;
+
+sub split_strings {
+ my ($words, $separator) = @_;
+
+ # Flatten the split results and remove empty strings
+ my @result = grep { $_ ne '' } map { split /\Q$separator\E/, $_ } @$words;
+
+ return \@result;
+}
+
+# Tests
+use Test::More tests => 2;
+is_deeply(split_strings(["one.two.three", "four.five", "six"], "."), ["one", "two", "three", "four", "five", "six"]);
+
+# Note: Escaping the $ character with a backslash
+is_deeply(split_strings(["\$perl\$\$", "\$\$raku\$"], "\$"), ["perl", "raku"]);
+
+# Sample Outputs
+print join(",", @{split_strings(["one.two.three", "four.five", "six"], ".")}), "\n";
+
+# Note: Escaping the $ character with a backslash
+print join(",", @{split_strings(["\$perl\$\$", "\$\$raku\$"], "\$")}), "\n";
diff --git a/challenge-253/lubos-kolouch/perl/ch-2.pl b/challenge-253/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..3659fd55ef
--- /dev/null
+++ b/challenge-253/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+sub weakest_row {
+ my ($matrix) = @_;
+
+ # Count the number of 1s in each row and keep track of the row index
+ my @row_strength = map { [scalar(grep { $_ == 1 } @{$matrix->[$_]}), $_] } 0..$#$matrix;
+
+ # Sort based on the number of 1s and then by row index
+ @row_strength = sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] } @row_strength;
+
+ # Extract and return the sorted indices
+ return map { $_->[1] } @row_strength;
+}
+
+# Test the function
+my @result1 = weakest_row([
+ [1, 1, 0, 0, 0],
+ [1, 1, 1, 1, 0],
+ [1, 0, 0, 0, 0],
+ [1, 1, 0, 0, 0],
+ [1, 1, 1, 1, 1]
+]);
+print "@result1\n"; # Should print 2 0 3 1 4
+
+my @result2 = weakest_row([
+ [1, 0, 0, 0],
+ [1, 1, 1, 1],
+ [1, 0, 0, 0],
+ [1, 0, 0, 0]
+]);
+print "@result2\n"; # Should print 0 2 3 1
diff --git a/challenge-253/lubos-kolouch/python/ch-1.py b/challenge-253/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..aeaad5ea59
--- /dev/null
+++ b/challenge-253/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,32 @@
+def split_strings(words, separator):
+ """
+ Splits strings in an array by a given separator and returns non-empty strings.
+
+ Args:
+ words (List[str]): A list of strings to be split.
+ separator (str): The character used as a separator.
+
+ Returns:
+ List[str]: A list of non-empty strings after splitting by the separator.
+ """
+ result = []
+ for word in words:
+ # Split each word by the separator and extend the result list
+ result.extend([s for s in word.split(separator) if s])
+ return result
+
+
+# Tests
+assert split_strings(["one.two.three", "four.five", "six"], ".") == [
+ "one",
+ "two",
+ "three",
+ "four",
+ "five",
+ "six",
+]
+assert split_strings(["$perl$$", "$$raku$"], "$") == ["perl", "raku"]
+
+# Sample Outputs
+print(split_strings(["one.two.three", "four.five", "six"], "."))
+print(split_strings(["$perl$$", "$$raku$"], "$"))
diff --git a/challenge-253/lubos-kolouch/python/ch-2.py b/challenge-253/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..67f6f3e678
--- /dev/null
+++ b/challenge-253/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,39 @@
+from typing import List
+
+
+def weakest_row(matrix: list[list[int]]) -> list[int]:
+ """
+ Returns the indices of the rows in the matrix sorted from the weakest to the strongest.
+
+ A row is considered weaker if it has fewer 1s or, in case of a tie, has a lower index.
+
+ :param matrix: A list of lists representing the binary matrix.
+ :return: A list of integers representing the order of rows from weakest to strongest.
+ """
+ # Count the number of 1s in each row and keep track of the row index
+ row_strength = [(sum(row), index) for index, row in enumerate(matrix)]
+
+ # Sort based on the number of 1s and then by row index
+ row_strength.sort()
+
+ # Extract and return the sorted indices
+ return [index for _, index in row_strength]
+
+
+# Test the function
+assert weakest_row(
+ [
+ [1, 1, 0, 0, 0],
+ [1, 1, 1, 1, 0],
+ [1, 0, 0, 0, 0],
+ [1, 1, 0, 0, 0],
+ [1, 1, 1, 1, 1],
+ ]
+) == [2, 0, 3, 1, 4]
+
+assert weakest_row([[1, 0, 0, 0], [1, 1, 1, 1], [1, 0, 0, 0], [1, 0, 0, 0]]) == [
+ 0,
+ 2,
+ 3,
+ 1,
+]
diff --git a/challenge-253/lubos-kolouch/raku/ch-1.raku b/challenge-253/lubos-kolouch/raku/ch-1.raku
new file mode 100644
index 0000000000..d6d4ee6a67
--- /dev/null
+++ b/challenge-253/lubos-kolouch/raku/ch-1.raku
@@ -0,0 +1,13 @@
+sub split-strings(@words, $separator) {
+ # Flatten the split results and remove empty strings
+ return flat(@words.map(*.split($separator))).grep(*.chars);
+}
+
+# Tests
+use Test;
+is-deeply split-strings(<one.two.three four.five six>, '.'), <one two three four five six>;
+is-deeply split-strings(<$perl$$ $$raku$>, '$'), <perl raku>;
+
+# Sample Outputs
+say split-strings(<one.two.three four.five six>, '.').join(",");
+say split-strings(<$perl$$ $$raku$>, '$').join(",");
diff --git a/challenge-253/lubos-kolouch/raku/ch-2.raku b/challenge-253/lubos-kolouch/raku/ch-2.raku
new file mode 100644
index 0000000000..89c594d18a
--- /dev/null
+++ b/challenge-253/lubos-kolouch/raku/ch-2.raku
@@ -0,0 +1,23 @@
+sub weakest-row(@matrix) {
+ # Count the number of 1s in each row and keep track of the row index
+ my @row-strength = @matrix.kv.map({ (.grep(* == 1).elems, .key) });
+
+ # Sort based on the number of 1s and then by row index
+ @row-strength.sort({$^a[0] <=> $^b[0] || $^a[1] <=> $^b[1]}).map(*[1])
+}
+
+# Test the function
+say weakest-row([
+ [1, 1, 0, 0, 0],
+ [1, 1, 1, 1, 0],
+ [1, 0, 0, 0, 0],
+ [1, 1, 0, 0, 0],
+ [1, 1, 1, 1, 1]
+]); # Should output (2, 0, 3, 1, 4)
+
+say weakest-row([
+ [1, 0, 0, 0],
+ [1, 1, 1, 1],
+ [1, 0, 0, 0],
+ [1, 0, 0, 0]
+]); # Should output (0, 2, 3, 1)