aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-278/packy-anderson/README.md2
-rw-r--r--challenge-278/packy-anderson/blog.txt1
-rwxr-xr-xchallenge-278/packy-anderson/elixir/ch-1.exs31
-rwxr-xr-xchallenge-278/packy-anderson/elixir/ch-2.exs30
-rwxr-xr-xchallenge-278/packy-anderson/perl/ch-1.pl30
-rwxr-xr-xchallenge-278/packy-anderson/perl/ch-2.pl32
-rwxr-xr-xchallenge-278/packy-anderson/python/ch-1.py29
-rwxr-xr-xchallenge-278/packy-anderson/python/ch-2.py26
-rwxr-xr-xchallenge-278/packy-anderson/raku/ch-1.raku28
-rwxr-xr-xchallenge-278/packy-anderson/raku/ch-2.raku32
10 files changed, 240 insertions, 1 deletions
diff --git a/challenge-278/packy-anderson/README.md b/challenge-278/packy-anderson/README.md
index 84812d2a96..738aeb6df0 100644
--- a/challenge-278/packy-anderson/README.md
+++ b/challenge-278/packy-anderson/README.md
@@ -22,4 +22,4 @@
## Blog Post
-["They call me the count, because I love to count pairs! Ah, ah, ah!"](https://wp.me/p2aV3x-m0)
+[Perl Weekly Challenge: Word Reverse String Sort](https://packy.dardan.com/b/P9)
diff --git a/challenge-278/packy-anderson/blog.txt b/challenge-278/packy-anderson/blog.txt
new file mode 100644
index 0000000000..461ee890cb
--- /dev/null
+++ b/challenge-278/packy-anderson/blog.txt
@@ -0,0 +1 @@
+https://packy.dardan.com/b/P9 \ No newline at end of file
diff --git a/challenge-278/packy-anderson/elixir/ch-1.exs b/challenge-278/packy-anderson/elixir/ch-1.exs
new file mode 100755
index 0000000000..ff16ba0b63
--- /dev/null
+++ b/challenge-278/packy-anderson/elixir/ch-1.exs
@@ -0,0 +1,31 @@
+#!/usr/bin/env elixir
+
+defmodule PWC do
+ def sortString(str) do
+ str
+ |> String.split
+ |> Enum.map(fn(w) ->
+ [_, word, order] = Regex.run(~r/(\D+)(\d+)/, w)
+ %{order: order, word: word}
+ end)
+ |> Enum.sort_by( &( Integer.parse(&1[:order]) ) )
+ |> Enum.map_join(" ", &( &1[:word] ))
+ end
+
+ def solution(str) do
+ IO.puts("Input: $str = \"#{str}\"")
+ IO.puts("Output: \"" <> sortString(str) <> "\"")
+ end
+end
+
+IO.puts("Example 1:")
+PWC.solution("and2 Raku3 cousins5 Perl1 are4")
+
+IO.puts("\nExample 2:")
+PWC.solution("guest6 Python1 most4 the3 popular5 is2 language7")
+
+IO.puts("\nExample 3:")
+PWC.solution("Challenge3 The1 Weekly2")
+
+IO.puts("\nExample 3:")
+PWC.solution("The1 brown9 dog10 fox4 jumped5 lazy8 over6 quick2 red3 the7")
diff --git a/challenge-278/packy-anderson/elixir/ch-2.exs b/challenge-278/packy-anderson/elixir/ch-2.exs
new file mode 100755
index 0000000000..bffb7ec9d3
--- /dev/null
+++ b/challenge-278/packy-anderson/elixir/ch-2.exs
@@ -0,0 +1,30 @@
+#!/usr/bin/env elixir
+
+defmodule PWC do
+ def reverseWord(str, char) do
+ if String.contains?(str, char) do
+ [part1, part2] = String.split(str, char, parts: 2)
+ part1 = part1 <> char # put back the char from the split
+ |> String.graphemes
+ |> Enum.sort
+ |> Enum.join
+ Enum.join([part1, part2])
+ else
+ str
+ end
+ end
+
+ def solution(str, char) do
+ IO.puts("Input: $str = \"#{str}\", $char = \"#{char}\"")
+ IO.puts("Output: \"" <> reverseWord(str, char) <> "\"" )
+ end
+end
+
+IO.puts("Example 1:")
+PWC.solution("challenge", "e")
+
+IO.puts("\nExample 2:")
+PWC.solution("programming", "a")
+
+IO.puts("\nExample 3:")
+PWC.solution("champion", "b")
diff --git a/challenge-278/packy-anderson/perl/ch-1.pl b/challenge-278/packy-anderson/perl/ch-1.pl
new file mode 100755
index 0000000000..28c58f48af
--- /dev/null
+++ b/challenge-278/packy-anderson/perl/ch-1.pl
@@ -0,0 +1,30 @@
+#!/usr/bin/env perl
+use v5.40;
+
+sub sortString($str) {
+ my %words;
+ foreach my $w ( split /\s+/, $str ) {
+ $w ~~ / (?<word>\D+) (?<order>\d+) /x;
+ $words{$+{order}} = $+{word};
+ }
+ return join q{ }, map {
+ $words{$_}
+ } sort { $a <=> $b } keys %words;
+}
+
+sub solution($str) {
+ say qq{Input: \$str = "$str"};
+ say 'Output: "' . sortString($str) . '"';
+}
+
+say "Example 1:";
+solution("and2 Raku3 cousins5 Perl1 are4");
+
+say "\nExample 2:";
+solution("guest6 Python1 most4 the3 popular5 is2 language7");
+
+say "\nExample 3:";
+solution("Challenge3 The1 Weekly2");
+
+say "\nExample 4:";
+solution("The1 brown9 dog10 fox4 jumped5 lazy8 over6 quick2 red3 the7");
diff --git a/challenge-278/packy-anderson/perl/ch-2.pl b/challenge-278/packy-anderson/perl/ch-2.pl
new file mode 100755
index 0000000000..96941c1f23
--- /dev/null
+++ b/challenge-278/packy-anderson/perl/ch-2.pl
@@ -0,0 +1,32 @@
+#!/usr/bin/env perl
+use v5.40;
+
+sub reverseWord($str, $char) {
+ my $pos = index($str, $char);
+
+ # if the character isn't in the string, do nothing
+ return $str unless $pos >= 0;
+
+ my @parts = (
+ substr($str, 0, $pos+1),
+ substr($str, $pos+1)
+ );
+
+ $parts[0] = join(q{}, sort split //, $parts[0]);
+
+ return join(q{}, @parts);
+}
+
+sub solution($str, $char) {
+ say qq{Input: \$str = "$str", \$char = "$char"};
+ say 'Output: "' . reverseWord($str, $char) . '"';
+}
+
+say "Example 1:";
+solution("challenge", "e");
+
+say "\nExample 2:";
+solution("programming", "a");
+
+say "\nExample 3:";
+solution("champion", "b");
diff --git a/challenge-278/packy-anderson/python/ch-1.py b/challenge-278/packy-anderson/python/ch-1.py
new file mode 100755
index 0000000000..68061862f0
--- /dev/null
+++ b/challenge-278/packy-anderson/python/ch-1.py
@@ -0,0 +1,29 @@
+#!/usr/bin/env python
+
+import re
+
+def sortString(str):
+ words = {}
+ for w in str.split():
+ m = re.search('(?P<word>\D+)(?P<order>\d+)', w)
+ words[m.group('order')] = m.group('word')
+
+ return (' '.join([
+ words[k] for k in sorted(words.keys(), key=int)
+ ]))
+
+def solution(str):
+ print(f'Input: $str = "{str}"')
+ print(f'Output: "{sortString(str)}"')
+
+print('Example 1:')
+solution("and2 Raku3 cousins5 Perl1 are4")
+
+print('\nExample 2:')
+solution("guest6 Python1 most4 the3 popular5 is2 language7")
+
+print('\nExample 3:')
+solution("Challenge3 The1 Weekly2")
+
+print('\nExample 4:')
+solution("The1 brown9 dog10 fox4 jumped5 lazy8 over6 quick2 red3 the7")
diff --git a/challenge-278/packy-anderson/python/ch-2.py b/challenge-278/packy-anderson/python/ch-2.py
new file mode 100755
index 0000000000..863bb90365
--- /dev/null
+++ b/challenge-278/packy-anderson/python/ch-2.py
@@ -0,0 +1,26 @@
+#!/usr/bin/env python
+
+def reverseWord(str, char):
+ pos = str.find(char)
+
+ # if the character isn't in the string, do nothing
+ if pos < 0: return str
+
+ parts = [ str[0:pos+1], str[pos+1:] ]
+
+ parts[0] = ''.join(sorted(list(parts[0])))
+
+ return ''.join(parts)
+
+def solution(str, char):
+ print(f'Input: $str = "{str}", $char = "{char}"')
+ print(f'Output: "{reverseWord(str, char)}"')
+
+print('Example 1:')
+solution("challenge", "e")
+
+print('\nExample 2:')
+solution("programming", "a")
+
+print('\nExample 3:')
+solution("champion", "b")
diff --git a/challenge-278/packy-anderson/raku/ch-1.raku b/challenge-278/packy-anderson/raku/ch-1.raku
new file mode 100755
index 0000000000..42e81b433d
--- /dev/null
+++ b/challenge-278/packy-anderson/raku/ch-1.raku
@@ -0,0 +1,28 @@
+#!/usr/bin/env raku
+use v6;
+
+sub sortString($str) {
+ my %words;
+ for $str.comb(/\S+/) -> $w {
+ $w ~~ / $<word> = (\D+) $<order> = (\d+) /;
+ %words{~$<order>} = ~$<word>;
+ }
+ return %words.sort(*.key.Int)>>.values.join(q{ });
+}
+
+sub solution($str) {
+ say qq{Input: \$str = "$str"};
+ say 'Output: "' ~ sortString($str) ~ '"';
+}
+
+say "Example 1:";
+solution("and2 Raku3 cousins5 Perl1 are4");
+
+say "\nExample 2:";
+solution("guest6 Python1 most4 the3 popular5 is2 language7");
+
+say "\nExample 3:";
+solution("Challenge3 The1 Weekly2");
+
+say "\nExample 4:";
+solution("The1 brown9 dog10 fox4 jumped5 lazy8 over6 quick2 red3 the7");
diff --git a/challenge-278/packy-anderson/raku/ch-2.raku b/challenge-278/packy-anderson/raku/ch-2.raku
new file mode 100755
index 0000000000..7ce0c0190f
--- /dev/null
+++ b/challenge-278/packy-anderson/raku/ch-2.raku
@@ -0,0 +1,32 @@
+#!/usr/bin/env raku
+use v6;
+
+sub reverseWord($str, $char) {
+ my $pos = $str.index($char);
+
+ # if the character isn't in the string, do nothing
+ return $str unless $pos.defined;
+
+ my @parts = (
+ substr($str, 0, $pos+1),
+ substr($str, $pos+1)
+ );
+
+ @parts[0] = @parts[0].comb.sort.join;
+
+ return @parts.join;
+}
+
+sub solution($str, $char) {
+ say qq{Input: \$str = "$str", \$char = "$char"};
+ say 'Output: "' ~ reverseWord($str, $char) ~ '"';
+}
+
+say "Example 1:";
+solution("challenge", "e");
+
+say "\nExample 2:";
+solution("programming", "a");
+
+say "\nExample 3:";
+solution("champion", "b");