aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-277/packy-anderson/README.md2
-rw-r--r--challenge-277/packy-anderson/blog.txt1
-rwxr-xr-xchallenge-277/packy-anderson/elixir/ch-1.exs82
-rwxr-xr-xchallenge-277/packy-anderson/elixir/ch-2.exs37
-rwxr-xr-xchallenge-277/packy-anderson/perl/ch-1.pl36
-rwxr-xr-xchallenge-277/packy-anderson/perl/ch-2.pl33
-rwxr-xr-xchallenge-277/packy-anderson/python/ch-1.py36
-rwxr-xr-xchallenge-277/packy-anderson/python/ch-2.py27
-rwxr-xr-xchallenge-277/packy-anderson/raku/ch-1.raku34
-rwxr-xr-xchallenge-277/packy-anderson/raku/ch-2.raku31
10 files changed, 318 insertions, 1 deletions
diff --git a/challenge-277/packy-anderson/README.md b/challenge-277/packy-anderson/README.md
index 1c88177fb7..84812d2a96 100644
--- a/challenge-277/packy-anderson/README.md
+++ b/challenge-277/packy-anderson/README.md
@@ -22,4 +22,4 @@
## Blog Post
-[Complete Maximum Day Frequency](https://packy.dardan.com/b/NT)
+["They call me the count, because I love to count pairs! Ah, ah, ah!"](https://wp.me/p2aV3x-m0)
diff --git a/challenge-277/packy-anderson/blog.txt b/challenge-277/packy-anderson/blog.txt
new file mode 100644
index 0000000000..bfa6bd269f
--- /dev/null
+++ b/challenge-277/packy-anderson/blog.txt
@@ -0,0 +1 @@
+https://wp.me/p2aV3x-m0 \ No newline at end of file
diff --git a/challenge-277/packy-anderson/elixir/ch-1.exs b/challenge-277/packy-anderson/elixir/ch-1.exs
new file mode 100755
index 0000000000..2596d6539c
--- /dev/null
+++ b/challenge-277/packy-anderson/elixir/ch-1.exs
@@ -0,0 +1,82 @@
+#!/usr/bin/env elixir
+
+defmodule Multiset do
+ @opaque t :: %Multiset{map: %{value => non_neg_integer}}
+ @type value :: term
+ defstruct map: %{}, size: 0
+
+ @spec new :: t
+ def new(), do: %Multiset{}
+
+ @spec new(Enum.t) :: t
+ def new(enumerable), do: Enum.reduce(enumerable, %Multiset{}, &put(&2, &1))
+
+ @spec put(t, value, integer) :: t
+ def put(multiset, value, k \\ 1)
+ def put(%Multiset{map: map, size: size} = multiset, value, k) do
+ if k < 1 do
+ multiset
+ else
+ new_map = Map.update(map, value, k, fn multiplicity -> multiplicity + k end)
+ new_size = size + k
+ %{multiset | map: new_map, size: new_size}
+ end
+ end
+
+ @spec values(t) :: MapSet.t
+ def values(multiset)
+ def values(%Multiset{map: map}), do: map |> Map.keys |> MapSet.new
+
+ @spec multiplicity(t, value) :: non_neg_integer
+ def multiplicity(multiset, value)
+ def multiplicity(%Multiset{map: map}, value), do: Map.get(map, value, 0)
+end
+defmodule PWC do
+ def countCommon(words1, words2) do
+ # make Multisets that count the words
+ counts1 = words1
+ |> Enum.map(fn w -> String.downcase(w) end)
+ |> Multiset.new
+ counts2 = words2
+ |> Enum.map(fn w -> String.downcase(w) end)
+ |> Multiset.new
+
+ # filter to get set of words occuring only once
+ counts1 = counts1
+ |> Multiset.values
+ |> Enum.filter(fn w -> Multiset.multiplicity(counts1, w) == 1 end)
+ |> MapSet.new
+ counts2 = counts2
+ |> Multiset.values
+ |> Enum.filter(fn w -> Multiset.multiplicity(counts2, w) == 1 end)
+ |> MapSet.new
+
+ # find the elements common in both
+ common = MapSet.intersection(counts1, counts2)
+ MapSet.size(common)
+ end
+
+ def solution(words1, words2) do
+ IO.puts("Input: @words1 = (\"" <> Enum.join(words1, "\", \"") <> "\")")
+ IO.puts(" @words2 = (\"" <> Enum.join(words2, "\", \"") <> "\")")
+ IO.puts("Output: " <> to_string(countCommon(words1, words2)) )
+ end
+end
+
+IO.puts("Example 1:")
+PWC.solution(
+ ["Perl", "is", "my", "friend"],
+ ["Perl", "and", "Raku", "are", "friend"]
+)
+
+IO.puts("\nExample 2:")
+PWC.solution(
+ ["Perl", "and", "Python", "are", "very", "similar"],
+ ["Python", "is", "top", "in", "guest", "languages"]
+)
+
+IO.puts("\nExample 3:")
+PWC.solution(
+ ["Perl", "is", "imperative", "Lisp", "is", "functional"],
+ ["Crystal", "is", "similar", "to", "Ruby"]
+)
diff --git a/challenge-277/packy-anderson/elixir/ch-2.exs b/challenge-277/packy-anderson/elixir/ch-2.exs
new file mode 100755
index 0000000000..4d48b0f99d
--- /dev/null
+++ b/challenge-277/packy-anderson/elixir/ch-2.exs
@@ -0,0 +1,37 @@
+#!/usr/bin/env elixir
+
+defmodule PWC do
+ def isStrongPair(x, y) do
+ diff = abs(x - y)
+ 0 < diff and diff < min(x, y)
+ end
+
+ def findStrongPairs([], strong), do: strong
+
+ def findStrongPairs([x | rest], strong) do
+ strongY = Enum.filter(rest, fn y -> isStrongPair(x, y) end)
+ strong = strong ++ Enum.map(strongY, fn y -> "(#{x}, #{y})" end)
+ findStrongPairs(rest, strong)
+ end
+
+ def strongPairs(ints) do
+ ints = ints
+ |> Enum.uniq
+ |> Enum.sort
+ strong = findStrongPairs(ints, [])
+ { length(strong), Enum.join(strong, ", ") }
+ end
+
+ def solution(ints) do
+ IO.puts("Input: @ints = (" <> Enum.join(ints, ", ") <> ")")
+ {count, explain} = strongPairs(ints)
+ IO.puts("Output: " <> to_string(count) )
+ IO.puts("\nStrong Pairs: " <> explain)
+ end
+end
+
+IO.puts("Example 1:")
+PWC.solution([1, 2, 3, 4, 5])
+
+IO.puts("\nExample 2:")
+PWC.solution([5, 7, 1, 7])
diff --git a/challenge-277/packy-anderson/perl/ch-1.pl b/challenge-277/packy-anderson/perl/ch-1.pl
new file mode 100755
index 0000000000..488bc30eab
--- /dev/null
+++ b/challenge-277/packy-anderson/perl/ch-1.pl
@@ -0,0 +1,36 @@
+#!/usr/bin/env perl
+use v5.40;
+
+use Set::Intersection;
+
+sub countCommon($words1, $words2) {
+ # make Bags that count the words
+ my %counts1; map { $counts1{lc $_}++ } @$words1;
+ my %counts2; map { $counts2{lc $_}++ } @$words2;
+
+ # filter to get set of words occuring only once
+ my @counts1 = grep { $counts1{$_} == 1 } keys %counts1;
+ my @counts2 = grep { $counts2{$_} == 1 } keys %counts2;
+
+ # find the elements common in both
+ my @common = get_intersection(\@counts1, \@counts2);
+ return scalar(@common);
+}
+
+sub solution($words1, $words2) {
+ say 'Input: @words1 = (' . join(', ', map { qq{"$_"} } @$words1) . ')';
+ say ' @words2 = (' . join(', ', map { qq{"$_"} } @$words2) . ')';
+ say 'Output: ' . countCommon($words1, $words2);
+}
+
+say "Example 1:";
+solution(["Perl", "is", "my", "friend"],
+ ["Perl", "and", "Raku", "are", "friend"]);
+
+say "\nExample 2:";
+solution(["Perl", "and", "Python", "are", "very", "similar"],
+ ["Python", "is", "top", "in", "guest", "languages"]);
+
+say "\nExample 3:";
+solution(["Perl", "is", "imperative", "Lisp", "is", "functional"],
+ ["Crystal", "is", "similar", "to", "Ruby"]);
diff --git a/challenge-277/packy-anderson/perl/ch-2.pl b/challenge-277/packy-anderson/perl/ch-2.pl
new file mode 100755
index 0000000000..ef31ae94f9
--- /dev/null
+++ b/challenge-277/packy-anderson/perl/ch-2.pl
@@ -0,0 +1,33 @@
+#!/usr/bin/env perl
+use v5.40;
+
+use List::Util qw( min uniq );
+
+sub isStrongPair($x, $y) {
+ return 0 < abs($x - $y) < min($x, $y);
+}
+
+sub strongPairs(@ints) {
+ my @strong;
+ my @ints2 = uniq sort @ints;
+ foreach my $x ( @ints2 ) {
+ foreach my $y ( @ints2 ) {
+ next if $x > $y;
+ next unless isStrongPair($x, $y);
+ push @strong, "($x, $y)";
+ }
+ }
+ return scalar(@strong), join(', ', @strong);
+}
+
+sub solution(@ints) {
+ say 'Input: @ints = (' . @ints.join(', ') . ')';
+ my ($count, $explain) = strongPairs(@ints);
+ say "Output: $count\n\nStrong Pairs: $explain";
+}
+
+say "Example 1:";
+solution(1, 2, 3, 4, 5);
+
+say "\nExample 2:";
+solution(5, 7, 1, 7);
diff --git a/challenge-277/packy-anderson/python/ch-1.py b/challenge-277/packy-anderson/python/ch-1.py
new file mode 100755
index 0000000000..59bef5bdec
--- /dev/null
+++ b/challenge-277/packy-anderson/python/ch-1.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env python
+
+from collections import Counter
+
+def countCommon(words1, words2):
+ # make Counters that count the words
+ counts1 = Counter([ w.lower() for w in words1 ])
+ counts2 = Counter([ w.lower() for w in words2 ])
+
+ # filter to get set of words occuring only once
+ counts1 = set([ w for w in counts1.keys() if counts1[w] == 1 ])
+ counts2 = set([ w for w in counts2.keys() if counts2[w] == 1 ])
+
+ # find the elements common in both
+ common = counts1 & counts2
+ return len(common)
+
+def quoted_join(arr):
+ return ', '.join(map(lambda i: f'"{i}"', arr))
+
+def solution(words1, words2):
+ print(f'Input: @words1 == ({quoted_join(words1)})')
+ print(f' @words2 == ({quoted_join(words2)})')
+ print(f'Output: {countCommon(words1, words2)}')
+
+print('Example 1:')
+solution(["Perl", "is", "my", "friend"],
+ ["Perl", "and", "Raku", "are", "friend"])
+
+print('\nExample 2:')
+solution(["Perl", "and", "Python", "are", "very", "similar"],
+ ["Python", "is", "top", "in", "guest", "languages"])
+
+print('\nExample 3:')
+solution(["Perl", "is", "imperative", "Lisp", "is", "functional"],
+ ["Crystal", "is", "similar", "to", "Ruby"])
diff --git a/challenge-277/packy-anderson/python/ch-2.py b/challenge-277/packy-anderson/python/ch-2.py
new file mode 100755
index 0000000000..44097c430c
--- /dev/null
+++ b/challenge-277/packy-anderson/python/ch-2.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python
+
+def isStrongPair(x, y):
+ return( 0 < abs(x - y) < min(x, y) )
+
+def strongPairs(ints):
+ strong = []
+ ints = set(sorted(ints))
+ for x in ints:
+ for y in ints:
+ if x < y and isStrongPair(x, y):
+ strong.append( f'({x}, {y})' )
+ return len(strong), ', '.join(strong)
+
+def comma_join(ints):
+ return ', '.join(map(lambda i: str(i), ints))
+
+def solution(ints):
+ print(f'Input: @ints = ({comma_join(ints)})')
+ c, explain = strongPairs(ints)
+ print(f'Output: {c}\n\nStrong Pairs: {explain}')
+
+print('Example 1:')
+solution([1, 2, 3, 4, 5])
+
+print('\nExample 2:')
+solution([5, 7, 1, 7])
diff --git a/challenge-277/packy-anderson/raku/ch-1.raku b/challenge-277/packy-anderson/raku/ch-1.raku
new file mode 100755
index 0000000000..0e3d7320a2
--- /dev/null
+++ b/challenge-277/packy-anderson/raku/ch-1.raku
@@ -0,0 +1,34 @@
+#!/usr/bin/env raku
+use v6;
+
+sub countCommon(@words1, @words2) {
+ # make Bags that count the words
+ my %counts1 = @words1.map({ $_.lc }).Bag;
+ my %counts2 = @words2.map({ $_.lc }).Bag;
+
+ # filter to get set of words occuring only once
+ my @counts1 = %counts1.keys.grep({ %counts1{$_} == 1});
+ my @counts2 = %counts2.keys.grep({ %counts2{$_} == 1});
+
+ # find the elements common in both
+ my @common = @counts1 ∩ @counts2;
+ return @common.elems;
+}
+
+sub solution(@words1, @words2) {
+ say 'Input: @words1 = (' ~ @words1.map({qq{"$_"}}).join(', ') ~ ')';
+ say ' @words2 = (' ~ @words2.map({qq{"$_"}}).join(', ') ~ ')';
+ say 'Output: ' ~ countCommon(@words1, @words2);
+}
+
+say "Example 1:";
+solution(["Perl", "is", "my", "friend"],
+ ["Perl", "and", "Raku", "are", "friend"]);
+
+say "\nExample 2:";
+solution(["Perl", "and", "Python", "are", "very", "similar"],
+ ["Python", "is", "top", "in", "guest", "languages"]);
+
+say "\nExample 3:";
+solution(["Perl", "is", "imperative", "Lisp", "is", "functional"],
+ ["Crystal", "is", "similar", "to", "Ruby"]);
diff --git a/challenge-277/packy-anderson/raku/ch-2.raku b/challenge-277/packy-anderson/raku/ch-2.raku
new file mode 100755
index 0000000000..72b5559abf
--- /dev/null
+++ b/challenge-277/packy-anderson/raku/ch-2.raku
@@ -0,0 +1,31 @@
+#!/usr/bin/env raku
+use v6;
+
+sub isStrongPair($x, $y) {
+ return 0 < abs($x - $y) < min($x, $y);
+}
+
+sub strongPairs(@ints) {
+ my @strong;
+ my @ints2 = @ints.unique.sort;
+ for @ints2 -> $x {
+ for @ints2 -> $y {
+ next if $x > $y;
+ next unless isStrongPair($x, $y);
+ @strong.push("($x, $y)");
+ }
+ }
+ return @strong.elems, @strong.join(', ');
+}
+
+sub solution(@ints) {
+ say 'Input: @ints = (' ~ @ints.join(', ') ~ ')';
+ my ($count, $explain) = strongPairs(@ints);
+ say "Output: $count\n\nStrong Pairs: $explain";
+}
+
+say "Example 1:";
+solution([1, 2, 3, 4, 5]);
+
+say "\nExample 2:";
+solution([5, 7, 1, 7]);