diff options
| author | Packy Anderson <packy@cpan.org> | 2025-08-08 20:04:52 -0400 |
|---|---|---|
| committer | Packy Anderson <packy@cpan.org> | 2025-08-08 20:04:52 -0400 |
| commit | 939249c3cfb3cbb70885d9bdbda75cec4b0b0faa (patch) | |
| tree | 63750d2b95bc4aa3fcf744cf1bd61a0c1b146e05 | |
| parent | ad7449cae11b27d93368184ea2bbe271fee65ecd (diff) | |
| download | perlweeklychallenge-club-939249c3cfb3cbb70885d9bdbda75cec4b0b0faa.tar.gz perlweeklychallenge-club-939249c3cfb3cbb70885d9bdbda75cec4b0b0faa.tar.bz2 perlweeklychallenge-club-939249c3cfb3cbb70885d9bdbda75cec4b0b0faa.zip | |
Challenge 333 solutions by Packy Anderson
* Raku that maybe looks like Raku, but mostly like Perl
* Perl
* Python that definitely looks like Perl
* Elixir that's starting to look more like Elixir
1 blog post
| -rw-r--r-- | challenge-333/packy-anderson/README.md | 2 | ||||
| -rw-r--r-- | challenge-333/packy-anderson/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-333/packy-anderson/elixir/ch-1.exs | 41 | ||||
| -rwxr-xr-x | challenge-333/packy-anderson/elixir/ch-2.exs | 38 | ||||
| -rwxr-xr-x | challenge-333/packy-anderson/perl/ch-1.pl | 34 | ||||
| -rwxr-xr-x | challenge-333/packy-anderson/perl/ch-2.pl | 33 | ||||
| -rwxr-xr-x | challenge-333/packy-anderson/python/ch-1.py | 32 | ||||
| -rwxr-xr-x | challenge-333/packy-anderson/python/ch-2.py | 32 | ||||
| -rwxr-xr-x | challenge-333/packy-anderson/raku/ch-1.raku | 34 | ||||
| -rwxr-xr-x | challenge-333/packy-anderson/raku/ch-2.raku | 33 |
10 files changed, 279 insertions, 1 deletions
diff --git a/challenge-333/packy-anderson/README.md b/challenge-333/packy-anderson/README.md index 00b39f4b47..b25fced0ab 100644 --- a/challenge-333/packy-anderson/README.md +++ b/challenge-333/packy-anderson/README.md @@ -23,4 +23,4 @@ ## Blog Post -[Perl Weekly Challenge: Oddly Binary](https://packy.dardan.com/b/ZQ) +[Perl Weekly Challenge: Zero is Not the End of the Line](https://packy.dardan.com/b/Zd) diff --git a/challenge-333/packy-anderson/blog.txt b/challenge-333/packy-anderson/blog.txt new file mode 100644 index 0000000000..be3a2dee7b --- /dev/null +++ b/challenge-333/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/b/Zd
\ No newline at end of file diff --git a/challenge-333/packy-anderson/elixir/ch-1.exs b/challenge-333/packy-anderson/elixir/ch-1.exs new file mode 100755 index 0000000000..8fa8e10d56 --- /dev/null +++ b/challenge-333/packy-anderson/elixir/ch-1.exs @@ -0,0 +1,41 @@ +#!/usr/bin/env elixir + +defmodule PWC do + def straightLine(point_list) do + {a, point_list} = List.pop_at(point_list, 0) + {b, point_list} = List.pop_at(point_list, 0) + {c, _} = List.pop_at(point_list, 0) + ( + (Enum.at(a,0) - Enum.at(b,0)) * + (Enum.at(b,1) - Enum.at(c,1)) + == + (Enum.at(b,0) - Enum.at(c,0)) * + (Enum.at(a,1) - Enum.at(b,1)) + ) + end + + def solution(point_list) do + {_, list} = Enum.map_reduce(point_list, [], + fn p, list -> + {p, list ++ ["[#{Enum.at(p,0)}, #{Enum.at(p,1)}]"]} + end + ) + IO.puts("Input: @ints = (" <> Enum.join(list, ", ") <> ")") + IO.puts("Output: #{straightLine(point_list)}") + end +end + +IO.puts("Example 1:") +PWC.solution([[2, 1], [2, 3], [2, 5]]) + +IO.puts("\nExample 2:") +PWC.solution([[1, 4], [3, 4], [10, 4]]) + +IO.puts("\nExample 3:") +PWC.solution([[0, 0], [1, 1], [2, 3]]) + +IO.puts("\nExample 4:") +PWC.solution([[1, 1], [1, 1], [1, 1]]) + +IO.puts("\nExample 5:") +PWC.solution([[1000000, 1000000], [2000000, 2000000], [3000000, 3000000]]) diff --git a/challenge-333/packy-anderson/elixir/ch-2.exs b/challenge-333/packy-anderson/elixir/ch-2.exs new file mode 100755 index 0000000000..237ee7cfe0 --- /dev/null +++ b/challenge-333/packy-anderson/elixir/ch-2.exs @@ -0,0 +1,38 @@ +#!/usr/bin/env elixir + +defmodule PWC do + def duplicate_zeros(ints) do + size = length(ints) + {_, dest} = Enum.map_reduce(ints, [], + fn i, dest -> + case i do + 0 -> {i, dest ++ [0, 0]} + _ -> {i, dest ++ [i]} + end + end + ) + drop = size - length(dest) + Enum.drop(dest, drop) + end + + def solution(ints) do + IO.puts("Input: @ints = (" <> Enum.join(ints, ", ") <> ")") + duped = duplicate_zeros(ints) + IO.puts("Output: (" <> Enum.join(duped, ", ") <> ")") + end +end + +IO.puts("Example 1:") +PWC.solution([1, 0, 2, 3, 0, 4, 5, 0]) + +IO.puts("\nExample 2:") +PWC.solution([1, 2, 3]) + +IO.puts("\nExample 3:") +PWC.solution([1, 2, 3, 0]) + +IO.puts("\nExample 4:") +PWC.solution([0, 0, 1, 2]) + +IO.puts("\nExample 5:") +PWC.solution([1, 2, 0, 3, 4]) diff --git a/challenge-333/packy-anderson/perl/ch-1.pl b/challenge-333/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..3c3e4bef29 --- /dev/null +++ b/challenge-333/packy-anderson/perl/ch-1.pl @@ -0,0 +1,34 @@ +#!/usr/bin/env perl +use v5.40; + +sub straightLine(@list) { + my $a = shift @list; + my $b = shift @list; + my $c = shift @list; + return ( + ($a->[0] - $b->[0]) * ($b->[1] - $c->[1]) + == + ($b->[0] - $c->[0]) * ($a->[1] - $b->[1]) + ) ? 'true' : 'false'; +} + +sub solution($list) { + my $str_list = join ', ', map { "[$_->[0], $_->[1]]" } @$list; + say "Input: \@list = ($str_list)"; + say "Output: " . straightLine(@$list); +} + +say "Example 1:"; +solution([[2, 1], [2, 3], [2, 5]]); + +say "\nExample 2:"; +solution([[1, 4], [3, 4], [10, 4]]); + +say "\nExample 3:"; +solution([[0, 0], [1, 1], [2, 3]]); + +say "\nExample 4:"; +solution([[1, 1], [1, 1], [1, 1]]); + +say "\nExample 5:"; +solution([[1000000, 1000000], [2000000, 2000000], [3000000, 3000000]]); diff --git a/challenge-333/packy-anderson/perl/ch-2.pl b/challenge-333/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..c0b6bdb4d4 --- /dev/null +++ b/challenge-333/packy-anderson/perl/ch-2.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl +use v5.40; + +sub duplicateZeros(@ints) { + my $size = @ints; + my @dest; + while (@dest < $size) { + my $i = shift @ints; + push @dest, $i; + push @dest, $i if $i == 0; + } + return splice(@dest, 0, $size); +} + +sub solution($ints) { + say 'Input: @ints = (' . join(', ', @$ints) . ')'; + say 'Output: (' . join(', ', duplicateZeros(@$ints)) . ')'; +} + +say "Example 1:"; +solution([1, 0, 2, 3, 0, 4, 5, 0]); + +say "\nExample 2:"; +solution([1, 2, 3]); + +say "\nExample 3:"; +solution([1, 2, 3, 0]); + +say "\nExample 43:"; +solution([0, 0, 1, 2]); + +say "\nExample 5:"; +solution([1, 2, 0, 3, 4]); diff --git a/challenge-333/packy-anderson/python/ch-1.py b/challenge-333/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..2109c54efb --- /dev/null +++ b/challenge-333/packy-anderson/python/ch-1.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python + +def straightLine(point_list): + a = point_list.pop(0) + b = point_list.pop(0) + c = point_list.pop(0) + return ( + (a[0] - b[0]) * (b[1] - c[1]) + == + (b[0] - c[0]) * (a[1] - b[1]) + ) + +def solution(point_list): + str_list = ", ".join([ f"[{p[0]}, {p[1]}]" for p in point_list ]) + print(f'Input: @lists = ({str_list})') + print(f'Output: {straightLine(point_list)}') + + +print('Example 1:') +solution([[2, 1], [2, 3], [2, 5]]) + +print('\nExample 2:') +solution([[1, 4], [3, 4], [10, 4]]) + +print('\nExample 3:') +solution([[0, 0], [1, 1], [2, 3]]) + +print('\nExample 4:') +solution([[1, 1], [1, 1], [1, 1]]) + +print('\nExample 5:') +solution([[1000000, 1000000], [2000000, 2000000], [3000000, 3000000]])
\ No newline at end of file diff --git a/challenge-333/packy-anderson/python/ch-2.py b/challenge-333/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..075a48007a --- /dev/null +++ b/challenge-333/packy-anderson/python/ch-2.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python + +def duplicate_zeros(ints): + size = len(ints) + dest = [] + while len(dest) < size: + i = ints.pop(0) + dest.append(i) + if i == 0: dest.append(i) + return dest[0:size] + +def int_join(joiner, arr): + return joiner.join(map(lambda i: str(i), arr)) + +def solution(ints): + print(f'Input: @ints = ({int_join(", ", ints)})') + print(f'Output: ({int_join(", ", duplicate_zeros(ints))})') + +print('Example 1:') +solution([1, 0, 2, 3, 0, 4, 5, 0]) + +print('\nExample 2:') +solution([1, 2, 3]) + +print('\nExample 3:') +solution([1, 2, 3, 0]) + +print('\nExample 4:') +solution([0, 0, 1, 2]) + +print('\nExample 5:') +solution([1, 2, 0, 3, 4]) diff --git a/challenge-333/packy-anderson/raku/ch-1.raku b/challenge-333/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..d3c30d5b2a --- /dev/null +++ b/challenge-333/packy-anderson/raku/ch-1.raku @@ -0,0 +1,34 @@ +#!/usr/bin/env raku +use v6; + +sub straightLine(@list) { + my @a = @list[0].Array; + my @b = @list[1].Array; + my @c = @list[2].Array; + return ( + (@a[0] - @b[0]) * (@b[1] - @c[1]) + == + (@b[0] - @c[0]) * (@a[1] - @b[1]) + ); +} + +sub solution(@list) { + my $str_list = @list.map({ "[$_[0], $_[1]]"}).join(', '); + say "Input: \@list = ($str_list)"; + say "Output: " ~ straightLine(@list); +} + +say "Example 1:"; +solution([[2, 1], [2, 3], [2, 5]]); + +say "\nExample 2:"; +solution([[1, 4], [3, 4], [10, 4]]); + +say "\nExample 3:"; +solution([[0, 0], [1, 1], [2, 3]]); + +say "\nExample 4:"; +solution([[1, 1], [1, 1], [1, 1]]); + +say "\nExample 5:"; +solution([[1000000, 1000000], [2000000, 2000000], [3000000, 3000000]]);
\ No newline at end of file diff --git a/challenge-333/packy-anderson/raku/ch-2.raku b/challenge-333/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..b5b74c890e --- /dev/null +++ b/challenge-333/packy-anderson/raku/ch-2.raku @@ -0,0 +1,33 @@ +#!/usr/bin/env raku +use v6; + +sub duplicateZeros(@ints is copy) { + my $size = @ints.elems; + my @dest; + while (@dest.elems < $size) { + my $i = @ints.shift; + @dest.push($i); + @dest.push($i) if $i == 0; + } + return @dest.splice(0, $size); +} + +sub solution(@ints) { + say 'Input: @ints = (' ~ @ints.join(', ') ~ ')'; + say 'Output: (' ~ duplicateZeros(@ints).join(', ') ~ ')'; +} + +say "Example 1:"; +solution([1, 0, 2, 3, 0, 4, 5, 0]); + +say "\nExample 2:"; +solution([1, 2, 3]); + +say "\nExample 3:"; +solution([1, 2, 3, 0]); + +say "\nExample 43:"; +solution([0, 0, 1, 2]); + +say "\nExample 5:"; +solution([1, 2, 0, 3, 4]); |
