aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-09-03 11:35:40 +0100
committerGitHub <noreply@github.com>2025-09-03 11:35:40 +0100
commit36fc6b23891689f93ad50ccc7d576be8a695a159 (patch)
treee9af5105a45fdbf78247381ebf850405ce8264d7
parentdcf0178f1f308209b36c8800a4f9856c6c87b56c (diff)
parent8381082e05bb2d6c83fb1aed029f9d5d510c8e17 (diff)
downloadperlweeklychallenge-club-36fc6b23891689f93ad50ccc7d576be8a695a159.tar.gz
perlweeklychallenge-club-36fc6b23891689f93ad50ccc7d576be8a695a159.tar.bz2
perlweeklychallenge-club-36fc6b23891689f93ad50ccc7d576be8a695a159.zip
Merge pull request #12621 from packy/master
Challenge 337 solutions by Packy Anderson
-rw-r--r--challenge-337/packy-anderson/README.md2
-rw-r--r--challenge-337/packy-anderson/blog.txt1
-rwxr-xr-xchallenge-337/packy-anderson/elixir/ch-1.exs45
-rwxr-xr-xchallenge-337/packy-anderson/elixir/ch-2.exs116
-rwxr-xr-xchallenge-337/packy-anderson/perl/ch-1.pl36
-rwxr-xr-xchallenge-337/packy-anderson/perl/ch-2.pl99
-rwxr-xr-xchallenge-337/packy-anderson/python/ch-1.py36
-rwxr-xr-xchallenge-337/packy-anderson/python/ch-2.py93
-rwxr-xr-xchallenge-337/packy-anderson/raku/ch-1.raku32
-rwxr-xr-xchallenge-337/packy-anderson/raku/ch-2.raku93
10 files changed, 552 insertions, 1 deletions
diff --git a/challenge-337/packy-anderson/README.md b/challenge-337/packy-anderson/README.md
index 64c1d65b5e..8dd33a6d4c 100644
--- a/challenge-337/packy-anderson/README.md
+++ b/challenge-337/packy-anderson/README.md
@@ -23,4 +23,4 @@
## Blog Post
-[Perl Weekly Challenge: The Score for Group Therapy](https://packy.dardan.com/b/a9)
+[Perl Weekly Challenge: Small, but Oddly Current](https://packy.dardan.com/b/aR)
diff --git a/challenge-337/packy-anderson/blog.txt b/challenge-337/packy-anderson/blog.txt
new file mode 100644
index 0000000000..760fa0590e
--- /dev/null
+++ b/challenge-337/packy-anderson/blog.txt
@@ -0,0 +1 @@
+https://packy.dardan.com/b/aR \ No newline at end of file
diff --git a/challenge-337/packy-anderson/elixir/ch-1.exs b/challenge-337/packy-anderson/elixir/ch-1.exs
new file mode 100755
index 0000000000..a4a163904b
--- /dev/null
+++ b/challenge-337/packy-anderson/elixir/ch-1.exs
@@ -0,0 +1,45 @@
+#!/usr/bin/env elixir
+
+defmodule PWC do
+ def smaller_than([], _, out), do: out
+
+ def smaller_than([i | rest], bag, out) do
+ {_, count} = Enum.map_reduce(Map.keys(bag), 0,
+ fn k, count ->
+ cond do
+ k <= i ->
+ {k, count + Map.get(bag, k)}
+ true ->
+ {k, count}
+ end
+ end
+ )
+ smaller_than(rest, bag, out ++ [count - 1])
+ end
+
+ def smaller_than(num1) do
+ bag = Enum.frequencies(num1)
+ smaller_than(num1, bag, [])
+ end
+
+ def solution(num1) do
+ IO.puts("Input: @ints = (" <> Enum.join(num1, ", ") <> ")")
+ out = smaller_than(num1)
+ IO.puts("Output: (" <> Enum.join(out, ", ") <> ")")
+ end
+end
+
+IO.puts("Example 1:")
+PWC.solution([6, 5, 4, 8])
+
+IO.puts("\nExample 2:")
+PWC.solution([7, 7, 7, 7])
+
+IO.puts("\nExample 3:")
+PWC.solution([5, 4, 3, 2, 1])
+
+IO.puts("\nExample 4:")
+PWC.solution([-1, 0, 3, -2, 1])
+
+IO.puts("\nExample 5:")
+PWC.solution([0, 1, 1, 2, 0])
diff --git a/challenge-337/packy-anderson/elixir/ch-2.exs b/challenge-337/packy-anderson/elixir/ch-2.exs
new file mode 100755
index 0000000000..b9e53e75eb
--- /dev/null
+++ b/challenge-337/packy-anderson/elixir/ch-2.exs
@@ -0,0 +1,116 @@
+#!/usr/bin/env elixir
+
+defmodule PWC do
+ require Integer # because is_odd is a guard
+
+ def display_two(display1, display2) do
+ Enum.zip_with(
+ String.split(display1, "\n"),
+ String.split(display2, "\n"),
+ fn x, y -> x <> y end
+ )
+ |> Enum.join("\n")
+ end
+
+ def display_matrix(label, matrix) do
+ rows = for row <- matrix, do:
+ "[ " <> Enum.join(row, " ") <> " ] "
+ width = (length(Enum.at(matrix, 0)) + 2) * 2 + 1
+ Enum.join(
+ [ String.pad_trailing(label, width) ] ++ rows, "\n"
+ ) <> "\n"
+ end
+
+ def empty_matrix(row, col) do
+ for _r <- 1..row do
+ for _c <- 1..col, do: 0
+ end
+ end
+
+ def increment_row(row, matrix) do
+ List.replace_at(matrix, row,
+ (for elem <- Enum.at(matrix, row), do: elem + 1)
+ )
+ end
+
+ def increment_col(col, matrix) do
+ for row <- matrix, do:
+ List.replace_at(row, col, Enum.at(row, col) + 1)
+ end
+
+ def count_odd(matrix) do
+ {_, count} = Enum.map_reduce(matrix, 0, fn row, count ->
+ {_, count} = Enum.map_reduce(row, count, fn elem, count ->
+ {
+ elem,
+ cond do
+ Integer.is_odd(elem) -> count + 1
+ true -> count
+ end
+ }
+ end)
+ { row, count }
+ end)
+ count
+ end
+
+ def odd_matrix([], matrix, display) do
+ {
+ count_odd(matrix),
+ display <> "\n" <> display_matrix("Final:", matrix)
+ }
+ end
+
+ def odd_matrix([loc | locs], matrix, display) do
+ {row, col} = {List.first(loc), List.last(loc)}
+ display = display <> "\nApply [#{row},#{col}]:\n"
+ display = display <> "Increment row #{row}:\n"
+ before_var = display_matrix("Before", matrix)
+ matrix = increment_row(row, matrix)
+ after_var = display_matrix("After", matrix)
+ display = display <> display_two(before_var, after_var)
+
+ display = display <> "Increment col #{col}:\n"
+ before_var = display_matrix("Before", matrix)
+ matrix = increment_col(col, matrix)
+ after_var = display_matrix("After", matrix)
+ display = display <> display_two(before_var, after_var)
+
+ odd_matrix(locs, matrix, display)
+ end
+
+ def odd_matrix(row, col, locations) do
+ matrix = empty_matrix(row, col)
+ odd_matrix(
+ locations, matrix,
+ display_matrix("Initial:", matrix)
+ )
+ end
+
+ def solution(row, col, locations) do
+ loc_list = locations
+ |> Enum.map(fn l -> "(" <> Enum.join(l,",") <> ")" end)
+ |> Enum.join(",")
+ IO.puts(
+ "Input: $row = #{row}, $col = #{col}, " <>
+ "@locations = (#{loc_list})"
+ )
+ {odd, explain} = odd_matrix(row, col, locations)
+ IO.puts("Output: #{odd}\n\n#{explain}")
+ end
+end
+
+IO.puts("Example 1:")
+PWC.solution(2, 3, [[0,1],[1,1]])
+
+IO.puts("\nExample 2:")
+PWC.solution(2, 2, [[1,1],[0,0]])
+
+IO.puts("\nExample 3:")
+PWC.solution(3, 3, [[0,0],[1,2],[2,1]])
+
+IO.puts("\nExample 4:")
+PWC.solution(1, 5, [[0,2],[0,4]])
+
+IO.puts("\nExample 5:")
+PWC.solution(4, 2, [[1,0],[3,1],[2,0],[0,1]])
diff --git a/challenge-337/packy-anderson/perl/ch-1.pl b/challenge-337/packy-anderson/perl/ch-1.pl
new file mode 100755
index 0000000000..edfe649f30
--- /dev/null
+++ b/challenge-337/packy-anderson/perl/ch-1.pl
@@ -0,0 +1,36 @@
+#!/usr/bin/env perl
+use v5.40;
+
+use List::AllUtils qw( sum );
+use List::MoreUtils qw( frequency );
+
+sub smallerThan(@num1) {
+ my %bag = frequency @num1;
+ my @out;
+ foreach my $i ( 0 .. $#num1 ) {
+ my %bag2 = %bag{grep { $_ <= $num1[$i] } keys %bag };
+ $out[$i] = sum(values %bag2) - 1;
+ }
+ @out
+}
+
+sub solution($num1) {
+ say 'Input: @arr = (' . join(', ', @$num1) . ')';
+ my @out = smallerThan(@$num1);
+ say 'Output: (' . join(', ', @out) . ')';
+}
+
+say "Example 1:";
+solution([6, 5, 4, 8]);
+
+say "\nExample 2:";
+solution([7, 7, 7, 7]);
+
+say "\nExample 3:";
+solution([5, 4, 3, 2, 1]);
+
+say "\nExample 4:";
+solution([-1, 0, 3, -2, 1]);
+
+say "\nExample 5:";
+solution([0, 1, 1, 2, 0]); \ No newline at end of file
diff --git a/challenge-337/packy-anderson/perl/ch-2.pl b/challenge-337/packy-anderson/perl/ch-2.pl
new file mode 100755
index 0000000000..25bcd3c3a1
--- /dev/null
+++ b/challenge-337/packy-anderson/perl/ch-2.pl
@@ -0,0 +1,99 @@
+#!/usr/bin/env perl
+use v5.40;
+
+use List::AllUtils qw( pairwise );
+
+# take two displayMatrix() strings and display them side-by-side
+sub displayTwo($display1, $display2) {
+ # split the two strings and join each row together
+ my @d1 = split /\n/, $display1;
+ my @d2 = split /\n/, $display2;
+ join("\n", pairwise { $a . $b } @d1, @d2) . "\n";
+}
+
+sub displayMatrix($label, @matrix) {
+ my $width = (scalar(@{$matrix[0]}) + 2) * 2 + 1;
+ my $display = sprintf "%-*s\n", $width, $label;
+ foreach my $r (0 .. $#matrix) {
+ my $row = $matrix[$r];
+ $display .= "[ " . join(" ", @$row) . " ] \n";
+ }
+ $display
+}
+
+sub emptyMatrix($row, $col) {
+ my @matrix;
+ foreach my $r (1 .. $row) { push @matrix, [ (0) x $col ]; }
+ @matrix;
+}
+
+sub incrementRow($row, @matrix) {
+ foreach my $col ( 0 .. $#{$matrix[0]} ) {
+ $matrix[$row][$col]++;
+ }
+ @matrix
+}
+
+sub incrementCol($col, @matrix) {
+ foreach my $row ( 0 .. $#matrix ) { $matrix[$row][$col]++; }
+ @matrix
+}
+
+sub countOdd(@matrix) {
+ my $count = 0;
+ foreach my $row ( 0 .. $#matrix ) {
+ foreach my $col ( 0 .. $#{$matrix[0]} ) {
+ $count++ if $matrix[$row][$col] % 2;
+ }
+ }
+ $count
+}
+
+sub oddMatrix($row, $col, @locations) {
+ my @matrix = emptyMatrix($row, $col);
+ my $display = displayMatrix("Initial:", @matrix);
+ foreach my $location ( @locations ) {
+ my ($row, $col) = @$location;
+ $display .= "\nApply [$row,$col]:\n";
+
+ $display .= "Increment row $row:\n";
+ my $before = displayMatrix("Before", @matrix);
+ @matrix = incrementRow($row, @matrix);
+ my $after = displayMatrix("After", @matrix);
+ $display .= displayTwo($before, $after);
+
+ $display .= "Increment col $col:\n";
+ $before = displayMatrix("Before", @matrix);
+ @matrix = incrementCol($col, @matrix);
+ $after = displayMatrix("After", @matrix);
+ $display .= displayTwo($before, $after);
+ }
+ $display .= "\n";
+ return
+ countOdd(@matrix),
+ $display . displayMatrix('Final:', @matrix);
+}
+
+sub solution($row, $col, $locations) {
+ my $loc_list = join ",", map { qq{[$_->[0],$_->[1]]} } @$locations;
+ say qq{Input: \$row = $row, \$col = $col, }
+ . qq{\@locations = ($loc_list)};
+ my ($odd, $explain) = oddMatrix($row, $col, @$locations);
+ say "Output: $odd";
+ say "\n$explain";
+}
+
+say "Example 1:";
+solution(2, 3, [[0,1],[1,1]]);
+
+say "\nExample 2:";
+solution(2, 2, [[1,1],[0,0]]);
+
+say "\nExample 3:";
+solution(3, 3, [[0,0],[1,2],[2,1]]);
+
+say "\nExample 4:";
+solution(1, 5, [[0,2],[0,4]]);
+
+say "\nExample 5:";
+solution(4, 2, [[1,0],[3,1],[2,0],[0,1]]);
diff --git a/challenge-337/packy-anderson/python/ch-1.py b/challenge-337/packy-anderson/python/ch-1.py
new file mode 100755
index 0000000000..5890dcf1e5
--- /dev/null
+++ b/challenge-337/packy-anderson/python/ch-1.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env python
+
+from collections import Counter
+
+def smaller_than(num1):
+ bag = Counter(num1)
+ out = []
+ for i in range(len(num1)):
+ out.append(
+ sum(bag[key] for key in bag.keys() if key <= num1[i]) - 1
+ )
+ return out
+
+def int_join(joiner, arr):
+ return joiner.join(map(lambda i: str(i), arr))
+
+def solution(num1):
+ print(f'Input: @num1 = ({int_join(", ", num1)})')
+ out = smaller_than(num1)
+ print(f'Output: ({int_join(", ", out)})')
+
+
+print('Example 1:')
+solution([6, 5, 4, 8])
+
+print('\nExample 2:')
+solution([7, 7, 7, 7])
+
+print('\nExample 3:')
+solution([5, 4, 3, 2, 1])
+
+print('\nExample 4:')
+solution([-1, 0, 3, -2, 1])
+
+print('\nExample 5:')
+solution([0, 1, 1, 2, 0])
diff --git a/challenge-337/packy-anderson/python/ch-2.py b/challenge-337/packy-anderson/python/ch-2.py
new file mode 100755
index 0000000000..c200df6a7d
--- /dev/null
+++ b/challenge-337/packy-anderson/python/ch-2.py
@@ -0,0 +1,93 @@
+#!/usr/bin/env python
+
+def display_two(display1, display2):
+ display = []
+ # split the two strings and join each row together
+ for d1,d2 in zip(display1.split("\n"), display2.split("\n")):
+ display.append(d1 + d2)
+ return "\n".join(display)
+
+def display_matrix(label, matrix):
+ width = (len(matrix[0]) + 2) * 2 + 1
+ display = label.ljust(width) + "\n"
+ for r in range(len(matrix)):
+ display += "[ " + int_join(" ",matrix[r]) + " ] \n"
+ return display
+
+def empty_matrix(row, col):
+ matrix = []
+ for r in range(row):
+ matrix.append([])
+ for c in range(col):
+ matrix[r].append(0)
+ return matrix
+
+def increment_row(row, matrix):
+ for col in range(len(matrix[0])):
+ matrix[row][col] += 1
+ return matrix
+
+def increment_col(col, matrix):
+ for row in range(len(matrix)):
+ matrix[row][col] += 1
+ return matrix
+
+def count_odd(matrix):
+ count = 0
+ for row in range(len(matrix)):
+ for col in range(len(matrix[0])):
+ if matrix[row][col] % 2: count += 1
+ return count
+
+def odd_matrix(row, col, locations):
+ matrix = empty_matrix(row, col)
+ display = display_matrix("Initial:", matrix)
+ for row,col in locations:
+ display += f"\nApply [{row},{col}]:\n"
+
+ display += f"Increment row {row}:\n"
+ before = display_matrix("Before", matrix)
+ matrix = increment_row(row, matrix)
+ after = display_matrix("After", matrix)
+ display += display_two(before, after)
+
+ display += f"Increment col {col}:\n"
+ before = display_matrix("Before", matrix)
+ matrix = increment_col(col, matrix)
+ after = display_matrix("After", matrix)
+ display += display_two(before, after)
+ display += "\n"
+ return(
+ count_odd(matrix),
+ display + display_matrix('Final:', matrix)
+ )
+
+def int_join(joiner, arr):
+ return joiner.join(map(lambda i: str(i), arr))
+
+def solution(row, col, locations):
+ loc_list = ",".join([
+ f"[{int_join(',', x)}]" for x in locations
+ ])
+ print(
+ f'Input: $row = {row}, $col = {col}, ' +
+ f'@locations = ({loc_list})'
+ )
+ odd, explain = odd_matrix(row, col, locations)
+ print(f'Output: {odd}\n\n{explain}')
+
+
+print('Example 1:')
+solution(2, 3, [[0,1],[1,1]])
+
+print('\nExample 2:')
+solution(2, 2, [[1,1],[0,0]])
+
+print('\nExample 3:')
+solution(3, 3, [[0,0],[1,2],[2,1]])
+
+print('\nExample 4:')
+solution(1, 5, [[0,2],[0,4]])
+
+print('\nExample 5:')
+solution(4, 2, [[1,0],[3,1],[2,0],[0,1]])
diff --git a/challenge-337/packy-anderson/raku/ch-1.raku b/challenge-337/packy-anderson/raku/ch-1.raku
new file mode 100755
index 0000000000..123a1fdec5
--- /dev/null
+++ b/challenge-337/packy-anderson/raku/ch-1.raku
@@ -0,0 +1,32 @@
+#!/usr/bin/env raku
+use v6;
+
+sub smallerThan(@num1) {
+ my $bag = @num1.Bag;
+ my @out;
+ for 0..@num1.end -> $i {
+ @out[$i] = $bag{ $bag.keys.grep({ $_ <= @num1[$i] }) }.sum - 1;
+ }
+ @out
+}
+
+sub solution(@num1) {
+ say 'Input: @arr = (' ~ @num1.join(', ') ~ ')';
+ my @out = smallerThan(@num1);
+ say 'Output: (' ~ @out.join(', ') ~ ')';
+}
+
+say "Example 1:";
+solution([6, 5, 4, 8]);
+
+say "\nExample 2:";
+solution([7, 7, 7, 7]);
+
+say "\nExample 3:";
+solution([5, 4, 3, 2, 1]);
+
+say "\nExample 4:";
+solution([-1, 0, 3, -2, 1]);
+
+say "\nExample 5:";
+solution([0, 1, 1, 2, 0]);
diff --git a/challenge-337/packy-anderson/raku/ch-2.raku b/challenge-337/packy-anderson/raku/ch-2.raku
new file mode 100755
index 0000000000..f06b7f4abd
--- /dev/null
+++ b/challenge-337/packy-anderson/raku/ch-2.raku
@@ -0,0 +1,93 @@
+#!/usr/bin/env raku
+use v6;
+
+# take two displayMatrix() strings and display them side-by-side
+sub displayTwo($display1, $display2) {
+ # split the two strings and join each row together
+ my @display = $display1.split(/\n/) Z~ $display2.split(/\n/);
+ @display.join("\n")
+}
+
+sub displayMatrix($label, @matrix) {
+ my $width = (@matrix[0].elems + 2) * 2 + 1;
+ my $display = sprintf "%-*s\n", $width, $label;
+ for 0 .. @matrix.end -> $r {
+ my @row = @matrix[$r];
+ $display ~= "[ " ~ @row.join(" ") ~ " ] \n";
+ }
+ $display
+}
+
+sub emptyMatrix($row, $col) {
+ my @matrix;
+ for 1..$row -> $r { @matrix.push([ 0 xx $col ]); }
+ @matrix;
+}
+
+sub incrementRow($row, @matrix is copy) {
+ for 0..@matrix[0].end -> $col { @matrix[$row][$col]++; }
+ @matrix
+}
+
+sub incrementCol($col, @matrix is copy) {
+ for 0..@matrix.end -> $row { @matrix[$row][$col]++; }
+ @matrix
+}
+
+sub countOdd(@matrix) {
+ my $count = 0;
+ for 0..@matrix.end -> $row {
+ for 0..@matrix[0].end -> $col {
+ $count++ if @matrix[$row][$col] % 2;
+ }
+ }
+ $count
+}
+
+sub oddMatrix($row, $col, @locations) {
+ my @matrix = emptyMatrix($row, $col);
+ my $display = displayMatrix("Initial:", @matrix);
+ for @locations -> ($row, $col) {
+ $display ~= "\nApply [$row,$col]:\n";
+
+ $display ~= "Increment row $row:\n";
+ my $before = displayMatrix("Before", @matrix);
+ @matrix = incrementRow($row, @matrix);
+ my $after = displayMatrix("After", @matrix);
+ $display ~= displayTwo($before, $after);
+
+ $display ~= "Increment col $col:\n";
+ $before = displayMatrix("Before", @matrix);
+ @matrix = incrementCol($col, @matrix);
+ $after = displayMatrix("After", @matrix);
+ $display ~= displayTwo($before, $after);
+ }
+ $display ~= "\n";
+ return
+ countOdd(@matrix),
+ $display ~ displayMatrix('Final:', @matrix);
+}
+
+sub solution($row, $col, @locations) {
+ my $loc_list = @locations.map({ qq{[$_[0],$_[1]]} }).join(',');
+ say qq{Input: \$row = $row, \$col = $col, }
+ ~ qq{\@locations = ($loc_list)};
+ my ($odd, $explain) = oddMatrix($row, $col, @locations);
+ say "Output: $odd";
+ say "\n$explain";
+}
+
+say "Example 1:";
+solution(2, 3, [[0,1],[1,1]]);
+
+say "\nExample 2:";
+solution(2, 2, [[1,1],[0,0]]);
+
+say "\nExample 3:";
+solution(3, 3, [[0,0],[1,2],[2,1]]);
+
+say "\nExample 4:";
+solution(1, 5, [[0,2],[0,4]]);
+
+say "\nExample 5:";
+solution(4, 2, [[1,0],[3,1],[2,0],[0,1]]);