aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-05-11 21:47:56 +0100
committerGitHub <noreply@github.com>2025-05-11 21:47:56 +0100
commit662d4a445f6c15d2a584ae855f78cfb4c5690584 (patch)
treef3b16e77aaff0a6d90cf1f7dd2c7af8dc9bc7c35
parentebf7f9c27450b3412ac278454438d8f4e180f65a (diff)
parent098c34d31898bb4c3ae81b018eb462c64bc827ca (diff)
downloadperlweeklychallenge-club-662d4a445f6c15d2a584ae855f78cfb4c5690584.tar.gz
perlweeklychallenge-club-662d4a445f6c15d2a584ae855f78cfb4c5690584.tar.bz2
perlweeklychallenge-club-662d4a445f6c15d2a584ae855f78cfb4c5690584.zip
Merge pull request #12005 from packy/master
Challenge 320 solutions by Packy Anderson
-rw-r--r--challenge-320/packy-anderson/README.md2
-rw-r--r--challenge-320/packy-anderson/blog.txt1
-rwxr-xr-xchallenge-320/packy-anderson/elixir/ch-1.exs39
-rwxr-xr-xchallenge-320/packy-anderson/elixir/ch-2.exs40
-rwxr-xr-xchallenge-320/packy-anderson/perl/ch-1.pl37
-rwxr-xr-xchallenge-320/packy-anderson/perl/ch-2.pl34
-rwxr-xr-xchallenge-320/packy-anderson/python/ch-1.py34
-rwxr-xr-xchallenge-320/packy-anderson/python/ch-2.py33
-rwxr-xr-xchallenge-320/packy-anderson/raku/ch-1.raku35
-rwxr-xr-xchallenge-320/packy-anderson/raku/ch-2.raku32
10 files changed, 286 insertions, 1 deletions
diff --git a/challenge-320/packy-anderson/README.md b/challenge-320/packy-anderson/README.md
index ca6c64697e..9ffd00fb24 100644
--- a/challenge-320/packy-anderson/README.md
+++ b/challenge-320/packy-anderson/README.md
@@ -23,4 +23,4 @@
## Blog Post
-[Perl Weekly Challenge: Count the Minimum Common Word](https://packy.dardan.com/b/UD)
+[Perl Weekly Challenge: Happy Mother's Day](https://packy.dardan.com/b/UZ)
diff --git a/challenge-320/packy-anderson/blog.txt b/challenge-320/packy-anderson/blog.txt
new file mode 100644
index 0000000000..1219f85689
--- /dev/null
+++ b/challenge-320/packy-anderson/blog.txt
@@ -0,0 +1 @@
+https://packy.dardan.com/b/UZ \ No newline at end of file
diff --git a/challenge-320/packy-anderson/elixir/ch-1.exs b/challenge-320/packy-anderson/elixir/ch-1.exs
new file mode 100755
index 0000000000..03e1d1ccaf
--- /dev/null
+++ b/challenge-320/packy-anderson/elixir/ch-1.exs
@@ -0,0 +1,39 @@
+#!/usr/bin/env elixir
+
+defmodule PWC do
+ defp counter(int, {pos, neg}) do
+ {pos, neg} = cond do
+ int > 0 -> {pos+1, neg }
+ int < 0 -> {pos, neg+1}
+ true -> {pos, neg }
+ end
+ {int, {pos, neg}}
+ end
+
+ def maxCount(ints) do
+ {_, {pos, neg}} = Enum.map_reduce(ints, {0, 0}, &counter/2)
+ max = Enum.max([pos, neg])
+ {
+ max,
+ "There are #{pos} positive integers.\n" <>
+ "There are #{neg} negative integers.\n" <>
+ "The maximum between #{pos} and #{neg} is #{max}."
+ }
+ end
+
+ def solution(ints) do
+ IO.puts("Input: @ints = (" <> Enum.join(ints, ", ") <> ")")
+ {max, explain} = maxCount(ints)
+ IO.puts("Output: #{max}")
+ IO.puts("\n" <> explain)
+ end
+end
+
+IO.puts("Example 1:")
+PWC.solution([-3, -2, -1, 1, 2, 3])
+
+IO.puts("\nExample 2:")
+PWC.solution([-2, -1, 0, 0, 1])
+
+IO.puts("\nExample 3:")
+PWC.solution([1, 2, 3, 4])
diff --git a/challenge-320/packy-anderson/elixir/ch-2.exs b/challenge-320/packy-anderson/elixir/ch-2.exs
new file mode 100755
index 0000000000..a737304b75
--- /dev/null
+++ b/challenge-320/packy-anderson/elixir/ch-2.exs
@@ -0,0 +1,40 @@
+#!/usr/bin/env elixir
+
+defmodule PWC do
+ def int_join(ints, joiner), do: Enum.join(ints, joiner)
+
+ def subDiff(ints) do
+ element_sum = ints |> Enum.sum
+ digits = ints
+ |> Enum.join
+ |> String.codepoints
+ |> Enum.map(fn i -> String.to_integer(i) end)
+ digit_sum = digits |> Enum.sum
+ abs_diff = abs(element_sum - digit_sum)
+ int_list = int_join(ints, " + ")
+ digit_list = int_join(digits, " + ")
+ {
+ abs_diff,
+ "Element sum: #{int_list} => #{element_sum}\n" <>
+ "Digit sum: #{digit_list} => #{digit_sum}\n" <>
+ "Absolute difference: | #{element_sum} - #{digit_sum} |" <>
+ " => #{abs_diff}"
+ }
+ end
+
+ def solution(ints) do
+ IO.puts("Input: @ints = (" <> int_join(ints, ", ") <> ")")
+ {diff, explain} = subDiff(ints)
+ IO.puts("Output: " <> to_string(diff) )
+ IO.puts("\n" <> explain)
+ end
+end
+
+IO.puts("Example 1:")
+PWC.solution([1, 23, 4, 5])
+
+IO.puts("\nExample 2:")
+PWC.solution([1, 2, 3, 4, 5])
+
+IO.puts("\nExample 3:")
+PWC.solution([1, 2, 34])
diff --git a/challenge-320/packy-anderson/perl/ch-1.pl b/challenge-320/packy-anderson/perl/ch-1.pl
new file mode 100755
index 0000000000..6ca387a6b3
--- /dev/null
+++ b/challenge-320/packy-anderson/perl/ch-1.pl
@@ -0,0 +1,37 @@
+#!/usr/bin/env perl
+use v5.40;
+
+use List::AllUtils qw( max );
+
+sub maxCount(@ints) {
+ my ($pos, $neg) = (0, 0);
+ foreach my $int ( @ints ) {
+ if ($int > 0) {
+ $pos++;
+ }
+ elsif ($int < 0) {
+ $neg++;
+ }
+ }
+ my $max = max($pos, $neg);
+ my $explain =
+ "There are $pos positive integers.\n" .
+ "There are $neg negative integers.\n" .
+ "The maximum between $pos and $neg is $max.";
+ return ($max, $explain);
+}
+
+sub solution($ints) {
+ say 'Input: @ints = (' . join(', ', @$ints) . ')';
+ my ($max, $explain) = maxCount(@$ints);
+ say "Output: $max\n\n$explain";
+}
+
+say "Example 1:";
+solution([-3, -2, -1, 1, 2, 3]);
+
+say "\nExample 2:";
+solution([-2, -1, 0, 0, 1]);
+
+say "\nExample 3:";
+solution([1, 2, 3, 4]);
diff --git a/challenge-320/packy-anderson/perl/ch-2.pl b/challenge-320/packy-anderson/perl/ch-2.pl
new file mode 100755
index 0000000000..7246940746
--- /dev/null
+++ b/challenge-320/packy-anderson/perl/ch-2.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/env perl
+use v5.40;
+
+use List::AllUtils qw( sum );
+
+sub subDiff(@ints) {
+ my $element_sum = sum @ints;
+ my $digit_sum = sum split(//, join(q{}, @ints));
+ my $abs_diff = abs($element_sum - $digit_sum);
+ my $int_list = join " + ", @ints;
+ my $digit_list = join " + ", split(//, join(q{}, @ints));
+ return (
+ $abs_diff,
+ "Element sum: $int_list => $element_sum\n" .
+ "Digit sum: $digit_list => $digit_sum\n" .
+ "Absolute difference: | $element_sum - $digit_sum | " .
+ "=> $abs_diff"
+ );
+}
+
+sub solution($ints) {
+ say 'Input: @ints = (' . join(', ', @$ints) . ')';
+ my ($diff, $explain) = subDiff(@$ints);
+ say "Output: $diff\n\n$explain";
+}
+
+say "Example 1:";
+solution([1, 23, 4, 5]);
+
+say "\nExample 2:";
+solution([1, 2, 3, 4, 5]);
+
+say "\nExample 3:";
+solution([1, 2, 34]);
diff --git a/challenge-320/packy-anderson/python/ch-1.py b/challenge-320/packy-anderson/python/ch-1.py
new file mode 100755
index 0000000000..8e103a1509
--- /dev/null
+++ b/challenge-320/packy-anderson/python/ch-1.py
@@ -0,0 +1,34 @@
+#!/usr/bin/env python
+
+def maxCount(ints):
+ pos = 0
+ neg = 0
+ for int in ints:
+ if int > 0:
+ pos += 1
+ elif int < 0:
+ neg +=1
+ max_count = max(pos, neg)
+ explain = (
+ f"There are {pos} positive integers.\n" +
+ f"There are {neg} negative integers.\n" +
+ f"The maximum between {pos} and {neg} is {max_count}."
+ )
+ return max_count, explain
+
+def comma_join(arr):
+ return ', '.join(map(lambda i: str(i), arr))
+
+def solution(ints):
+ print(f'Input: @ints = ({comma_join(ints)})')
+ max_count, explain = maxCount(ints)
+ print(f'Output: {max_count}\n\n{explain}')
+
+print('Example 1:')
+solution([-3, -2, -1, 1, 2, 3])
+
+print('\nExample 2:')
+solution([-2, -1, 0, 0, 1])
+
+print('\nExample 3:')
+solution([1, 2, 3, 4])
diff --git a/challenge-320/packy-anderson/python/ch-2.py b/challenge-320/packy-anderson/python/ch-2.py
new file mode 100755
index 0000000000..07dfbd812c
--- /dev/null
+++ b/challenge-320/packy-anderson/python/ch-2.py
@@ -0,0 +1,33 @@
+#!/usr/bin/env python
+
+def int_join(joiner, arr):
+ return joiner.join(map(lambda i: str(i), arr))
+
+def subDiff(ints):
+ element_sum = sum(ints)
+ digits = [ int(i) for i in list(int_join("", ints)) ]
+ digit_sum = sum(digits)
+ abs_diff = abs(element_sum - digit_sum)
+ int_list = int_join(" + ", ints)
+ digit_list = int_join(" + ", digits)
+ return (
+ abs_diff,
+ f"Element sum: {int_list} => {element_sum}\n" +
+ f"Digit sum: {digit_list} => {digit_sum}\n" +
+ f"Absolute difference: | {element_sum} - {digit_sum} | " +
+ f"=> {abs_diff}"
+ )
+
+def solution(ints):
+ print(f'Input: @ints = ({int_join(", ", ints)})')
+ diff, explain = subDiff(ints)
+ print(f'Output: {diff}\n\n{explain}')
+
+print('Example 1:')
+solution([1, 23, 4, 5])
+
+print('\nExample 2:')
+solution([1, 2, 3, 4, 5])
+
+print('\nExample 3:')
+solution([1, 2, 34])
diff --git a/challenge-320/packy-anderson/raku/ch-1.raku b/challenge-320/packy-anderson/raku/ch-1.raku
new file mode 100755
index 0000000000..1da1d02d29
--- /dev/null
+++ b/challenge-320/packy-anderson/raku/ch-1.raku
@@ -0,0 +1,35 @@
+#!/usr/bin/env raku
+use v6;
+
+sub maxCount(@ints) {
+ my ($pos, $neg) = (0, 0);
+ for @ints -> $int {
+ if ($int > 0) {
+ $pos++;
+ }
+ elsif ($int < 0) {
+ $neg++;
+ }
+ }
+ my $max = ($pos, $neg).max;
+ my $explain =
+ "There are $pos positive integers.\n" ~
+ "There are $neg negative integers.\n" ~
+ "The maximum between $pos and $neg is $max.";
+ return ($max, $explain);
+}
+
+sub solution(@ints) {
+ say 'Input: @ints = (' ~ @ints.join(', ') ~ ')';
+ my ($max, $explain) = maxCount(@ints);
+ say "Output: $max\n\n$explain";
+}
+
+say "Example 1:";
+solution([-3, -2, -1, 1, 2, 3]);
+
+say "\nExample 2:";
+solution([-2, -1, 0, 0, 1]);
+
+say "\nExample 3:";
+solution([1, 2, 3, 4]);
diff --git a/challenge-320/packy-anderson/raku/ch-2.raku b/challenge-320/packy-anderson/raku/ch-2.raku
new file mode 100755
index 0000000000..c1cf067afd
--- /dev/null
+++ b/challenge-320/packy-anderson/raku/ch-2.raku
@@ -0,0 +1,32 @@
+#!/usr/bin/env raku
+use v6;
+
+sub subDiff(@ints) {
+ my $element_sum = @ints.sum;
+ my $digit_sum = @ints.join.comb.sum;
+ my $abs_diff = ($element_sum - $digit_sum).abs;
+ my $int_list = @ints.join(" + ");
+ my $digit_list = @ints.join.comb.join(" + ");
+ return (
+ $abs_diff,
+ "Element sum: $int_list => $element_sum\n" ~
+ "Digit sum: $digit_list => $digit_sum\n" ~
+ "Absolute difference: | $element_sum - $digit_sum | " ~
+ "=> $abs_diff"
+ );
+}
+
+sub solution(@ints) {
+ say 'Input: @ints = (' ~ @ints.join(', ') ~ ')';
+ my ($diff, $explain) = subDiff(@ints);
+ say "Output: $diff\n\n$explain";
+}
+
+say "Example 1:";
+solution([1, 23, 4, 5]);
+
+say "\nExample 2:";
+solution([1, 2, 3, 4, 5]);
+
+say "\nExample 3:";
+solution([1, 2, 34]);