diff options
| author | Packy Anderson <packy@cpan.org> | 2024-07-04 00:50:55 -0400 |
|---|---|---|
| committer | Packy Anderson <packy@cpan.org> | 2024-07-04 00:50:55 -0400 |
| commit | 9246c36a181a4a62197859d7bf7367e2295a5c64 (patch) | |
| tree | 3ce0086e0171ca068c032c3d170d026df7142def | |
| parent | d3cd6c8a7072e6f3566176284074c04501bfe8e3 (diff) | |
| download | perlweeklychallenge-club-9246c36a181a4a62197859d7bf7367e2295a5c64.tar.gz perlweeklychallenge-club-9246c36a181a4a62197859d7bf7367e2295a5c64.tar.bz2 perlweeklychallenge-club-9246c36a181a4a62197859d7bf7367e2295a5c64.zip | |
Challenge 276 solutions by Packy Anderson
* Raku that maybe looks like Raku
* Perl
* Python that definitely looks like Perl
* Elixir
1 Blog post
| -rw-r--r-- | challenge-276/packy-anderson/README.md | 2 | ||||
| -rw-r--r-- | challenge-276/packy-anderson/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-276/packy-anderson/elixir/ch-1.exs | 48 | ||||
| -rwxr-xr-x | challenge-276/packy-anderson/elixir/ch-2.exs | 86 | ||||
| -rwxr-xr-x | challenge-276/packy-anderson/perl/ch-1.pl | 33 | ||||
| -rwxr-xr-x | challenge-276/packy-anderson/perl/ch-2.pl | 38 | ||||
| -rwxr-xr-x | challenge-276/packy-anderson/python/ch-1.py | 33 | ||||
| -rwxr-xr-x | challenge-276/packy-anderson/python/ch-2.py | 44 | ||||
| -rwxr-xr-x | challenge-276/packy-anderson/raku/ch-1.raku | 33 | ||||
| -rwxr-xr-x | challenge-276/packy-anderson/raku/ch-2.raku | 34 |
10 files changed, 351 insertions, 1 deletions
diff --git a/challenge-276/packy-anderson/README.md b/challenge-276/packy-anderson/README.md index 0975c778da..1c88177fb7 100644 --- a/challenge-276/packy-anderson/README.md +++ b/challenge-276/packy-anderson/README.md @@ -22,4 +22,4 @@ ## Blog Post -[Digitally Replace Broken Keys](https://packy.dardan.com/b/NA) +[Complete Maximum Day Frequency](https://packy.dardan.com/b/NT) diff --git a/challenge-276/packy-anderson/blog.txt b/challenge-276/packy-anderson/blog.txt new file mode 100644 index 0000000000..f9d3198789 --- /dev/null +++ b/challenge-276/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/b/NT
\ No newline at end of file diff --git a/challenge-276/packy-anderson/elixir/ch-1.exs b/challenge-276/packy-anderson/elixir/ch-1.exs new file mode 100755 index 0000000000..dc0cc8bccf --- /dev/null +++ b/challenge-276/packy-anderson/elixir/ch-1.exs @@ -0,0 +1,48 @@ +#!/usr/bin/env elixir + +defmodule PWC do + def makePair([], _, pairs), do: pairs + def makePair([j], i, pairs) do + pairs ++ [ [i, j] ] + end + def makePair([j | rest], i, pairs) do + pairs = pairs ++ [ [i, j] ] + makePair(rest, i, pairs) + end + + def findPairs([], pairs), do: pairs + def findPairs([i | rest], pairs) do + canPair = Enum.filter(rest, fn j -> rem(i + j, 24) == 0 end) + pairs = makePair(canPair, i, pairs) + findPairs(rest, pairs) + end + + def explainPairs([], _, explain), do: explain + def explainPairs([pair | rest ], i, explain) do + pairList = "(" <> Enum.join(pair, ", ") <> ")" + pairNum = "Pair #{to_string(i)}: " + explainPairs(rest, i+1, explain <> "\n" <> pairNum <> pairList) + end + + def completeDays(hours) do + pairs = findPairs(hours, []) + explain = explainPairs(pairs, 1, "") + {length(pairs), explain} + end + + def solution(hours) do + IO.puts("Input: @hours = (" <> Enum.join(hours, ", ") <> ")") + {count, explain} = completeDays(hours) + IO.puts("Output: " <> to_string(count) ) + IO.puts(explain) + end +end + +IO.puts("Example 1:") +PWC.solution([12, 12, 30, 24, 24]) + +IO.puts("\nExample 2:") +PWC.solution([72, 48, 24, 5]) + +IO.puts("\nExample 3:") +PWC.solution([12, 18, 24]) diff --git a/challenge-276/packy-anderson/elixir/ch-2.exs b/challenge-276/packy-anderson/elixir/ch-2.exs new file mode 100755 index 0000000000..c3ef130314 --- /dev/null +++ b/challenge-276/packy-anderson/elixir/ch-2.exs @@ -0,0 +1,86 @@ +#!/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 multiplicities(t) :: MapSet.t + def multiplicities(multiset) + def multiplicities(%Multiset{map: map}), do: map |> Map.values + + @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 conjunction(l) do + if length(l) < 2 do + l + else + if length(l) == 2 do + to_string(List.first(l)) <> " and " <> + to_string(List.last(l)) + else + {last, rest} = List.pop_at(l, -1) + Enum.join(rest, ", ") <> ", and " <> to_string(last) + end + end + end + + def maxFrequency(ints) do + freq = Multiset.new(ints) + maxFreq = Enum.max(Multiset.multiplicities(freq)) + atMax = Enum.filter(Multiset.values(freq), fn i -> + Multiset.multiplicity(freq, i) == maxFreq + end) + + numList = conjunction(atMax) + {elements, have} = if length(atMax) > 1 do + {"elements", "have"} + else + {"element", "has"} + end + explain = "The maximum frequency is #{maxFreq}.\n" <> + "The #{elements} #{numList} #{have} " <> + "the maximum frequency." + + {maxFreq * length(atMax), explain} + end + + def solution(ints) do + IO.puts("Input: @ints = (" <> Enum.join(ints, ", ") <> ")") + {count, explain} = maxFrequency(ints) + IO.puts("Output: " <> to_string(count) ) + IO.puts("\n" <> explain) + end +end + +IO.puts("Example 1:") +PWC.solution([1, 2, 2, 4, 1, 5]) + +IO.puts("\nExample 2:") +PWC.solution([1, 2, 3, 4, 5]) diff --git a/challenge-276/packy-anderson/perl/ch-1.pl b/challenge-276/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..7371b5afde --- /dev/null +++ b/challenge-276/packy-anderson/perl/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl +use v5.40; + +sub completeDays(@hours) { + my @pairs; + foreach my $i ( 0 .. $#hours - 1 ) { + foreach my $j ( $i + 1 .. $#hours) { + push @pairs, [ $hours[$i], $hours[$j] ] + if ( $hours[$i] + $hours[$j] ) % 24 == 0; + } + } + my $explain = ""; + foreach my $i ( 0 .. $#pairs ) { + $explain .= "\n" . "Pair " . ($i+1) . ": (" + . join(", ", @{ $pairs[$i] }) . ")" + } + return scalar(@pairs), $explain; +} + +sub solution($hours) { + say 'Input: @hours = (' . join(', ', @$hours) . ')'; + my ($count, $explain) = completeDays(@$hours); + say "Output: $count\n$explain"; +} + +say "Example 1:"; +solution([12, 12, 30, 24, 24]); + +say "\nExample 2:"; +solution([72, 48, 24, 5]); + +say "\nExample 3:"; +solution([12, 18, 24]); diff --git a/challenge-276/packy-anderson/perl/ch-2.pl b/challenge-276/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..f2ec0c9205 --- /dev/null +++ b/challenge-276/packy-anderson/perl/ch-2.pl @@ -0,0 +1,38 @@ +#!/usr/bin/env perl +use v5.40; + +use List::Util qw( max ); +use Lingua::EN::Inflexion qw( inflect wordlist ); + +sub maxFrequency(@ints) { + my %freq; + map { $freq{$_}++ } @ints; + my $maxFreq = max(values %freq); + my @atMax; + foreach my $i ( sort keys %freq ) { + if ($freq{$i} == $maxFreq) { + push @atMax, $i; + } + } + my $list = wordlist(@atMax); + my $count = scalar(@atMax); + my $explain = "<#d:$count>The maximum frequency is $maxFreq.\n" + . "The <N:element> $list <V:has> " + . "the maximum frequency."; + return( + $maxFreq * $count, + inflect($explain) + ); +} + +sub solution($ints) { + say 'Input: @ints = (' . join(', ', @$ints) . ')'; + my ($count, $explain) = maxFrequency(@$ints); + say "Output: $count\n\n$explain"; +} + +say "Example 1:"; +solution([1, 2, 2, 4, 1, 5]); + +say "\nExample 2:"; +solution([1, 2, 3, 4, 5]); diff --git a/challenge-276/packy-anderson/python/ch-1.py b/challenge-276/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..54b4b6db1b --- /dev/null +++ b/challenge-276/packy-anderson/python/ch-1.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def completeDays(hours): + pairs = [] + for i in range(len(hours) - 1): + for j in range(i+1, len(hours)): + if ( hours[i] + hours[j] ) % 24 == 0: + pairs.append([ hours[i], hours[j] ]) + + explain = "" + i = 1 + for p in pairs: + explain += f"\nPair {i}: ({comma_join(p)})" + i += 1 + + return len(pairs), explain + +def solution(hours): + print(f'Input: @hours = ({comma_join(hours)})') + count, explain = completeDays(hours) + print(f'Output: {count}\n{explain}') + +print('Example 1:') +solution([12, 12, 30, 24, 24]) + +print('\nExample 2:') +solution([72, 48, 24, 5]) + +print('\nExample 3:') +solution([12, 18, 24]) diff --git a/challenge-276/packy-anderson/python/ch-2.py b/challenge-276/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..9428fa0118 --- /dev/null +++ b/challenge-276/packy-anderson/python/ch-2.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python + +from collections import Counter + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def conjunction(ints): + if len(ints) < 2: + return(ints) + elif len(ints) == 2: + return(f'{ints[0]} and {ints[1]}') + else: + last = ints.pop(-1) + l = comma_join(ints) + return(f'{l}, and {last}') + +def maxFrequency(ints): + freq = Counter(ints) + maxFreq = max(freq.values()) + atMax = [] + for i in sorted(freq.keys()): + if freq[i] == maxFreq: + atMax.append(i) + numList = conjunction(atMax) + elements = "elements" if len(atMax) > 1 else "element" + have = "have" if len(atMax) > 1 else "has" + explain = ( + f"The maximum frequency is {maxFreq}.\n" + + f"The {elements} {numList} {have} " + + f"the maximum frequency." + ) + return (maxFreq * len(atMax), explain) + +def solution(ints): + print(f'Input: @ints = ({comma_join(ints)})') + count, explain = maxFrequency(ints) + print(f'Output: {count}\n\n{explain}') + +print('Example 1:') +solution([1, 2, 2, 4, 1, 5]) + +print('\nExample 2:') +solution([1, 2, 3, 4, 5]) diff --git a/challenge-276/packy-anderson/raku/ch-1.raku b/challenge-276/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..60b9eeb919 --- /dev/null +++ b/challenge-276/packy-anderson/raku/ch-1.raku @@ -0,0 +1,33 @@ +#!/usr/bin/env raku +use v6; + +sub completeDays(@hours) { + my @pairs; + for 0 .. @hours.end - 1 -> $i { + for $i + 1 .. @hours.end -> $j { + @pairs.push([ @hours[$i], @hours[$j] ]) + if ( @hours[$i] + @hours[$j] ) % 24 == 0; + } + } + my $explain = ""; + for @pairs.kv -> $i, @p { + $explain ~= "\n" ~ "Pair " ~ ($i+1) ~ ": (" + ~ @p.join(", ") ~ ")" + } + return @pairs.elems, $explain; +} + +sub solution(@hours) { + say 'Input: @hours = (' ~ @hours.join(', ') ~ ')'; + my ($count, $explain) = completeDays(@hours); + say "Output: $count\n$explain"; +} + +say "Example 1:"; +solution([12, 12, 30, 24, 24]); + +say "\nExample 2:"; +solution([72, 48, 24, 5]); + +say "\nExample 3:"; +solution([12, 18, 24]); diff --git a/challenge-276/packy-anderson/raku/ch-2.raku b/challenge-276/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..e8f1590f25 --- /dev/null +++ b/challenge-276/packy-anderson/raku/ch-2.raku @@ -0,0 +1,34 @@ +#!/usr/bin/env raku +use v6; + +use Lingua::Conjunction; + +sub maxFrequency(@ints) { + my $freq = bag @ints; + my $maxFreq = max($freq.values); + my @atMax; + for $freq.keys.sort -> $i { + if ($freq{$i} == $maxFreq) { + @atMax.push($i); + } + } + my $explain = "The maximum frequency is $maxFreq.\n" + ~ "The element[|s] |list| [has|have] " + ~ "the maximum frequency."; + return( + $maxFreq * @atMax.elems, + conjunction(@atMax, :str($explain)) + ); +} + +sub solution(@ints) { + say 'Input: @ints = (' ~ @ints.join(', ') ~ ')'; + my ($count, $explain) = maxFrequency(@ints); + say "Output: $count\n\n$explain"; +} + +say "Example 1:"; +solution([1, 2, 2, 4, 1, 5]); + +say "\nExample 2:"; +solution([1, 2, 3, 4, 5]); |
