diff options
| author | Packy Anderson <PackyAnderson@gmail.com> | 2024-05-07 01:34:40 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-07 01:34:40 -0400 |
| commit | 459cbc90f6525486bdaaa02e1311793012a7dea3 (patch) | |
| tree | 3693ecbdd4c39673cfb6f970503741dd02adcdce | |
| parent | c1756b0e7aed0ad70fa63feb2565c69215c9d426 (diff) | |
| parent | 9df1740d3165b92d26f3e39a019cbabd6141d672 (diff) | |
| download | perlweeklychallenge-club-459cbc90f6525486bdaaa02e1311793012a7dea3.tar.gz perlweeklychallenge-club-459cbc90f6525486bdaaa02e1311793012a7dea3.tar.bz2 perlweeklychallenge-club-459cbc90f6525486bdaaa02e1311793012a7dea3.zip | |
Merge pull request #1 from packy/challenge-268
Challenge 268 solutions by Packy Anderson
| -rw-r--r-- | challenge-267/packy-anderson/elixir/ch-1.exs | 62 | ||||
| -rw-r--r-- | challenge-267/packy-anderson/elixir/ch-1a.exs | 57 | ||||
| -rwxr-xr-x | challenge-267/packy-anderson/elixir/ch-2.exs | 111 | ||||
| -rwxr-xr-x | challenge-267/packy-anderson/elixir/ch-2a.exs | 108 | ||||
| -rwxr-xr-x | challenge-267/packy-anderson/python/ch-1.py | 2 | ||||
| -rwxr-xr-x | challenge-267/packy-anderson/raku/ch-1.raku | 2 | ||||
| -rw-r--r-- | challenge-268/packy-anderson/README.md | 2 | ||||
| -rw-r--r-- | challenge-268/packy-anderson/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-268/packy-anderson/perl/ch-1.pl | 42 | ||||
| -rwxr-xr-x | challenge-268/packy-anderson/perl/ch-2.pl | 33 | ||||
| -rwxr-xr-x | challenge-268/packy-anderson/python/ch-1.py | 39 | ||||
| -rwxr-xr-x | challenge-268/packy-anderson/python/ch-2.py | 30 | ||||
| -rwxr-xr-x | challenge-268/packy-anderson/raku/ch-1.raku | 42 | ||||
| -rwxr-xr-x | challenge-268/packy-anderson/raku/ch-2.raku | 33 |
14 files changed, 561 insertions, 3 deletions
diff --git a/challenge-267/packy-anderson/elixir/ch-1.exs b/challenge-267/packy-anderson/elixir/ch-1.exs new file mode 100644 index 0000000000..a6152f78a0 --- /dev/null +++ b/challenge-267/packy-anderson/elixir/ch-1.exs @@ -0,0 +1,62 @@ +# You are given an array of @ints. + +# Write a script to find the sign of product of all integers in +# the given array. The sign is 1 if the product is positive, +# -1 if the product is negative and 0 if product is zero. + +# Example 1 +# Input: @ints = (-1, -2, -3, -4, 3, 2, 1) +# Output: 1 +# +# The product -1 x -2 x -3 x -4 x 3 x 2 x 1 => 144 > 0 + +# Example 2 +# Input: @ints = (1, 2, 0, -2, -1) +# Output: 0 +# +# The product 1 x 2 x 0 x -2 x -1 => 0 + +# Example 3 +# Input: @ints = (-1, -1, 1, -1, 2) +# Output: -1 +# +# The product -1 x -1 x 1 x -1 x 2 => -2 < 0 + +defmodule PWC do + + def productSign(ints) do + product = Enum.reduce(ints, &( &1 * &2 )) + sign = if product == 0, do: 0, + else: div(product, abs(product)) + explain = "The product " + <> Enum.join(ints, " × ") + <> " => " + <> to_string(product) + explain = cond do + sign > 0 -> explain <> " > 0" + sign < 0 -> explain <> " < 0" + true -> explain + end + {sign, explain} + end + + def solution(ints) do + IO.puts("Input: @ints = (" <> Enum.join(ints, ", ") <> ")") + {sign, explain} = PWC.productSign(ints) + IO.puts("Output: " <> to_string(sign) ) + IO.puts("\n" <> explain) + end +end + +IO.puts("Example 1:") +PWC.solution([-1, -2, -3, -4, 3, 2, 1]) + +IO.puts("") + +IO.puts("Example 2:") +PWC.solution([1, 2, 0, -2, -1]) + +IO.puts("") + +IO.puts("Example 3:") +PWC.solution([-1, -1, 1, -1, 2]) diff --git a/challenge-267/packy-anderson/elixir/ch-1a.exs b/challenge-267/packy-anderson/elixir/ch-1a.exs new file mode 100644 index 0000000000..86a7b6363c --- /dev/null +++ b/challenge-267/packy-anderson/elixir/ch-1a.exs @@ -0,0 +1,57 @@ +# You are given an array of @ints. + +# Write a script to find the sign of product of all integers in +# the given array. The sign is 1 if the product is positive, +# -1 if the product is negative and 0 if product is zero. + +# Example 1 +# Input: @ints = (-1, -2, -3, -4, 3, 2, 1) +# Output: 1 +# +# The product -1 x -2 x -3 x -4 x 3 x 2 x 1 => 144 > 0 + +# Example 2 +# Input: @ints = (1, 2, 0, -2, -1) +# Output: 0 +# +# The product 1 x 2 x 0 x -2 x -1 => 0 + +# Example 3 +# Input: @ints = (-1, -1, 1, -1, 2) +# Output: -1 +# +# The product -1 x -1 x 1 x -1 x 2 => -2 < 0 + +defmodule PWC do + + defp sign(n) when n > 0, do: " > 0" + defp sign(n) when n < 0, do: " < 0" + defp sign(_), do: "" + + def productSign(ints) do + product = Enum.product(ints) + sign = + case product do + 0 -> 0 + _ -> div(product, abs(product)) + end + explain = + "The product #{Enum.join(ints, " × ")} => #{to_string(product)}#{sign(sign)}" + {sign, explain} + end + + def solution(ints) do + IO.puts("Input: @ints = (#{Enum.join(ints, ", ")})") + {sign, explain} = PWC.productSign(ints) + IO.puts("Output: #{to_string(sign)}\n#{explain}") + end +end + +IO.puts("Example 1:") +PWC.solution([-1, -2, -3, -4, 3, 2, 1]) + +IO.puts("\nExample 2:") +PWC.solution([1, 2, 0, -2, -1]) + +IO.puts("\nExample 3:") +PWC.solution([-1, -1, 1, -1, 2]) diff --git a/challenge-267/packy-anderson/elixir/ch-2.exs b/challenge-267/packy-anderson/elixir/ch-2.exs new file mode 100755 index 0000000000..84372a76d5 --- /dev/null +++ b/challenge-267/packy-anderson/elixir/ch-2.exs @@ -0,0 +1,111 @@ +#!/usr/bin/env elixir + +# You are given a string, $str, and a 26-items array @widths +# containing the width of each character from a to z. + +# Write a script to find out the number of lines and the width +# of the last line needed to display the given string, +# assuming you can only fit 100 width units on a line. + +# Example 1 +# Input: $str = "abcdefghijklmnopqrstuvwxyz" +# @widths = (10,10,10,10,10,10,10,10,10,10,10,10,10, +# 10,10,10,10,10,10,10,10,10,10,10,10,10) +# Output: (3, 60) +# +# Line 1: abcdefghij (100 pixels) +# Line 2: klmnopqrst (100 pixels) +# Line 3: uvwxyz (60 pixels) + +# Example 2 +# Input: $str = "bbbcccdddaaa" +# @widths = (4,10,10,10,10,10,10,10,10,10,10,10,10, +# 10,10,10,10,10,10,10,10,10,10,10,10,10) +# Output: (2, 4) +# +# Line 1: bbbcccdddaa (98 pixels) +# Line 2: a (4 pixels) + +defmodule PWC do + @alphabet String.split("abcdefghijklmnopqrstuvwxyz", "", trim: true) + + defp updateExplain(vars) do + # because this is being called with an older version of vars + # (before lines is incremented), we need to add one to the value + # in this function + vars[:explain] <> "\nLine " <> to_string(vars[:lines] + 1) <> + ": #{vars[:last_line]} (#{to_string(vars[:line_width])} pixels)" + end + + # if the character list is empty + defp charCount([], _widthMap, vars) do + vars = %{vars | + explain: updateExplain(vars), + lines: vars[:lines] + 1 + } + vars + end + + # split the character list into the first character + # and all the remaining characters in the list + defp charCount([c | remaining], widthMap, vars) do + char_width = widthMap[c] + vars = if vars[:line_width] + char_width > 100 do + vars = %{vars | + explain: updateExplain(vars), + lines: vars[:lines] + 1, + line_width: char_width, + last_line: c + } + vars + else + vars = %{vars | + line_width: vars[:line_width] + char_width, + last_line: vars[:last_line] <> c + } + vars + end + charCount(remaining, widthMap, vars) + end + + def lineCounts(str, widths) do + widthMap = Enum.zip(@alphabet, widths) + |> Map.new() + vars = %{ + lines: 0, + line_width: 0, + last_line: "", + explain: "" + } + charCount(String.split(str, "", trim: true), widthMap, vars) + end + + def solution(str, widths) do + IO.puts("Input: $str = '#{str}'") + IO.puts(" @widths = (#{Enum.join(widths, ", ")})") + vars = PWC.lineCounts(str, widths) + IO.puts("Output: (#{to_string(vars[:lines])}, " <> + "#{to_string(vars[:line_width])})#{vars[:explain]}") + end +end + +IO.puts("Example 1:") +PWC.solution( + "abcdefghijklmnopqrstuvwxyz", + [10,10,10,10,10,10,10,10,10,10,10,10,10, + 10,10,10,10,10,10,10,10,10,10,10,10,10] +) + +IO.puts("\nExample 2:") +PWC.solution( + "bbbcccdddaaa", + [ 4,10,10,10,10,10,10,10,10,10,10,10,10, + 10,10,10,10,10,10,10,10,10,10,10,10,10] +) + +IO.puts("\nExample 3:") +PWC.solution( + "thequickbrownfoxjumpedoverthelazydog", + [7,8,7,8,7,5,8,8,4,4,8,4,12, + 8,8,8,8,5,6,4,8,8,12,8,8,7] +) diff --git a/challenge-267/packy-anderson/elixir/ch-2a.exs b/challenge-267/packy-anderson/elixir/ch-2a.exs new file mode 100755 index 0000000000..7eb75b1c05 --- /dev/null +++ b/challenge-267/packy-anderson/elixir/ch-2a.exs @@ -0,0 +1,108 @@ +#!/usr/bin/env elixir + +# You are given a string, $str, and a 26-items array @widths +# containing the width of each character from a to z. + +# Write a script to find out the number of lines and the width +# of the last line needed to display the given string, +# assuming you can only fit 100 width units on a line. + +# Example 1 +# Input: $str = "abcdefghijklmnopqrstuvwxyz" +# @widths = (10,10,10,10,10,10,10,10,10,10,10,10,10, +# 10,10,10,10,10,10,10,10,10,10,10,10,10) +# Output: (3, 60) +# +# Line 1: abcdefghij (100 pixels) +# Line 2: klmnopqrst (100 pixels) +# Line 3: uvwxyz (60 pixels) + +# Example 2 +# Input: $str = "bbbcccdddaaa" +# @widths = (4,10,10,10,10,10,10,10,10,10,10,10,10, +# 10,10,10,10,10,10,10,10,10,10,10,10,10) +# Output: (2, 4) +# +# Line 1: bbbcccdddaa (98 pixels) +# Line 2: a (4 pixels) + +defmodule PWC do + @alphabet String.split("abcdefghijklmnopqrstuvwxyz", "", trim: true) + + defp updateExplain(vars) do + # because this is being called with an older version of vars + # (before lines is incremented), we need to add one to the value + # in this function + vars[:explain] <> "\nLine " <> to_string(vars[:lines] + 1) <> + ": #{vars[:last_line]} (#{to_string(vars[:line_width])} pixels)" + end + + # if the character list is empty + defp charCount([], _widthMap, vars) do + %{vars | + explain: updateExplain(vars), + lines: vars[:lines] + 1 + } + end + + # split the character list into the first character + # and all the remaining characters in the list + defp charCount([c | remaining], widthMap, vars) do + char_width = widthMap[c] + vars = if vars[:line_width] + char_width > 100 do + %{vars | + explain: updateExplain(vars), + lines: vars[:lines] + 1, + line_width: char_width, + last_line: c + } + else + %{vars | + line_width: vars[:line_width] + char_width, + last_line: vars[:last_line] <> c + } + end + charCount(remaining, widthMap, vars) + end + + def lineCounts(str, widths) do + widthMap = Enum.zip(@alphabet, widths) + |> Map.new() + vars = %{ + lines: 0, + line_width: 0, + last_line: "", + explain: "" + } + charCount(String.split(str, "", trim: true), widthMap, vars) + end + + def solution(str, widths) do + IO.puts("Input: $str = '#{str}'") + IO.puts(" @widths = (#{Enum.join(widths, ", ")})") + vars = PWC.lineCounts(str, widths) + IO.puts("Output: (#{to_string(vars[:lines])}, " <> + "#{to_string(vars[:line_width])})#{vars[:explain]}") + end +end + +IO.puts("Example 1:") +PWC.solution( + "abcdefghijklmnopqrstuvwxyz", + [10,10,10,10,10,10,10,10,10,10,10,10,10, + 10,10,10,10,10,10,10,10,10,10,10,10,10] +) + +IO.puts("\nExample 2:") +PWC.solution( + "bbbcccdddaaa", + [ 4,10,10,10,10,10,10,10,10,10,10,10,10, + 10,10,10,10,10,10,10,10,10,10,10,10,10] +) + +IO.puts("\nExample 3:") +PWC.solution( + "thequickbrownfoxjumpedoverthelazydog", + [7,8,7,8,7,5,8,8,4,4,8,4,12, + 8,8,8,8,5,6,4,8,8,12,8,8,7] +) diff --git a/challenge-267/packy-anderson/python/ch-1.py b/challenge-267/packy-anderson/python/ch-1.py index 034e7ce8fd..a7f13f1d4f 100755 --- a/challenge-267/packy-anderson/python/ch-1.py +++ b/challenge-267/packy-anderson/python/ch-1.py @@ -19,7 +19,7 @@ def comma_join(arr): return ', '.join(map(lambda i: str(i), arr)) def solution(ints): - print(f'Input: @arr = ({comma_join(ints)})') + print(f'Input: @ints = ({comma_join(ints)})') sign, explain = productSign(ints) print(f'Output: {sign}\n{explain}') diff --git a/challenge-267/packy-anderson/raku/ch-1.raku b/challenge-267/packy-anderson/raku/ch-1.raku index d03830a9ab..8f325a38a2 100755 --- a/challenge-267/packy-anderson/raku/ch-1.raku +++ b/challenge-267/packy-anderson/raku/ch-1.raku @@ -17,7 +17,7 @@ sub productSign(@ints) { } sub solution(@ints) { - say 'Input: @arr = (' ~ @ints.join(', ') ~ ')'; + say 'Input: @ints = (' ~ @ints.join(', ') ~ ')'; my ($sign, $explain) = productSign(@ints); say "Output: $sign\n$explain"; } diff --git a/challenge-268/packy-anderson/README.md b/challenge-268/packy-anderson/README.md index 3c4bbfa52c..736b69a97f 100644 --- a/challenge-268/packy-anderson/README.md +++ b/challenge-268/packy-anderson/README.md @@ -16,4 +16,4 @@ ## Blog Post -[It’s the Product Line Sign that Counts](https://packy.dardan.com/b/Ko) +[Let's do the Numbers!](https://packy.dardan.com/b/L6) diff --git a/challenge-268/packy-anderson/blog.txt b/challenge-268/packy-anderson/blog.txt new file mode 100644 index 0000000000..c57ebc32d4 --- /dev/null +++ b/challenge-268/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/b/L6
\ No newline at end of file diff --git a/challenge-268/packy-anderson/perl/ch-1.pl b/challenge-268/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..8985dd6ccf --- /dev/null +++ b/challenge-268/packy-anderson/perl/ch-1.pl @@ -0,0 +1,42 @@ +#!/usr/bin/env perl +use v5.38; + +sub magicNumber($x, $y) { + my @xS = sort @$x; + my @yS = sort @$y; + my $magic = shift(@yS) - shift(@xS); + while (@xS) { + if (shift(@yS) - shift(@xS) != $magic) { + return; # no magic number + } + } + return $magic; +} + +sub solution($x, $y) { + say 'Input: @x = (' . join(', ', @$x) . ')'; + say ' @y = (' . join(', ', @$y) . ')'; + my $magic = magicNumber($x, $y); + if (defined $magic) { + say 'Output: ' . $magic; + say "\nThe magic number is $magic."; + say '@x = (' . join(', ', @$x) . ')'; + say ' + ' . join(' ', ($magic) x scalar(@$x)); + say '@y = (' . join(', ', map { $_ + $magic } @$x ) . ')'; + } + else { + say 'Output: no magic number'; + } +} + +say "Example 1:"; +solution([3, 7, 5], [9, 5, 7]); + +say "\nExample 2:"; +solution([1, 2, 1], [5, 4, 4]); + +say "\nExample 3:"; +solution([2], [5]); + +say "\nExample 4:"; +solution([1, 2], [4, 2]);
\ No newline at end of file diff --git a/challenge-268/packy-anderson/perl/ch-2.pl b/challenge-268/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..36944305a8 --- /dev/null +++ b/challenge-268/packy-anderson/perl/ch-2.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl +use v5.38; + +sub numberGame(@ints) { + my @intSorted = sort @ints; + my @new; + while (@intSorted) { + my $x = shift @intSorted; + my $y = shift @intSorted; + if ($x > $y) { + push @new, $x, $y; + } + else { + push @new, $y, $x; + } + } + return @new; +} + +sub solution($ints) { + say 'Input: @ints = (' . join(', ', @$ints) . ')'; + my @new = numberGame(@$ints); + say 'Output: (' . join(', ', @new) . ')'; +} + +say "Example 1:"; +solution([2, 5, 3, 4]); + +say "\nExample 2:"; +solution([9, 4, 1, 3, 6, 4, 6, 1]); + +say "\nExample 3:"; +solution([1, 2, 2, 3]);
\ No newline at end of file diff --git a/challenge-268/packy-anderson/python/ch-1.py b/challenge-268/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..902a1fe2fb --- /dev/null +++ b/challenge-268/packy-anderson/python/ch-1.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python + +def magicNumber(x, y): + xS = sorted(x) + yS = sorted(y) + magic = yS.pop(0) - xS.pop(0) + while xS: + if yS.pop(0) - xS.pop(0) != magic: + return None; # no magic number + return magic + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def solution(x, y): + print(f'Input: @x = ({comma_join(x)})') + print(f' @y = ({comma_join(y)})') + magic = magicNumber(x, y) + if magic is None: + print('Output: no magic number') + else: + print(f'Output: {magic}\n') + print(f'@x = ({comma_join(x)})') + magicStr = ' '.join(map(lambda i: str(magic), x)) + print(f' + {magicStr}') + yStr = ', '.join(map(lambda i: str(i+magic), x)) + print(f'@y = ({yStr}') + +print('Example 1:') +solution([3, 7, 5], [9, 5, 7]) + +print('\nExample 2:') +solution([1, 2, 1], [5, 4, 4]) + +print('\nExample 3:') +solution([2], [5]) + +print('\nExample 4:') +solution([1, 2], [4, 2])
\ No newline at end of file diff --git a/challenge-268/packy-anderson/python/ch-2.py b/challenge-268/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..51acaf6615 --- /dev/null +++ b/challenge-268/packy-anderson/python/ch-2.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python + +def numberGame(ints): + intSorted = sorted(ints) + new = [] + while intSorted: + x = intSorted.pop(0) + y = intSorted.pop(0) + if x > y: + new.extend([x, y]) + else: + new.extend([y, x]) + return new + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def solution(ints): + print(f'Input: @ints = ({comma_join(ints)})') + new = numberGame(ints) + print(f'Output: ({comma_join(new)})') + +print('Example 1:') +solution([2, 5, 3, 4]) + +print('\nExample 2:') +solution([9, 4, 1, 3, 6, 4, 6, 1]) + +print('\nExample 3:') +solution([1, 2, 2, 3])
\ No newline at end of file diff --git a/challenge-268/packy-anderson/raku/ch-1.raku b/challenge-268/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..6696fe24da --- /dev/null +++ b/challenge-268/packy-anderson/raku/ch-1.raku @@ -0,0 +1,42 @@ +#!/usr/bin/env raku +use v6; + +sub magicNumber(@x, @y) { + my @xS = @x.sort; + my @yS = @y.sort; + my $magic = @yS.shift - @xS.shift; + while (@xS) { + if (@yS.shift - @xS.shift != $magic) { + return; # no magic number + } + } + return $magic; +} + +sub solution(@x, @y) { + say 'Input: @x = (' ~ @x.join(', ') ~ ')'; + say ' @y = (' ~ @y.join(', ') ~ ')'; + my $magic = magicNumber(@x, @y); + if ($magic.defined) { + say 'Output: ' ~ $magic; + say "\nThe magic number is $magic."; + say '@x = (' ~ @x.join(', ') ~ ')'; + say ' + ' ~ ($magic xx @x.elems).join(' '); + say '@y = (' ~ @x.map(-> $v { $v + $magic }).join(', ') ~ ')'; + } + else { + say 'Output: no magic number'; + } +} + +say "Example 1:"; +solution([3, 7, 5], [9, 5, 7]); + +say "\nExample 2:"; +solution([1, 2, 1], [5, 4, 4]); + +say "\nExample 3:"; +solution([2], [5]); + +say "\nExample 4:"; +solution([1, 2], [4, 2]);
\ No newline at end of file diff --git a/challenge-268/packy-anderson/raku/ch-2.raku b/challenge-268/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..103fbe599f --- /dev/null +++ b/challenge-268/packy-anderson/raku/ch-2.raku @@ -0,0 +1,33 @@ +#!/usr/bin/env raku +use v6; + +sub numberGame(@ints) { + my @intSorted = @ints.sort; + my @new; + while (@intSorted) { + my $x = @intSorted.shift; + my $y = @intSorted.shift; + if ($x > $y) { + @new.push: ($x, $y).Slip; + } + else { + @new.push: ($y, $x).Slip; + } + } + return @new; +} + +sub solution(@ints) { + say 'Input: @ints = (' ~ @ints.join(', ') ~ ')'; + my @new = numberGame(@ints); + say 'Output: (' ~ @new.join(', ') ~ ')'; +} + +say "Example 1:"; +solution([2, 5, 3, 4]); + +say "\nExample 2:"; +solution([9, 4, 1, 3, 6, 4, 6, 1]); + +say "\nExample 3:"; +solution([1, 2, 2, 3]);
\ No newline at end of file |
