diff options
| author | Packy Anderson <packy@cpan.org> | 2024-07-19 21:13:39 -0400 |
|---|---|---|
| committer | Packy Anderson <packy@cpan.org> | 2024-07-19 21:13:39 -0400 |
| commit | d666b70a9e869f13fb16fbe2ffb609cfb394f0fa (patch) | |
| tree | 03ecea816cc9a0946f8509f8ed6b03643d36e5c8 | |
| parent | 4fd983bd163dfa634e5ae8b5d07255f14e33ad24 (diff) | |
| download | perlweeklychallenge-club-d666b70a9e869f13fb16fbe2ffb609cfb394f0fa.tar.gz perlweeklychallenge-club-d666b70a9e869f13fb16fbe2ffb609cfb394f0fa.tar.bz2 perlweeklychallenge-club-d666b70a9e869f13fb16fbe2ffb609cfb394f0fa.zip | |
Challenge 278 solutions by Packy Anderson
* Raku that maybe looks like Raku
* Perl
* Python that definitely looks like Perl
* Elixir
1 Blog post
| -rw-r--r-- | challenge-278/packy-anderson/README.md | 2 | ||||
| -rw-r--r-- | challenge-278/packy-anderson/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-278/packy-anderson/elixir/ch-1.exs | 31 | ||||
| -rwxr-xr-x | challenge-278/packy-anderson/elixir/ch-2.exs | 30 | ||||
| -rwxr-xr-x | challenge-278/packy-anderson/perl/ch-1.pl | 30 | ||||
| -rwxr-xr-x | challenge-278/packy-anderson/perl/ch-2.pl | 32 | ||||
| -rwxr-xr-x | challenge-278/packy-anderson/python/ch-1.py | 29 | ||||
| -rwxr-xr-x | challenge-278/packy-anderson/python/ch-2.py | 26 | ||||
| -rwxr-xr-x | challenge-278/packy-anderson/raku/ch-1.raku | 28 | ||||
| -rwxr-xr-x | challenge-278/packy-anderson/raku/ch-2.raku | 32 |
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"); |
