diff options
| author | Packy Anderson <packy@cpan.org> | 2025-10-20 21:05:13 -0400 |
|---|---|---|
| committer | Packy Anderson <packy@cpan.org> | 2025-10-20 21:05:13 -0400 |
| commit | 5bd4aa5c559492819e376a2c5c02a07c0405e67e (patch) | |
| tree | 4207162c35225a3b77035f313da2b3d56e1288a2 | |
| parent | 4ddca8aa5ba1d40a9764f3a0c764e8ab38f3638f (diff) | |
| download | perlweeklychallenge-club-5bd4aa5c559492819e376a2c5c02a07c0405e67e.tar.gz perlweeklychallenge-club-5bd4aa5c559492819e376a2c5c02a07c0405e67e.tar.bz2 perlweeklychallenge-club-5bd4aa5c559492819e376a2c5c02a07c0405e67e.zip | |
Challenge 344 solutions by Packy Anderson
* Raku that is finally Raku-ish
* Perl
* Python that kinda looks like Raku
* Elixir that is starting to look like Elixir
1 blog post
| -rw-r--r-- | challenge-344/packy-anderson/README.md | 2 | ||||
| -rw-r--r-- | challenge-344/packy-anderson/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-344/packy-anderson/elixir/ch-1.exs | 34 | ||||
| -rwxr-xr-x | challenge-344/packy-anderson/elixir/ch-2.exs | 53 | ||||
| -rwxr-xr-x | challenge-344/packy-anderson/perl/ch-1.pl | 28 | ||||
| -rwxr-xr-x | challenge-344/packy-anderson/perl/ch-2.pl | 36 | ||||
| -rwxr-xr-x | challenge-344/packy-anderson/python/ch-1.py | 24 | ||||
| -rwxr-xr-x | challenge-344/packy-anderson/python/ch-2.py | 31 | ||||
| -rwxr-xr-x | challenge-344/packy-anderson/raku/ch-1.raku | 26 | ||||
| -rwxr-xr-x | challenge-344/packy-anderson/raku/ch-2.raku | 33 |
10 files changed, 267 insertions, 1 deletions
diff --git a/challenge-344/packy-anderson/README.md b/challenge-344/packy-anderson/README.md index 57c0477cd1..dfffb8191c 100644 --- a/challenge-344/packy-anderson/README.md +++ b/challenge-344/packy-anderson/README.md @@ -23,4 +23,4 @@ ## Blog Post -[Perl Weekly Challenge: Ze-ro the CHAMPIONS!](https://packy.dardan.com/b/cg) +[Perl Weekly Challenge: A-ray Sunshine!](https://packy.dardan.com/b/ct) diff --git a/challenge-344/packy-anderson/blog.txt b/challenge-344/packy-anderson/blog.txt new file mode 100644 index 0000000000..0201a9aca3 --- /dev/null +++ b/challenge-344/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/b/ct
\ No newline at end of file diff --git a/challenge-344/packy-anderson/elixir/ch-1.exs b/challenge-344/packy-anderson/elixir/ch-1.exs new file mode 100755 index 0000000000..a47ca85f5d --- /dev/null +++ b/challenge-344/packy-anderson/elixir/ch-1.exs @@ -0,0 +1,34 @@ +#!/usr/bin/env elixir + +defmodule PWC do + def compute(ints, x) do + num = ints + |> Enum.join + |> String.to_integer + num + x + |> Integer.to_string + |> String.graphemes + end + + def solution(ints, x) do + joined = Enum.join(ints, ", ") + IO.puts("Input: @ints = (#{joined}), $x = #{x}") + joined = Enum.join(compute(ints, x), ", ") + IO.puts("Output: (#{joined})") + end +end + +IO.puts("Example 1:") +PWC.solution([1, 2, 3, 4], 12) + +IO.puts("\nExample 2:") +PWC.solution([2, 7, 4], 181) + +IO.puts("\nExample 3:") +PWC.solution([9, 9, 9], 1) + +IO.puts("\nExample 4:") +PWC.solution([1, 0, 0, 0, 0], 9999) + +IO.puts("\nExample 5:") +PWC.solution([0], 1000) diff --git a/challenge-344/packy-anderson/elixir/ch-2.exs b/challenge-344/packy-anderson/elixir/ch-2.exs new file mode 100755 index 0000000000..8668e599ab --- /dev/null +++ b/challenge-344/packy-anderson/elixir/ch-2.exs @@ -0,0 +1,53 @@ +#!/usr/bin/env elixir + +defmodule PWC do + def permutations([]), do: [[]] + + def permutations(list) do + for head <- list, rest <- permutations(list -- [head]), + do: [head|rest] + end + + def formation(source, target) do + source = Enum.map(source, fn s -> Enum.join(s, "") end) + target = Enum.join(target) + {_, result} = Enum.map_reduce( + permutations(source), + "false", + fn perm, result -> + { + perm, + cond do + Enum.join(perm) == target -> "true" + true -> result + end + } + end + ) + result + end + + def solution(source, target) do + arr = Enum.map( + source, fn s -> "[" <> Enum.join(s, ",") <> "]" end + ) + IO.puts("Input: @source = (" <> Enum.join(arr, ", ") <> ")") + IO.puts(" @target = (" <> Enum.join(target, ", ") <> ")") + IO.puts("Output: #{formation(source, target)}") + end +end + +IO.puts("Example 1:") +PWC.solution([[2,3], [1], [4]], [1, 2, 3, 4]) + +IO.puts("\nExample 2:") +PWC.solution([[1,3], [2,4]], [1, 2, 3, 4]) + +IO.puts("\nExample 3:") +PWC.solution([[9,1], [5,8], [2]], [5, 8, 2, 9, 1]) + +IO.puts("\nExample 4:") +PWC.solution([[1], [3]], [1, 2, 3]) + +IO.puts("\nExample 5:") +PWC.solution([[7,4,6]], [7,4,6]) diff --git a/challenge-344/packy-anderson/perl/ch-1.pl b/challenge-344/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..2260f02af7 --- /dev/null +++ b/challenge-344/packy-anderson/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/env perl +use v5.40; + +sub compute($ints, $x) { + split //, join('', @$ints) + $x; +} + +sub solution($ints, $x) { + my $joined = join(', ', @$ints); + say qq[Input: \@ints = ($joined), \$x = $x]; + $joined = join(', ', compute($ints, $x)); + say qq[Output: ($joined)]; +} + +say "Example 1:"; +solution([1, 2, 3, 4], 12); + +say "\nExample 2:"; +solution([2, 7, 4], 181); + +say "\nExample 3:"; +solution([9, 9, 9], 1); + +say "\nExample 4:"; +solution([1, 0, 0, 0, 0], 9999); + +say "\nExample 5:"; +solution([0], 1000); diff --git a/challenge-344/packy-anderson/perl/ch-2.pl b/challenge-344/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..e281d1ffd0 --- /dev/null +++ b/challenge-344/packy-anderson/perl/ch-2.pl @@ -0,0 +1,36 @@ +#!/usr/bin/env perl +use v5.40; + +use Algorithm::Combinatorics qw(permutations); + +sub formation($source, $target) { + my @source = map { join('', @$_) } @$source; + $target = join('', @$target); + my $iter = permutations(\@source); + while (my $perm = $iter->next) { + return 'true' if join('', @$perm) eq $target; + } + return 'false'; +} + +sub solution($source, $target) { + my @arr = map { '[' . join(',', @$_) . ']'} @$source; + say 'Input: @source = (' . join(', ', @arr) . ')'; + say ' @target = (' . join(', ', @$target) . ')'; + say 'Output: ' . formation($source, $target); +} + +say "Example 1:"; +solution([[2,3], [1], [4]], [1, 2, 3, 4]); + +say "\nExample 2:"; +solution([[1,3], [2,4]], [1, 2, 3, 4]); + +say "\nExample 3:"; +solution([[9,1], [5,8], [2]], [5, 8, 2, 9, 1]); + +say "\nExample 4:"; +solution([[1], [3]], [1, 2, 3]); + +say "\nExample 5:"; +solution([[7,4,6]], [7,4,6]); diff --git a/challenge-344/packy-anderson/python/ch-1.py b/challenge-344/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..143d6cf8da --- /dev/null +++ b/challenge-344/packy-anderson/python/ch-1.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python + +def compute(ints, x): + return list(str(int("".join(map(str, ints))) + x)) + +def solution(ints, x): + print(f'Input: @ints = ({"".join(map(str, ints))}), $x = {x}') + print(f'Output: ({"".join(map(str, compute(ints, x)))})') + + +print('Example 1:') +solution([1, 2, 3, 4], 12) + +print('\nExample 2:') +solution([2, 7, 4], 181) + +print('\nExample 3:') +solution([9, 9, 9], 1) + +print('\nExample 4:') +solution([1, 0, 0, 0, 0], 9999) + +print('\nExample 5:') +solution([0], 1000) diff --git a/challenge-344/packy-anderson/python/ch-2.py b/challenge-344/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..162159665f --- /dev/null +++ b/challenge-344/packy-anderson/python/ch-2.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python + +from itertools import permutations + +def formation(source, target): + source = [ "".join(map(str, s)) for s in source ] + target = "".join(map(str, target)) + for perm in permutations(source): + if "".join(perm) == target: return 'true' + return 'false' + +def solution(source, target): + arr = [ "[" + ",".join(map(str, s)) + "]" for s in source ] + print(f'Input: @source = ({arr})') + print(f' @target = ({", ".join(map(str, target))})') + print(f'Output: {formation(source, target)}') + +print('Example 1:') +solution([[2,3], [1], [4]], [1, 2, 3, 4]) + +print('\nExample 2:') +solution([[1,3], [2,4]], [1, 2, 3, 4]) + +print('\nExample 3:') +solution([[9,1], [5,8], [2]], [5, 8, 2, 9, 1]) + +print('\nExample 4:') +solution([[1], [3]], [1, 2, 3]) + +print('\nExample 5:') +solution([[7,4,6]], [7,4,6]) diff --git a/challenge-344/packy-anderson/raku/ch-1.raku b/challenge-344/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..b56c382d68 --- /dev/null +++ b/challenge-344/packy-anderson/raku/ch-1.raku @@ -0,0 +1,26 @@ +#!/usr/bin/env raku +use v6; + +sub compute(@ints, $x) { + (@ints.join('').Int + $x).Str.comb; +} + +sub solution(@ints, $x) { + say qq[Input: \@ints = ({ @ints.join(', ') }), \$x = $x]; + say qq[Output: ({ compute(@ints, $x).join(', ') })]; +} + +say "Example 1:"; +solution([1, 2, 3, 4], 12); + +say "\nExample 2:"; +solution([2, 7, 4], 181); + +say "\nExample 3:"; +solution([9, 9, 9], 1); + +say "\nExample 4:"; +solution([1, 0, 0, 0, 0], 9999); + +say "\nExample 5:"; +solution([0], 1000); diff --git a/challenge-344/packy-anderson/raku/ch-2.raku b/challenge-344/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..fb1aed33cf --- /dev/null +++ b/challenge-344/packy-anderson/raku/ch-2.raku @@ -0,0 +1,33 @@ +#!/usr/bin/env raku +use v6; + +sub formation(@source is copy, @target) { + @source = @source.map({ $_.join('') }); + my $target = @target.join(''); + for @source.permutations -> @perm { + return 'true' if @perm.join('') eq $target; + } + return 'false'; +} + +sub solution(@source, @target) { + my @arr = @source.map({ '[' ~ $_.join(',') ~ ']'}); + say 'Input: @source = (' ~ @arr.join(', ') ~ ')'; + say ' @target = (' ~ @target.join(', ') ~ ')'; + say 'Output: ' ~ formation(@source, @target); +} + +say "Example 1:"; +solution([[2,3], [1], [4]], [1, 2, 3, 4]); + +say "\nExample 2:"; +solution([[1,3], [2,4]], [1, 2, 3, 4]); + +say "\nExample 3:"; +solution([[9,1], [5,8], [2]], [5, 8, 2, 9, 1]); + +say "\nExample 4:"; +solution([[1], [3]], [1, 2, 3]); + +say "\nExample 5:"; +solution([[7,4,6]], [7,4,6]); |
