diff options
| author | Packy Anderson <packy@cpan.org> | 2025-05-24 14:29:33 -0400 |
|---|---|---|
| committer | Packy Anderson <packy@cpan.org> | 2025-05-24 14:29:33 -0400 |
| commit | 0af5f73f09593ee4c911f1eb8ff0733019ddb826 (patch) | |
| tree | 836572749c062223e2b1c17493fe1218f4361a2d | |
| parent | fe988cb221822fa23bc445bd25fe87682f2927ca (diff) | |
| download | perlweeklychallenge-club-0af5f73f09593ee4c911f1eb8ff0733019ddb826.tar.gz perlweeklychallenge-club-0af5f73f09593ee4c911f1eb8ff0733019ddb826.tar.bz2 perlweeklychallenge-club-0af5f73f09593ee4c911f1eb8ff0733019ddb826.zip | |
Challenge 322 solutions by Packy Anderson
* Raku that maybe looks like Raku, but mostly like Perl
* Perl
* Python that definitely looks like Perl
* Elixir that looks kinda like Elixir
1 blog post
| -rw-r--r-- | challenge-322/packy-anderson/README.md | 2 | ||||
| -rw-r--r-- | challenge-322/packy-anderson/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-322/packy-anderson/elixir/ch-1.exs | 34 | ||||
| -rwxr-xr-x | challenge-322/packy-anderson/elixir/ch-2.exs | 35 | ||||
| -rwxr-xr-x | challenge-322/packy-anderson/perl/ch-1.pl | 30 | ||||
| -rwxr-xr-x | challenge-322/packy-anderson/perl/ch-2.pl | 27 | ||||
| -rwxr-xr-x | challenge-322/packy-anderson/python/ch-1.py | 27 | ||||
| -rwxr-xr-x | challenge-322/packy-anderson/python/ch-2.py | 27 | ||||
| -rwxr-xr-x | challenge-322/packy-anderson/raku/ch-1.raku | 30 | ||||
| -rwxr-xr-x | challenge-322/packy-anderson/raku/ch-2.raku | 27 |
10 files changed, 239 insertions, 1 deletions
diff --git a/challenge-322/packy-anderson/README.md b/challenge-322/packy-anderson/README.md index 0d605b134b..766d853167 100644 --- a/challenge-322/packy-anderson/README.md +++ b/challenge-322/packy-anderson/README.md @@ -23,4 +23,4 @@ ## Blog Post -[Perl Weekly Challenge: Comparatively Distinct, but Great^H^H^H^H^HAverage](https://packy.dardan.com/b/Up) +[Perl Weekly Challenge: The array is rank, but the string is formatted](https://packy.dardan.com/b/VE) diff --git a/challenge-322/packy-anderson/blog.txt b/challenge-322/packy-anderson/blog.txt new file mode 100644 index 0000000000..fa3c8f3d58 --- /dev/null +++ b/challenge-322/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/b/VE
\ No newline at end of file diff --git a/challenge-322/packy-anderson/elixir/ch-1.exs b/challenge-322/packy-anderson/elixir/ch-1.exs new file mode 100755 index 0000000000..3fd3a8c96f --- /dev/null +++ b/challenge-322/packy-anderson/elixir/ch-1.exs @@ -0,0 +1,34 @@ +#!/usr/bin/env elixir + +defmodule PWC do + def strFormat(charlist, i, output) when length(charlist) <= i, + do: List.to_string(charlist) <> output + + def strFormat(charlist, i, output) do + chunk = Enum.take(charlist, -i) |> List.to_string + charlist = Enum.take(charlist, length(charlist)-i) + strFormat(charlist, i, "-" <> chunk <> output) + end + + def strFormat(str, i) do + str = String.replace(str, "-", "") + strFormat(String.graphemes(str), i, "") + end + + def solution(str, i) do + IO.puts("Input: $str = \"#{str}\", $i = #{i}") + IO.puts("Output: \"#{strFormat(str, i)}\"") + end +end + +IO.puts("Example 1:") +PWC.solution("ABC-D-E-F", 3) + +IO.puts("\nExample 2:") +PWC.solution("A-BC-D-E", 2) + +IO.puts("\nExample 3:") +PWC.solution("-A-B-CD-E", 4) + +IO.puts("\nExample 4:") +PWC.solution("-A-B-CD-E", 5) diff --git a/challenge-322/packy-anderson/elixir/ch-2.exs b/challenge-322/packy-anderson/elixir/ch-2.exs new file mode 100755 index 0000000000..6e15bca889 --- /dev/null +++ b/challenge-322/packy-anderson/elixir/ch-2.exs @@ -0,0 +1,35 @@ +#!/usr/bin/env elixir + +defmodule PWC do + defp makeRankMap(i, {rank, rankMap}) do + cond do + not Map.has_key?(rankMap, i) -> + rank = rank + 1 + {i, { rank, Map.put_new(rankMap, i, rank)} } + true -> + {i, { rank, rankMap } } + end + end + + def rankArray(ints) do + {_, {_, rankMap}} = Enum.map_reduce( + Enum.sort(ints), {0, %{}}, &makeRankMap/2 + ) + Enum.map(ints, fn i -> Map.get(rankMap, i) end) + end + + def solution(ints) do + IO.puts("Input: @ints = (" <> Enum.join(ints, ", ") <> ")") + ranks = rankArray(ints) + IO.puts("Output: (" <> Enum.join(ranks, ", ") <> ")") + end +end + +IO.puts("Example 1:") +PWC.solution([55, 22, 44, 33]) + +IO.puts("\nExample 2:") +PWC.solution([10, 10, 10]) + +IO.puts("\nExample 3:") +PWC.solution([5, 1, 1, 4, 3]) diff --git a/challenge-322/packy-anderson/perl/ch-1.pl b/challenge-322/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..128efe8d7c --- /dev/null +++ b/challenge-322/packy-anderson/perl/ch-1.pl @@ -0,0 +1,30 @@ +#!/usr/bin/env perl +use v5.40; + +sub strFormat($str, $i) { + $str =~ s/\-//g; + my $output = ''; + while (length($str) > $i) { + $output = "-" . substr($str, -$i) . $output; + substr($str, -$i) = ''; + } + $output = $str . $output; +} + +sub solution($str, $i) { + say qq[Input: \$str = "$str", \$i = $i]; + my $output = strFormat($str, $i); + say qq[Output: "$output"]; +} + +say "Example 1:"; +solution("ABC-D-E-F", 3); + +say "\nExample 2:"; +solution("A-BC-D-E", 2); + +say "\nExample 3:"; +solution("-A-B-CD-E", 4); + +say "\nExample 4:"; +solution("-A-B-CD-E", 5);
\ No newline at end of file diff --git a/challenge-322/packy-anderson/perl/ch-2.pl b/challenge-322/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..fba8309c23 --- /dev/null +++ b/challenge-322/packy-anderson/perl/ch-2.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl +use v5.40; + +sub rankArray(@ints) { + my $rank = 0; + my %rank; + foreach my $i (sort @ints) { + next if exists $rank{$i}; + $rank{$i} = ++$rank; + } + map { $rank{$_} } @ints; +} + +sub solution($ints) { + say 'Input: @ints = (' . join(', ', @$ints) . ')'; + my @ranks = rankArray(@$ints); + say 'Output: (' . join(', ', @ranks) . ')'; +} + +say "Example 1:"; +solution([55, 22, 44, 33]); + +say "\nExample 2:"; +solution([10, 10, 10]); + +say "\nExample 3:"; +solution([5, 1, 1, 4, 3]); diff --git a/challenge-322/packy-anderson/python/ch-1.py b/challenge-322/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..a42c02c26a --- /dev/null +++ b/challenge-322/packy-anderson/python/ch-1.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python + +def strFormat(strVar, i): + strVar = strVar.replace("-", "") + output = '' + while (len(strVar) > i): + output = "-" + strVar[-i:] + output + strVar = strVar[0:-i] # Python strings are IMMUTABLE + return strVar + output + +def solution(strVar, i): + print(f'Input: $str = "{strVar}", $i = {i}') + output = strFormat(strVar, i) + print(f'Output: "{output}"') + + +print('Example 1:') +solution("ABC-D-E-F", 3) + +print('\nExample 2:') +solution("A-BC-D-E", 2) + +print('\nExample 3:') +solution("-A-B-CD-E", 4) + +print('\nExample 4:') +solution("-A-B-CD-E", 5)
\ No newline at end of file diff --git a/challenge-322/packy-anderson/python/ch-2.py b/challenge-322/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..e0e0157f94 --- /dev/null +++ b/challenge-322/packy-anderson/python/ch-2.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python + +def rankArray(ints): + rankInt = 0 + rankDict = {} + for i in sorted(ints): + if i in rankDict: continue + rankInt += 1 + rankDict[i] = rankInt + return [ rankDict[i] for i in ints ] + +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(", ", rankArray(ints))})') + + +print('Example 1:') +solution([55, 22, 44, 33]) + +print('\nExample 2:') +solution([10, 10, 10]) + +print('\nExample 3:') +solution([5, 1, 1, 4, 3]) diff --git a/challenge-322/packy-anderson/raku/ch-1.raku b/challenge-322/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..afcf33ba40 --- /dev/null +++ b/challenge-322/packy-anderson/raku/ch-1.raku @@ -0,0 +1,30 @@ +#!/usr/bin/env raku +use v6; + +sub strFormat($str is copy, $i) { + $str = $str.subst("-", :g); + my $output = ''; + while ($str.chars > $i) { + $output = "-" ~ $str.substr(*-$i) ~ $output; + $str.substr-rw(*-$i) = ''; + } + $output = $str ~ $output; +} + +sub solution($str, $i) { + say qq[Input: \$str = "$str", \$i = $i]; + my $output = strFormat($str, $i); + say qq[Output: "$output"]; +} + +say "Example 1:"; +solution("ABC-D-E-F", 3); + +say "\nExample 2:"; +solution("A-BC-D-E", 2); + +say "\nExample 3:"; +solution("-A-B-CD-E", 4); + +say "\nExample 4:"; +solution("-A-B-CD-E", 5);
\ No newline at end of file diff --git a/challenge-322/packy-anderson/raku/ch-2.raku b/challenge-322/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..83ceaf313e --- /dev/null +++ b/challenge-322/packy-anderson/raku/ch-2.raku @@ -0,0 +1,27 @@ +#!/usr/bin/env raku +use v6; + +sub rankArray(@ints) { + my $rank = 0; + my %rank; + for @ints.sort() -> $i { + next if %rank{$i}:exists; + %rank{$i} = ++$rank; + } + @ints.map({ %rank{$_} }); +} + +sub solution(@ints) { + say 'Input: @ints = (' ~ @ints.join(', ') ~ ')'; + my @ranks = rankArray(@ints); + say 'Output: (' ~ @ranks.join(', ') ~ ')'; +} + +say "Example 1:"; +solution([55, 22, 44, 33]); + +say "\nExample 2:"; +solution([10, 10, 10]); + +say "\nExample 3:"; +solution([5, 1, 1, 4, 3]); |
