aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-08-20 08:12:02 +0100
committerGitHub <noreply@github.com>2024-08-20 08:12:02 +0100
commitc9e2021e40366f295331ec7c4b2be2be876e6eaa (patch)
tree4514553f20d642976fa59ef97d483fe9091a75a9
parent70acc6b56322e68ed9b4550fd6e66f6c5c9353e9 (diff)
parent5b21193086ff20b64b19f33112cd57a2a3a0077b (diff)
downloadperlweeklychallenge-club-c9e2021e40366f295331ec7c4b2be2be876e6eaa.tar.gz
perlweeklychallenge-club-c9e2021e40366f295331ec7c4b2be2be876e6eaa.tar.bz2
perlweeklychallenge-club-c9e2021e40366f295331ec7c4b2be2be876e6eaa.zip
Merge pull request #10669 from packy/master
Challenge 283 solutions by Packy Anderson
-rw-r--r--challenge-283/packy-anderson/README.md2
-rw-r--r--challenge-283/packy-anderson/blog.txt1
-rwxr-xr-xchallenge-283/packy-anderson/elixir/ch-1.exs64
-rwxr-xr-xchallenge-283/packy-anderson/elixir/ch-2.exs77
-rwxr-xr-xchallenge-283/packy-anderson/perl/ch-1.pl44
-rwxr-xr-xchallenge-283/packy-anderson/perl/ch-2.pl46
-rwxr-xr-xchallenge-283/packy-anderson/python/ch-1.py45
-rwxr-xr-xchallenge-283/packy-anderson/python/ch-2.py50
-rwxr-xr-xchallenge-283/packy-anderson/raku/ch-1.raku41
-rwxr-xr-xchallenge-283/packy-anderson/raku/ch-2.raku47
10 files changed, 416 insertions, 1 deletions
diff --git a/challenge-283/packy-anderson/README.md b/challenge-283/packy-anderson/README.md
index 014753ac41..19e155117f 100644
--- a/challenge-283/packy-anderson/README.md
+++ b/challenge-283/packy-anderson/README.md
@@ -22,4 +22,4 @@
## Blog Post
-[Perl Weekly Challenge: Ch-ch-ch-changes!](https://packy.dardan.com/b/Q8)
+[Perl Weekly Challenge: Bag! Bag! Bag!](https://packy.dardan.com/b/QM)
diff --git a/challenge-283/packy-anderson/blog.txt b/challenge-283/packy-anderson/blog.txt
new file mode 100644
index 0000000000..7c05ead268
--- /dev/null
+++ b/challenge-283/packy-anderson/blog.txt
@@ -0,0 +1 @@
+https://packy.dardan.com/b/QM \ No newline at end of file
diff --git a/challenge-283/packy-anderson/elixir/ch-1.exs b/challenge-283/packy-anderson/elixir/ch-1.exs
new file mode 100755
index 0000000000..98c5df8b92
--- /dev/null
+++ b/challenge-283/packy-anderson/elixir/ch-1.exs
@@ -0,0 +1,64 @@
+#!/usr/bin/env elixir
+
+defmodule PWC do
+ @doc """
+ Helper function that accepts a list and generates a Multiset/
+ Bag implemented as a Map.
+
+ https://en.wikipedia.org/wiki/Multiset
+ """
+ def makeBag(list) do
+ {_, bag} = Enum.map_reduce(list, %{}, fn i, bag ->
+ { i, Map.put(bag, i, Map.get(bag, i, 0) + 1) }
+ end)
+ bag
+ end
+
+ @doc """
+ You are given an array of integers where every element
+ appears more than once except one element.
+
+ Find the one element that appears exactly one time.
+ """
+ def uniqueNumber(ints) do
+ keys = ints
+ |> makeBag
+ |> Map.filter(fn {_, v} -> v == 1 end) # keys that occur once
+ |> Map.keys # return just the keys from the map
+ cond do
+ Kernel.length(keys) == 0 ->
+ "no element appears only once"
+ Kernel.length(keys) > 1 ->
+ "multiple elements appear only once"
+ true ->
+ keys |> List.first |> to_string
+ end
+ end
+
+ @doc """
+ Function to print the input, invoke the solution code,
+ and print the output.
+ """
+ def solution(ints) do
+ IO.puts("Input: @ints = (" <> Enum.join(ints, ", ") <> ")")
+ IO.puts("Output: " <> uniqueNumber(ints) )
+ end
+end
+
+IO.puts("Example 1:")
+PWC.solution([3, 3, 1])
+
+IO.puts("\nExample 2:")
+PWC.solution([3, 2, 4, 2, 4])
+
+IO.puts("\nExample 3:")
+PWC.solution([1])
+
+IO.puts("\nExample 4:")
+PWC.solution([4, 3, 1, 1, 1, 4])
+
+IO.puts("\nInvalid Input 1 (no element appears only once):")
+PWC.solution([4, 1, 1, 1, 4])
+
+IO.puts("\nInvalid Input 2 (multiple elements appear only once):")
+PWC.solution([1, 2, 3, 4])
diff --git a/challenge-283/packy-anderson/elixir/ch-2.exs b/challenge-283/packy-anderson/elixir/ch-2.exs
new file mode 100755
index 0000000000..82b3fd72ac
--- /dev/null
+++ b/challenge-283/packy-anderson/elixir/ch-2.exs
@@ -0,0 +1,77 @@
+#!/usr/bin/env elixir
+
+defmodule PWC do
+ @doc """
+ Helper function that accepts a list and generates a Multiset/
+ Bag implemented as a Map.
+
+ https://en.wikipedia.org/wiki/Multiset
+ """
+ def makeBag(list) do
+ {_, bag} = Enum.map_reduce(list, %{}, fn i, bag ->
+ { i, Map.put(bag, i, Map.get(bag, i, 0) + 1) }
+ end)
+ bag
+ end
+
+ @doc """
+ Return true if for every index i in the range 0 <= i < size
+ of array, the digit i occurs exactly the $ints[$i] times in
+ the given array, otherwise return false.
+ """
+ def digitCountValue(ints) do
+ bag = makeBag(ints)
+ results = Enum.map(0 .. length(ints)-1, fn i ->
+ # return a tuple of {digit, occurrences, expected}
+ { i, Map.get(bag, i, 0), Enum.at(ints, i) }
+ end)
+ explain = Enum.map(results, fn tuple ->
+ digit = elem(tuple, 0)
+ occurs = elem(tuple, 1)
+ times = elem(tuple, 2)
+ otimes = cond do
+ occurs == 0 -> "0 times"
+ occurs >= 2 -> "#{occurs} times"
+ true -> "1 time"
+ end
+ ttimes = cond do
+ times == 0 -> "0 times"
+ times >= 2 -> "#{times} times"
+ true -> "1 time"
+ end
+ if occurs == times do
+ "$ints[#{digit}] = #{times}, the digit #{digit} " <>
+ "occurs #{otimes}"
+ else
+ "$ints[#{digit}] = #{times}, the digit #{digit} " <>
+ "occurs #{otimes} rather than #{ttimes}"
+ end
+ end)
+ |> Enum.join("\n")
+ passes = Enum.all?(results, fn tuple ->
+ elem(tuple, 1) == elem(tuple, 2)
+ end)
+ |> to_string
+ {passes, explain}
+ end
+
+ @doc """
+ Function to print the input, invoke the solution code,
+ and print the output.
+ """
+ def solution(ints) do
+ IO.puts("Input: @ints = (" <> Enum.join(ints, ", ") <> ")")
+ {passes, explain} = digitCountValue(ints)
+ IO.puts("Output: " <> passes)
+ IO.puts("\n" <> explain)
+ end
+end
+
+IO.puts("Example 1:")
+PWC.solution([1, 2, 1, 0])
+
+IO.puts("\nExample 2:")
+PWC.solution([0, 3, 0])
+
+IO.puts("\nExample 3:")
+PWC.solution([0, 1, 2, 2])
diff --git a/challenge-283/packy-anderson/perl/ch-1.pl b/challenge-283/packy-anderson/perl/ch-1.pl
new file mode 100755
index 0000000000..0017c4c1bc
--- /dev/null
+++ b/challenge-283/packy-anderson/perl/ch-1.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/env perl
+use v5.40;
+
+sub uniqueNumber(@ints) {
+ my %bag;
+ grep { $bag{$_}++ } @ints; # count occurrences
+ my @keys;
+ foreach my($key, $value) (%bag) { # get key/value pairs
+ next unless $value == 1; # just keys that occur once
+ push @keys, $key; # return just the keys
+ }
+
+ # invalid input
+ return 'no element appears only once'
+ if @keys == 0;
+ return 'multiple elements appear only once'
+ if @keys > 1;
+
+ # return the one value
+ return shift @keys;
+}
+
+sub solution($ints) {
+ say 'Input: @ints = (' . join(', ', @$ints) . ')';
+ say 'Output: ' . uniqueNumber(@$ints);
+}
+
+say "Example 1:";
+solution([3, 3, 1]);
+
+say "\nExample 2:";
+solution([3, 2, 4, 2, 4]);
+
+say "\nExample 3:";
+solution([1]);
+
+say "\nExample 4:";
+solution([4, 3, 1, 1, 1, 4]);
+
+say "\nInvalid Input 1 (no element appears only once):";
+solution([4, 1, 1, 1, 4]);
+
+say "\nInvalid Input 2 (multiple elements appear only once):";
+solution([1, 2, 3, 4]);
diff --git a/challenge-283/packy-anderson/perl/ch-2.pl b/challenge-283/packy-anderson/perl/ch-2.pl
new file mode 100755
index 0000000000..72c6bc41c2
--- /dev/null
+++ b/challenge-283/packy-anderson/perl/ch-2.pl
@@ -0,0 +1,46 @@
+#!/usr/bin/env perl
+use v5.40;
+
+sub digitCountValue(@ints) {
+ my %bag;
+ grep { $bag{$_}++ } @ints; # count occurrences
+ my @explain;
+ my $passes = 1;
+ foreach my $digit ( 0 .. $#ints ) {
+ my $times = $ints[$digit];
+ my $occurs = $bag{$digit} // 0;
+ my $otimes = ($occurs == 0) ? "0 times"
+ : ($occurs >= 2) ? "$occurs times"
+ : "1 time";
+ if ($times == $occurs) {
+ push @explain,
+ "\$ints[$digit] = $times, the digit $digit " .
+ "occurs $otimes";
+ }
+ else {
+ $passes = 0;
+ my $ttimes = ($times == 0) ? "0 times"
+ : ($times >= 2) ? "$times times"
+ : "1 time";
+ push @explain,
+ "\$ints[$digit] = $times, the digit $digit " .
+ "occurs $otimes rather than $ttimes";
+ }
+ }
+ return $passes ? 'True' : 'False', join("\n", @explain);
+}
+
+sub solution($ints) {
+ say 'Input: @ints = (' . join(', ', @$ints) . ')';
+ my ($passes, $explain) = digitCountValue(@$ints);
+ say "Output: $passes\n\n$explain";
+}
+
+say "Example 1:";
+solution([1, 2, 1, 0]);
+
+say "\nExample 2:";
+solution([0, 3, 0]);
+
+say "\nExample 3:";
+solution([0, 1, 2, 2]);
diff --git a/challenge-283/packy-anderson/python/ch-1.py b/challenge-283/packy-anderson/python/ch-1.py
new file mode 100755
index 0000000000..dde2091f5c
--- /dev/null
+++ b/challenge-283/packy-anderson/python/ch-1.py
@@ -0,0 +1,45 @@
+#!/usr/bin/env python
+
+from collections import Counter
+
+def uniqueNumber(ints):
+ bag = Counter(ints) # count occurrences
+ keys = [
+ k # return just the keys
+ for k,v in bag.items() # get key/value pairs
+ if v == 1 # keys that occur once
+ ]
+
+ # invalid input
+ if len(keys) == 0:
+ return 'no element appears only once'
+ if len(keys) > 1:
+ return 'multiple elements appear only once'
+
+ # return the one value
+ return keys.pop(0)
+
+def comma_join(arr):
+ return ', '.join(map(lambda i: str(i), arr))
+
+def solution(ints):
+ print(f'Input: @ints = ({comma_join(ints)})')
+ print(f'Output: {uniqueNumber(ints)}')
+
+print('Example 1:')
+solution([3, 3, 1])
+
+print('\nExample 2:')
+solution([3, 2, 4, 2, 4])
+
+print('\nExample 3:')
+solution([1])
+
+print('\nExample 4:')
+solution([4, 3, 1, 1, 1, 4])
+
+print('\nInvalid Input 1 (no element appears only once):')
+solution([4, 1, 1, 1, 4])
+
+print('\nInvalid Input 2 (multiple elements appear only once):')
+solution([1, 2, 3, 4])
diff --git a/challenge-283/packy-anderson/python/ch-2.py b/challenge-283/packy-anderson/python/ch-2.py
new file mode 100755
index 0000000000..e8ccd8b72c
--- /dev/null
+++ b/challenge-283/packy-anderson/python/ch-2.py
@@ -0,0 +1,50 @@
+#!/usr/bin/env python
+
+from collections import Counter
+
+def digitCountValue(ints):
+ bag = Counter(ints) # count occurrences
+ explain = []
+ passes = True
+ for digit in range(len(ints)):
+ times = ints[digit]
+ occurs = bag[digit]
+ otimes = (
+ "0 times" if occurs == 0 else
+ f"{occurs} times" if occurs >= 2 else
+ "1 time"
+ )
+ if times == occurs:
+ explain.append(
+ f"$ints[{digit}] = {times}, the digit {digit} " +
+ f"occurs {otimes}"
+ )
+ else:
+ passes = False
+ ttimes = (
+ "0 times" if times == 0 else
+ f"{times} times" if times >= 2 else
+ "1 time"
+ )
+ explain.append(
+ f"$ints[{digit}] = {times}, the digit {digit} " +
+ f"occurs {otimes} rather than {ttimes}"
+ )
+ return passes, "\n".join(explain)
+
+def comma_join(arr):
+ return ', '.join(map(lambda i: str(i), arr))
+
+def solution(ints):
+ print(f'Input: @ints = ({comma_join(ints)})')
+ passes, explain = digitCountValue(ints)
+ print(f'Output: {passes}\n\n{explain}')
+
+print('Example 1:')
+solution([1, 2, 1, 0])
+
+print('\nExample 2:')
+solution([0, 3, 0])
+
+print('\nExample 3:')
+solution([0, 1, 2, 2])
diff --git a/challenge-283/packy-anderson/raku/ch-1.raku b/challenge-283/packy-anderson/raku/ch-1.raku
new file mode 100755
index 0000000000..14b303e558
--- /dev/null
+++ b/challenge-283/packy-anderson/raku/ch-1.raku
@@ -0,0 +1,41 @@
+#!/usr/bin/env raku
+use v6;
+
+sub uniqueNumber(@ints) {
+ my @keys = @ints.Bag # count occurrences
+ .pairs # get key/value pairs
+ .grep({ .value == 1 }) # keys that occur once
+ .map({ .key }); # return just the keys
+
+ # invalid input
+ return 'no element appears only once'
+ if @keys == 0;
+ return 'multiple elements appear only once'
+ if @keys > 1;
+
+ # return the one value
+ return @keys.shift;
+}
+
+sub solution(@ints) {
+ say 'Input: @ints = (' ~ @ints.join(', ') ~ ')';
+ say 'Output: ' ~ uniqueNumber(@ints);
+}
+
+say "Example 1:";
+solution([3, 3, 1]);
+
+say "\nExample 2:";
+solution([3, 2, 4, 2, 4]);
+
+say "\nExample 3:";
+solution([1]);
+
+say "\nExample 4:";
+solution([4, 3, 1, 1, 1, 4]);
+
+say "\nInvalid Input 1 (no element appears only once):";
+solution([4, 1, 1, 1, 4]);
+
+say "\nInvalid Input 2 (multiple elements appear only once):";
+solution([1, 2, 3, 4]);
diff --git a/challenge-283/packy-anderson/raku/ch-2.raku b/challenge-283/packy-anderson/raku/ch-2.raku
new file mode 100755
index 0000000000..753dda8751
--- /dev/null
+++ b/challenge-283/packy-anderson/raku/ch-2.raku
@@ -0,0 +1,47 @@
+#!/usr/bin/env raku
+use v6;
+
+sub digitCountValue(@ints) {
+ my %bag = @ints.Bag; # count occurrences
+ my @explain;
+ my $passes = True;
+ for 0 .. @ints.end -> $digit {
+ my $times = @ints[$digit];
+ my $occurs = %bag{$digit} // 0;
+ my $otimes = ($occurs == 0) ?? "0 times"
+ !! ($occurs >= 2) ?? "$occurs times"
+ !! "1 time";
+ if ($times == $occurs) {
+ @explain.push(
+ "\$ints[$digit] = $times, the digit $digit " ~
+ "occurs $otimes"
+ );
+ }
+ else {
+ $passes = False;
+ my $ttimes = ($times == 0) ?? "0 times"
+ !! ($times >= 2) ?? "$times times"
+ !! "1 time";
+ @explain.push(
+ "\$ints[$digit] = $times, the digit $digit " ~
+ "occurs $otimes rather than $ttimes"
+ );
+ }
+ }
+ return $passes, @explain.join("\n");
+}
+
+sub solution(@ints) {
+ say 'Input: @ints = (' ~ @ints.join(', ') ~ ')';
+ my ($passes, $explain) = digitCountValue(@ints);
+ say "Output: $passes\n\n$explain";
+}
+
+say "Example 1:";
+solution([1, 2, 1, 0]);
+
+say "\nExample 2:";
+solution([0, 3, 0]);
+
+say "\nExample 3:";
+solution([0, 1, 2, 2]);