aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-322/packy-anderson/README.md2
-rw-r--r--challenge-322/packy-anderson/blog.txt1
-rwxr-xr-xchallenge-322/packy-anderson/elixir/ch-1.exs34
-rwxr-xr-xchallenge-322/packy-anderson/elixir/ch-2.exs35
-rwxr-xr-xchallenge-322/packy-anderson/perl/ch-1.pl30
-rwxr-xr-xchallenge-322/packy-anderson/perl/ch-2.pl27
-rwxr-xr-xchallenge-322/packy-anderson/python/ch-1.py27
-rwxr-xr-xchallenge-322/packy-anderson/python/ch-2.py27
-rwxr-xr-xchallenge-322/packy-anderson/raku/ch-1.raku30
-rwxr-xr-xchallenge-322/packy-anderson/raku/ch-2.raku27
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]);