diff options
| -rwxr-xr-x | challenge-270/packy-anderson/elixir/ch-2.exs | 10 | ||||
| -rwxr-xr-x | challenge-270/packy-anderson/raku/ch-2.raku | 3 | ||||
| -rw-r--r-- | challenge-271/packy-anderson/README.md | 2 | ||||
| -rw-r--r-- | challenge-271/packy-anderson/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-271/packy-anderson/elixir/ch-1.exs | 53 | ||||
| -rwxr-xr-x | challenge-271/packy-anderson/elixir/ch-2.exs | 24 | ||||
| -rwxr-xr-x | challenge-271/packy-anderson/perl/ch-1.pl | 49 | ||||
| -rwxr-xr-x | challenge-271/packy-anderson/perl/ch-2.pl | 33 | ||||
| -rwxr-xr-x | challenge-271/packy-anderson/python/ch-1.py | 42 | ||||
| -rwxr-xr-x | challenge-271/packy-anderson/python/ch-2.py | 22 | ||||
| -rwxr-xr-x | challenge-271/packy-anderson/raku/ch-1.raku | 47 | ||||
| -rwxr-xr-x | challenge-271/packy-anderson/raku/ch-2.raku | 36 |
12 files changed, 317 insertions, 5 deletions
diff --git a/challenge-270/packy-anderson/elixir/ch-2.exs b/challenge-270/packy-anderson/elixir/ch-2.exs index 2904331718..ee1b9a420a 100755 --- a/challenge-270/packy-anderson/elixir/ch-2.exs +++ b/challenge-270/packy-anderson/elixir/ch-2.exs @@ -3,7 +3,7 @@ defmodule PWC do # get just the indices stored in the map of maps defp indicesInMap(mapping) do - List.flatten( for i <- Map.values(mapping), do: Map.keys(i) ) + List.flatten( for i <- Map.values(mapping), do: MapSet.to_list(i) ) end def fmtInts(ints) do @@ -11,13 +11,15 @@ defmodule PWC do end def addToMap(mapping, intVal, i) do - submap = Map.put(Map.get(mapping, intVal, %{}), i, 1) + submap = MapSet.put(Map.get(mapping, intVal, MapSet.new()), i) Map.put(mapping, intVal, submap) end def removeFromMap(mapping, intVal, i) do - submap = Map.delete(Map.get(mapping, intVal, %{}), i) - if submap == %{} do + submap = mapping + |> Map.get(intVal, MapSet.new()) + |> MapSet.delete(i) + if MapSet.size(submap) == 0 do Map.delete(mapping, intVal) else Map.put(mapping, intVal, submap) diff --git a/challenge-270/packy-anderson/raku/ch-2.raku b/challenge-270/packy-anderson/raku/ch-2.raku index 897ddc89a2..0fc3db2d1d 100755 --- a/challenge-270/packy-anderson/raku/ch-2.raku +++ b/challenge-270/packy-anderson/raku/ch-2.raku @@ -92,3 +92,6 @@ solution([4,1], 3, 2); say "\nExample 2:"; solution([2, 3, 3, 3, 5], 2, 1); + +say "\nExample Ergon:"; +solution([1,2,2], 3, 1); diff --git a/challenge-271/packy-anderson/README.md b/challenge-271/packy-anderson/README.md index 3f65972d0f..bfedb42a8d 100644 --- a/challenge-271/packy-anderson/README.md +++ b/challenge-271/packy-anderson/README.md @@ -20,4 +20,4 @@ ## Blog Post -[Bitwise Distribution](https://packy.dardan.com/b/LS) +[Only Ones](https://packy.dardan.com/b/MC) diff --git a/challenge-271/packy-anderson/blog.txt b/challenge-271/packy-anderson/blog.txt new file mode 100644 index 0000000000..79befae346 --- /dev/null +++ b/challenge-271/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/b/MC
\ No newline at end of file diff --git a/challenge-271/packy-anderson/elixir/ch-1.exs b/challenge-271/packy-anderson/elixir/ch-1.exs new file mode 100755 index 0000000000..c5223f3df3 --- /dev/null +++ b/challenge-271/packy-anderson/elixir/ch-1.exs @@ -0,0 +1,53 @@ +#!/usr/bin/env elixir + +defmodule PWC do + # we've exhasuted the matrix, return maxRow as 1-indexed value + def maximumOnes([], _, _, maxRow), do: maxRow + 1 + + def maximumOnes([row | matrix], rowNum, maxCount, maxRow) do + count = Enum.sum(row) + if count > maxCount do + # make the recursive call with this count and rowNum + maximumOnes(matrix, rowNum+1, count, rowNum) + else + # make the recursive call with the count and rowNum + # it was called with + maximumOnes(matrix, rowNum+1, maxCount, maxRow) + end + end + + def maximumOnes(matrix) do + # set up the loop with initial values + maximumOnes(matrix, 0, -1, -1) + end + + # default value for indent + def formatMatrix(matrix), do: formatMatrix(matrix, 17) + + def formatMatrix(matrix, indent) do + output = for row <- matrix do + String.duplicate(" ", indent) <> " [" <> + Enum.join(row, ", ") <> "]" + end + "[\n" <> Enum.join(output, ",\n") <> "\n" <> + String.duplicate(" ", indent) <> "]" + end + + def solution(matrix) do + IO.puts("Input: $matrix = " <> formatMatrix(matrix)) + IO.puts("Output: " <> to_string(maximumOnes(matrix))) + end +end + +IO.puts("Example 1:") +PWC.solution([ [0, 1], + [1, 0] ]) + +IO.puts("\nExample 2:") +PWC.solution([ [0, 0, 0], + [1, 0, 1] ]) + +IO.puts("\nExample 3:") +PWC.solution([ [0, 0], + [1, 1], + [0, 0] ]) diff --git a/challenge-271/packy-anderson/elixir/ch-2.exs b/challenge-271/packy-anderson/elixir/ch-2.exs new file mode 100755 index 0000000000..fb7daad40a --- /dev/null +++ b/challenge-271/packy-anderson/elixir/ch-2.exs @@ -0,0 +1,24 @@ +#!/usr/bin/env elixir + +defmodule PWC do + def bit_count(num) do + binary = Integer.to_string(num, 2) + String.length(Regex.replace(~r/0/, binary, "")) + end + + def sortByHammingWeight(ints) do + Enum.sort_by(ints, &{ bit_count(&1), &1 }) + end + + def solution(ints) do + IO.puts("Input: @ints = (" <> Enum.join(ints, ", ") <> ")") + out = sortByHammingWeight(ints) + IO.puts("Output: (" <> Enum.join(out, ", ") <> ")") + end +end + +IO.puts("Example 1:") +PWC.solution([0, 1, 2, 3, 4, 5, 6, 7, 8]) + +IO.puts("\nExample 2:") +PWC.solution([1024, 512, 256, 128, 64]) diff --git a/challenge-271/packy-anderson/perl/ch-1.pl b/challenge-271/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..c11fc91bb0 --- /dev/null +++ b/challenge-271/packy-anderson/perl/ch-1.pl @@ -0,0 +1,49 @@ +#!/usr/bin/env perl +use v5.38; + +use List::Util qw( sum ); + +sub maximumOnes($matrix) { + my ($maxCount, $maxRow) = (-1, -1); + foreach my $rowNum ( 0 .. $#{$matrix} ) { + my @row = @{ $matrix->[$rowNum] }; + my $count = sum @row; + if ($count > $maxCount) { + $maxCount = $count; + $maxRow = $rowNum; + } + } + # we're displaying rows 1-indexed + return $maxRow + 1; +} + +sub formatMatrix($matrix, $indent=17) { + my @output; + foreach my $row (@$matrix) { + my $output_row = q{ } x $indent . " ["; + $output_row .= join(', ', map { sprintf "%1d", $_ } @$row) . ']'; + push @output, $output_row; + } + return "[\n" + . join(",\n", @output) + . "\n" + . q{ } x $indent . "]"; +} + +sub solution($matrix) { + say 'Input: $matrix = ' . formatMatrix($matrix); + say 'Output: ' . maximumOnes($matrix); +} + +say "Example 1:"; +solution([ [0, 1], + [1, 0] ]); + +say "\nExample 2:"; +solution([ [0, 0, 0], + [1, 0, 1] ]); + +say "\nExample 3:"; +solution([ [0, 0], + [1, 1], + [0, 0] ]); diff --git a/challenge-271/packy-anderson/perl/ch-2.pl b/challenge-271/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..f0b22e1417 --- /dev/null +++ b/challenge-271/packy-anderson/perl/ch-2.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl +use v5.38; + +use Memoize; +memoize('bit_count'); +sub bit_count($num) { + # convert to binary + my $binary = unpack("B32", pack("N", $num)); + # remove the 0s + $binary =~ s/0//g; + # what remains is just 1s + return length($binary); +} + +sub sortByHammingWeight(@ints) { + return sort { + bit_count($a) <=> bit_count($b) + || + $a <=> $b + } @ints; +} + +sub solution(@ints) { + say 'Input: @ints = (' . join(', ', @ints) . ')'; + my @out = sortByHammingWeight(@ints); + say 'Output: (' . join(', ', @out) . ')'; +} + +say "Example 1:"; +solution(0, 1, 2, 3, 4, 5, 6, 7, 8); + +say "\nExample 2:"; +solution(1024, 512, 256, 128, 64);
\ No newline at end of file diff --git a/challenge-271/packy-anderson/python/ch-1.py b/challenge-271/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..144d150650 --- /dev/null +++ b/challenge-271/packy-anderson/python/ch-1.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python + +def maximumOnes(matrix): + maxCount = -1 + maxRow = -1 + for rowNum, row in enumerate(matrix): + count = sum(row) + if count > maxCount: + maxCount = count + maxRow = rowNum + # we're displaying rows 1-indexed + return maxRow + 1 + +def formatMatrix(matrix, indent=17): + output = [] + for row in matrix: + output_row = ' ' * indent + ' [' + output_row += ', '.join(map(lambda i: str(i), row)) + output_row += ']' + output.append(output_row) + + return( + "[\n" + ",\n".join(output) + "\n" + + ' ' * indent + ']' + ) + +def solution(matrix): + print(f'Input: $matrix = {formatMatrix(matrix)}') + print(f'Output: { maximumOnes(matrix) }') + +print('Example 1:') +solution([ [0, 1], + [1, 0] ]) + +print('\nExample 2:') +solution([ [0, 0, 0], + [1, 0, 1] ]) + +print('\nExample 3:') +solution([ [0, 0], + [1, 1], + [0, 0] ]) diff --git a/challenge-271/packy-anderson/python/ch-2.py b/challenge-271/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..05d588853f --- /dev/null +++ b/challenge-271/packy-anderson/python/ch-2.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python + +def sortByHammingWeight(ints): + # use Decorate-Sort-Undecorate idiom + # (yes, it's a Schwartzian Transformation) + decorated = [ ( i.bit_count(), i ) for i in ints ] + decorated.sort() + return [ i for (bits, i) in decorated ] + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def solution(ints): + print(f'Input: @ints = ({comma_join(ints)})') + out = sortByHammingWeight(ints) + print(f'Output: ({comma_join(out)})') + +print('Example 1:') +solution([0, 1, 2, 3, 4, 5, 6, 7, 8]) + +print('\nExample 2:') +solution([1024, 512, 256, 128, 64])
\ No newline at end of file diff --git a/challenge-271/packy-anderson/raku/ch-1.raku b/challenge-271/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..550982f834 --- /dev/null +++ b/challenge-271/packy-anderson/raku/ch-1.raku @@ -0,0 +1,47 @@ +#!/usr/bin/env raku +use v6; + +sub maximumOnes(@matrix) { + my ($maxCount, $maxRow) = (-1, -1); + for @matrix.kv -> $rowNum, @row { + my $count = [+] @row; + if ($count > $maxCount) { + $maxCount = $count; + $maxRow = $rowNum; + } + } + # we're displaying rows 1-indexed + return $maxRow + 1; +} + +sub formatMatrix(@matrix, $indent=17) { + my @output; + for @matrix -> @row { + my $output_row = q{ } x $indent ~ " ["; + $output_row ~= @row.map({ sprintf "%1d", $_ }) + .join(', ') ~ "]"; + @output.push($output_row); + } + return "[\n" + ~ @output.join(",\n") + ~ "\n" + ~ q{ } x $indent ~ "]"; +} + +sub solution(@matrix) { + say 'Input: $matrix = ' ~ formatMatrix(@matrix); + say 'Output: ' ~ maximumOnes(@matrix); +} + +say "Example 1:"; +solution([ [0, 1], + [1, 0] ]); + +say "\nExample 2:"; +solution([ [0, 0, 0], + [1, 0, 1] ]); + +say "\nExample 3:"; +solution([ [0, 0], + [1, 1], + [0, 0] ]); diff --git a/challenge-271/packy-anderson/raku/ch-2.raku b/challenge-271/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..0043d7049a --- /dev/null +++ b/challenge-271/packy-anderson/raku/ch-2.raku @@ -0,0 +1,36 @@ +#!/usr/bin/env raku +use v6; + +use experimental :cached; +sub bit_count($num) is cached { + # convert to binary + my $binary = $num.Int.base(2); + # remove the 0s + $binary ~~ s:g/0//; + # what remains is just 1s + return $binary.chars; +} + +sub sortFunction($a, $b) { + if ( bit_count($a) > bit_count($b) ) { Order::More } + elsif ( bit_count($a) < bit_count($b) ) { Order::Less } + elsif ( $a > $b ) { Order::More } + elsif ( $a > $b ) { Order::Less } + else { Order::Same } +} + +sub sortByHammingWeight(@ints) { + return @ints.sort: { sortFunction($^a, $^b) }; +} + +sub solution(@ints) { + say 'Input: @ints = (' ~ @ints.join(', ') ~ ')'; + my @out = sortByHammingWeight(@ints); + say 'Output: (' ~ @out.join(', ') ~ ')'; +} + +say "Example 1:"; +solution([0, 1, 2, 3, 4, 5, 6, 7, 8]); + +say "\nExample 2:"; +solution([1024, 512, 256, 128, 64]);
\ No newline at end of file |
