aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-253/packy-anderson/README.md2
-rw-r--r--challenge-253/packy-anderson/blog.txt1
-rwxr-xr-xchallenge-253/packy-anderson/perl/ch-1.pl23
-rwxr-xr-xchallenge-253/packy-anderson/perl/ch-2.pl53
-rwxr-xr-xchallenge-253/packy-anderson/python/ch-1.py24
-rwxr-xr-xchallenge-253/packy-anderson/python/ch-2.py56
-rwxr-xr-xchallenge-253/packy-anderson/raku/ch-1.raku23
-rwxr-xr-xchallenge-253/packy-anderson/raku/ch-2.raku51
8 files changed, 232 insertions, 1 deletions
diff --git a/challenge-253/packy-anderson/README.md b/challenge-253/packy-anderson/README.md
index 336f14bee6..9ec528211e 100644
--- a/challenge-253/packy-anderson/README.md
+++ b/challenge-253/packy-anderson/README.md
@@ -16,4 +16,4 @@
## Blog Post
-[Sum Enchanted Evening](https://packy.dardan.com/b/GW)
+[The Weakest Split](https://packy.dardan.com/b/Gu)
diff --git a/challenge-253/packy-anderson/blog.txt b/challenge-253/packy-anderson/blog.txt
new file mode 100644
index 0000000000..a428b692cf
--- /dev/null
+++ b/challenge-253/packy-anderson/blog.txt
@@ -0,0 +1 @@
+https://packy.dardan.com/b/Gu \ No newline at end of file
diff --git a/challenge-253/packy-anderson/perl/ch-1.pl b/challenge-253/packy-anderson/perl/ch-1.pl
new file mode 100755
index 0000000000..4925a259e7
--- /dev/null
+++ b/challenge-253/packy-anderson/perl/ch-1.pl
@@ -0,0 +1,23 @@
+#!/usr/bin/env perl
+use v5.38;
+
+sub splitOnSeparator($separator, @words) {
+ my @output;
+ foreach my $str ( @words ) {
+ push @output, grep { !/^$/ } split(/\Q$separator/, $str);
+ }
+ return @output;
+}
+
+sub solution($separator, @words) {
+ say 'Input: @words = ("' . join("', '", @words) . '")';
+ say ' $separator = "' . $separator . '"';
+ my @output = splitOnSeparator($separator, @words);
+ say 'Output: "' . join('", "', @output) . '"';
+}
+
+say "Example 1:";
+solution('.', "one.two.three","four.five","six");
+
+say "\nExample 2:";
+solution('$', '$perl$$', '$$raku$'); \ No newline at end of file
diff --git a/challenge-253/packy-anderson/perl/ch-2.pl b/challenge-253/packy-anderson/perl/ch-2.pl
new file mode 100755
index 0000000000..37b7e3db38
--- /dev/null
+++ b/challenge-253/packy-anderson/perl/ch-2.pl
@@ -0,0 +1,53 @@
+#!/usr/bin/env perl
+use v5.38;
+
+use List::Util qw( sum );
+
+sub weakestRows(@matrix) {
+ my @oneCount = map { sum(@$_) } @matrix;
+ my @weakest = sort {
+ # sort by count first
+ $oneCount[$a] <=> $oneCount[$b]
+ ||
+ # then by index order
+ $a cmp $b
+ } (0 .. $#oneCount);
+
+ return @weakest;
+}
+
+sub formatMatrix($matrix, $indent) {
+ my @output;
+ foreach my $row ( @$matrix ) {
+ my $output_row = q{ } x $indent . " [";
+ $output_row .= join(', ', @$row) . "]";
+ push @output, $output_row;
+ }
+ return "[\n"
+ . join(",\n", @output)
+ . "\n"
+ . q{ } x $indent . "]";
+}
+
+sub solution(@matrix) {
+ say 'Input: $matrix = ' . formatMatrix(\@matrix, 17);
+ my @output = weakestRows(@matrix);
+ say 'Output: (' . join(', ', @output) . ')';
+}
+
+say "Example 1:";
+solution(
+ [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]
+ );
+
+say "\nExample 2:";
+solution(
+ [1, 0, 0, 0],
+ [1, 1, 1, 1],
+ [1, 0, 0, 0],
+ [1, 0, 0, 0]
+ ); \ No newline at end of file
diff --git a/challenge-253/packy-anderson/python/ch-1.py b/challenge-253/packy-anderson/python/ch-1.py
new file mode 100755
index 0000000000..7d6f592dd7
--- /dev/null
+++ b/challenge-253/packy-anderson/python/ch-1.py
@@ -0,0 +1,24 @@
+#!/usr/bin/env python
+
+def splitOnSeparator(words, separator):
+ output = []
+ for str in words:
+ output.extend(
+ list(filter(lambda w: w > "", str.split(separator)))
+ )
+ return output
+
+def comma_join(arr):
+ return '"' + '", "'.join(arr) + '"'
+
+def solution(words, separator):
+ print(f'Input: @words = ({comma_join(words)})')
+ print(f' $separator = "{separator}"')
+ output = splitOnSeparator(words, separator)
+ print(f'Output: {comma_join(output)}')
+
+print('Example 1:')
+solution(["one.two.three","four.five","six"], '.')
+
+print('\nExample 2:')
+solution(['$perl$$', '$$raku$'], '$')
diff --git a/challenge-253/packy-anderson/python/ch-2.py b/challenge-253/packy-anderson/python/ch-2.py
new file mode 100755
index 0000000000..50006d5d5e
--- /dev/null
+++ b/challenge-253/packy-anderson/python/ch-2.py
@@ -0,0 +1,56 @@
+#!/usr/bin/env python
+
+def weakestRows(matrix):
+ oneCount = [ sum(row) for row in matrix ]
+ # sort the rows by their oneCount values
+ # use the Decorate-Sort-Undecorate idiom
+ # to convert the dict into a list
+ # https://docs.python.org/3/howto/sorting.html#decorate-sort-undecorate
+ decorated = [
+ (oneCount[i], i) for i in range(len(oneCount))
+ ]
+ sorted_tuples = sorted(
+ decorated,
+ # the - before the first element sorts descending
+ key=lambda k: (k[0], k[1])
+ )
+ weakest = [ t[1] for t in sorted_tuples ]
+ return weakest
+
+def comma_join(arr):
+ return ', '.join(map(lambda i: str(i), arr))
+
+def formatMatrix(matrix, indent=17):
+ output = []
+ for row in matrix:
+ output_row = ' ' * indent + ' ['
+ output_row += ', '.join(map(lambda i: str(i), row))
+ output_row += ']'
+ output.append(output_row)
+
+ return(
+ "[\n" + ",\n".join(output) + "\n" +
+ ' ' * indent + ']'
+ )
+
+def solution(matrix):
+ print(f'Input: $matrix = {formatMatrix(matrix)}')
+ output = weakestRows(matrix)
+ print(f'Output: ({comma_join(output)})')
+
+print('Example 1:')
+solution([
+ [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('\nExample 2:')
+solution([
+ [1, 0, 0, 0],
+ [1, 1, 1, 1],
+ [1, 0, 0, 0],
+ [1, 0, 0, 0]
+ ]) \ No newline at end of file
diff --git a/challenge-253/packy-anderson/raku/ch-1.raku b/challenge-253/packy-anderson/raku/ch-1.raku
new file mode 100755
index 0000000000..7a365a74dc
--- /dev/null
+++ b/challenge-253/packy-anderson/raku/ch-1.raku
@@ -0,0 +1,23 @@
+#!/usr/bin/env raku
+use v6;
+
+sub splitOnSeparator(@words, $separator) {
+ my @output;
+ for @words -> $str {
+ @output.append( $str.split($separator, :skip-empty) );
+ }
+ return @output;
+}
+
+sub solution(@words, $separator) {
+ say 'Input: @words = ("' ~ @words.join("', '") ~ '")';
+ say ' $separator = "' ~ $separator ~ '"';
+ my @output = splitOnSeparator(@words, $separator);
+ say 'Output: "' ~ @output.join('", "') ~ '"';
+}
+
+say "Example 1:";
+solution(["one.two.three","four.five","six"], '.');
+
+say "\nExample 2:";
+solution(['$perl$$', '$$raku$'], '$');
diff --git a/challenge-253/packy-anderson/raku/ch-2.raku b/challenge-253/packy-anderson/raku/ch-2.raku
new file mode 100755
index 0000000000..04f329e880
--- /dev/null
+++ b/challenge-253/packy-anderson/raku/ch-2.raku
@@ -0,0 +1,51 @@
+#!/usr/bin/env raku
+use v6;
+
+sub weakestRows(@matrix) {
+ my @oneCount = @matrix.map({ $_.sum });
+ my @weakest = (0 .. @oneCount.end).sort: {
+ # sort by count first
+ @oneCount[$^a] <=> @oneCount[$^b]
+ ||
+ # then by index order
+ $^a <=> $^b
+ };
+
+ return @weakest;
+}
+
+sub formatMatrix(@matrix, $indent) {
+ my @output;
+ for @matrix -> @row {
+ my $output_row = q{ } x $indent ~ " [";
+ $output_row ~= @row.join(', ') ~ "]";
+ @output.push($output_row);
+ }
+ return "[\n"
+ ~ @output.join(",\n")
+ ~ "\n"
+ ~ q{ } x $indent ~ "]";
+}
+
+sub solution(@matrix) {
+ say 'Input: $matrix = ' ~ formatMatrix(@matrix, 17);
+ my @output = weakestRows(@matrix);
+ say 'Output: (' ~ @output.join(', ') ~ ')';
+}
+
+say "Example 1:";
+solution([
+ [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]
+ ]);
+
+say "\nExample 2:";
+solution([
+ [1, 0, 0, 0],
+ [1, 1, 1, 1],
+ [1, 0, 0, 0],
+ [1, 0, 0, 0]
+ ]);