diff options
| author | Packy Anderson <packy@cpan.org> | 2024-10-03 00:39:37 -0400 |
|---|---|---|
| committer | Packy Anderson <packy@cpan.org> | 2024-10-03 00:39:37 -0400 |
| commit | 49c055ebfc0616a848f5fa5e485ff5a46b2d517a (patch) | |
| tree | 19db2b8406aecad23474a868ccbaf1286df71d68 | |
| parent | 3114956979e812e8ca166cb84d8dea346cee22d3 (diff) | |
| download | perlweeklychallenge-club-49c055ebfc0616a848f5fa5e485ff5a46b2d517a.tar.gz perlweeklychallenge-club-49c055ebfc0616a848f5fa5e485ff5a46b2d517a.tar.bz2 perlweeklychallenge-club-49c055ebfc0616a848f5fa5e485ff5a46b2d517a.zip | |
Challenge 289 solutions by Packy Anderson
* Raku that maybe looks like Raku, but mostly like Perl
* Perl
* Python that definitely looks like Perl
* Elixir that's starting to look like Elixir
1 Blog post
| -rw-r--r-- | challenge-289/packy-anderson/README.md | 2 | ||||
| -rw-r--r-- | challenge-289/packy-anderson/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-289/packy-anderson/elixir/ch-1.exs | 49 | ||||
| -rwxr-xr-x | challenge-289/packy-anderson/elixir/ch-2.exs | 80 | ||||
| -rwxr-xr-x | challenge-289/packy-anderson/perl/ch-1.pl | 28 | ||||
| -rwxr-xr-x | challenge-289/packy-anderson/perl/ch-2.pl | 76 | ||||
| -rwxr-xr-x | challenge-289/packy-anderson/python/ch-1.py | 27 | ||||
| -rwxr-xr-x | challenge-289/packy-anderson/python/ch-2.py | 72 | ||||
| -rwxr-xr-x | challenge-289/packy-anderson/raku/ch-1.raku | 26 | ||||
| -rwxr-xr-x | challenge-289/packy-anderson/raku/ch-2.raku | 74 |
10 files changed, 434 insertions, 1 deletions
diff --git a/challenge-289/packy-anderson/README.md b/challenge-289/packy-anderson/README.md index 6e22767c9c..eed6d2f191 100644 --- a/challenge-289/packy-anderson/README.md +++ b/challenge-289/packy-anderson/README.md @@ -22,4 +22,4 @@ ## Blog Post -[Perl Weekly Challenge: A Man, A Plan, A Canal... PANAMA!](https://packy.dardan.com/b/Rm) +[Perl Weekly Challenge: Tirhd Mumixam Prel Welkey Caelhlnge](https://packy.dardan.com/b/Ry) diff --git a/challenge-289/packy-anderson/blog.txt b/challenge-289/packy-anderson/blog.txt new file mode 100644 index 0000000000..3b4bc9ee5b --- /dev/null +++ b/challenge-289/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/b/Ry
\ No newline at end of file diff --git a/challenge-289/packy-anderson/elixir/ch-1.exs b/challenge-289/packy-anderson/elixir/ch-1.exs new file mode 100755 index 0000000000..65e42064c8 --- /dev/null +++ b/challenge-289/packy-anderson/elixir/ch-1.exs @@ -0,0 +1,49 @@ +#!/usr/bin/env elixir + +defmodule Bag do + @moduledoc """ + Generates a Multiset/Bag implemented as a Map. + + https://en.wikipedia.org/wiki/Multiset + """ + @moduledoc since: "1.0.0" + + @doc """ + Generates a Bag from an enum. + """ + def from_enum(enum) do + {_, bag} = Enum.map_reduce(enum, %{}, fn i, bag -> + { i, Map.put(bag, i, Map.get(bag, i, 0) + 1) } + end) + bag + end +end + +defmodule PWC do + def thirdMaximum(ints) do + distinct = Bag.from_enum(ints) # get distinct integers + if (length(Map.keys(distinct)) < 3) do + # not enough values for a third max, return max + Enum.max(Map.keys(distinct)) + else + # sort and then get the third from the end + Map.keys(distinct) + |> Enum.sort(:desc) + |> Enum.at(2) + end + end + + def solution(ints) do + IO.puts("Input: @ints = (" <> Enum.join(ints, ", ") <> ")") + IO.puts("Output: #{ thirdMaximum(ints) }") + end +end + +IO.puts("Example 1:") +PWC.solution([5, 6, 4, 1]) + +IO.puts("\nExample 2:") +PWC.solution([4, 5]) + +IO.puts("\nExample 3:") +PWC.solution([1, 2, 2, 3]) diff --git a/challenge-289/packy-anderson/elixir/ch-2.exs b/challenge-289/packy-anderson/elixir/ch-2.exs new file mode 100755 index 0000000000..b25f2a62de --- /dev/null +++ b/challenge-289/packy-anderson/elixir/ch-2.exs @@ -0,0 +1,80 @@ +#!/usr/bin/env elixir + +Mix.install([ :excribe ]) +defmodule PWC do + def reassemble([], _, word), do: word + + def reassemble([c | remaining], letters, word) do + cond do + Regex.match?(~r/\p{L}/u, c) -> + # if it's a letter, pull it's replacement + # off the letters array + {next, letters} = List.pop_at(letters, 0) + reassemble(remaining, letters, word <> next) + true -> + # otherwise, it's punctuation, so + # leave it alone + reassemble(remaining, letters, word <> c) + end + end + + def rearrange(word) when length(word) < 3, do: word + + def rearrange(word) do + chars = String.codepoints(word) # break word into characters + letters = Enum.filter( + chars, + fn c -> Regex.match?(~r/\p{L}/u, c) end # only letters + ) + {first, letters} = List.pop_at(letters, 0) # remove the first letter + {last, letters} = List.pop_at(letters,-1) # remove the last letter + letters = Enum.shuffle(letters) # shuffle the remaining letters + |> List.insert_at(0, first) # put the first letter back + |> List.insert_at(-1, last) # put the last letter back + + # reassemble the word + reassemble(chars, letters, "") + end + + def jumbleLetters(text) do + String.split(text) + |> Enum.map(fn word -> rearrange(word) end) + |> Enum.join(" ") + end + + def solution(text) do + IO.puts("Input:") + IO.puts(Excribe.format(text, %{width: 65})) + jumbled = jumbleLetters(text) + IO.puts("\nOutput:") + IO.puts(Excribe.format(jumbled, %{width: 65})) + end +end + +IO.puts("Example 1:") +PWC.solution( + "According to research at Cambridge University, it doesn't " <> + "matter in what order the letters in a word are, the only " <> + "important thing is that the first and last letter be at " <> + "the right place. The rest can be a total mess and you can " <> + "still read it without problem. This is because the human " <> + "mind does not read every letter by itself, but the word as " <> + "a whole." +) + +IO.puts("\nExample 2:") +PWC.solution("Perl Weekly Challenge") + +IO.puts("\nExample 3:") +PWC.solution( + "’Twas brillig, and the slithy toves " <> + "Did gyre and gimble in the wabe: " <> + "All mimsy were the borogoves, " <> + "And the mome raths outgrabe." +) + +IO.puts("\nExample 4:") +PWC.solution( + "My mother-in-law says I shouldn't've have used " <> + "so many parentheticals (but I completely disagree!)." +) diff --git a/challenge-289/packy-anderson/perl/ch-1.pl b/challenge-289/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..2a29c9229d --- /dev/null +++ b/challenge-289/packy-anderson/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/env perl +use v5.40; + +use List::AllUtils qw( max ); + +sub thirdMaximum(@ints) { + my %distinct; $distinct{$_}=1 for @ints; # get distinct integers + if ((keys %distinct) < 3) { + # not enough values for a third max, return max + return max(keys %distinct); + } + # sort and then get the third from the end + return( (sort keys %distinct)[-3] ); +} + +sub solution($ints) { + say 'Input: @arr = (' . join(', ', @$ints) . ')'; + say 'Output: ' . thirdMaximum(@$ints); +} + +say "Example 1:"; +solution([5, 6, 4, 1]); + +say "\nExample 2:"; +solution([4, 5]); + +say "\nExample 3:"; +solution([1, 2, 2, 3]); diff --git a/challenge-289/packy-anderson/perl/ch-2.pl b/challenge-289/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..05cbb47750 --- /dev/null +++ b/challenge-289/packy-anderson/perl/ch-2.pl @@ -0,0 +1,76 @@ +#!/usr/bin/env perl +use v5.40; + +use List::AllUtils qw( shuffle ); +use Text::Wrap; + +sub rearrange($word) { + return $word unless length($word) > 2; + my @chars = split //, $word; # break word into characters + my @letters = grep {/\p{L}/} @chars; # only letters + my $first = shift @letters; # remove the first letter + my $last = pop @letters; # remove the last letter + @letters = shuffle @letters; # shuffle the remaining letters + unshift @letters, $first; # put the first letter back + push @letters, $last; # put the last letter back + + # reassemble the word + $word = ''; + # loop over the char list + foreach my $i (0 .. $#chars) { + if ( $chars[$i] =~ /\p{L}/ ) { + # if it's a letter, pull it's replacement + # off the @letters array + $word .= shift @letters; + } + else { + # otherwise, it's punctuation, so + # leave it alone + $word .= $chars[$i]; + } + } + + return $word; +} + +sub jumbleLetters($text) { + my @words = split /\s+/, $text; + foreach my $word (@words) { + $word = rearrange($word); + } + return join(" ", @words); +} + +sub solution($text) { + $Text::Wrap::columns = 65; + say 'Input:'; + say wrap('', '', $text); + my $jumbled = jumbleLetters($text); + say "\nOutput:"; + say wrap('', '', $jumbled); + say ""; +} + +say "Example 1:"; +solution( + "According to research at Cambridge University, it doesn't " . + "matter in what order the letters in a word are, the only " . + "important thing is that the first and last letter be at " . + "the right place. The rest can be a total mess and you can " . + "still read it without problem. This is because the human " . + "mind does not read every letter by itself, but the word as " . + "a whole." +); + +say "\nExample 2:"; +solution("Perl Weekly Challenge"); + +say "\nExample 3:"; +solution("’Twas brillig, and the slithy toves +Did gyre and gimble in the wabe: +All mimsy were the borogoves, +And the mome raths outgrabe."); + +say "\nExample 4:"; +solution("My mother-in-law says I shouldn't've have used +so many parentheticals (but I completely disagree!)."); diff --git a/challenge-289/packy-anderson/python/ch-1.py b/challenge-289/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..2a2cce0e83 --- /dev/null +++ b/challenge-289/packy-anderson/python/ch-1.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python + +from collections import Counter + +def thirdMaximum(ints): + distinct = Counter(ints) # get distinct integers + if len(list(distinct)) < 3: + # not enough values for a third max, return max + return max(list(distinct)) + # sort and then get the third from the end + return sorted(list(distinct))[-3] + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def solution(ints): + print(f'Input: @arr = ({comma_join(ints)})') + print(f'Output: { thirdMaximum(ints) }') + +print('Example 1:') +solution([5, 6, 4, 1]) + +print('\nExample 2:') +solution([4, 5]) + +print('\nExample 3:') +solution([1, 2, 2, 3]) diff --git a/challenge-289/packy-anderson/python/ch-2.py b/challenge-289/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..32d6b98bb5 --- /dev/null +++ b/challenge-289/packy-anderson/python/ch-2.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python + +from textwrap import fill +from random import shuffle +from regex import match + +def rearrange(word): + if len(word) < 3: + return word + + chars = [ c for c in word ] # break word into characters + letters = list(filter(lambda c: match(r"\p{L}", c), chars)) # only letters + first = letters.pop(0); # remove the first letter + last = letters.pop(-1); # remove the last letter + shuffle(letters) # shuffle the remaining letters + letters.insert(0, first) # put the first letter back + letters.append(last) # put the last letter back + + # reassemble the word + word = '' + # loop over the char list + for i in range(len(chars)): + if match(r"\p{L}", chars[i]): + # if it's a letter, pull it's replacement + # off the @letters array + word += letters.pop(0) + else: + # otherwise, it's punctuation, so + # leave it alone + word += chars[i] + + return word + +def jumbleLetters(text): + words = text.split() + words[:] = [ rearrange(word) for word in words ] + return " ".join(words) + +def solution(text): + print('Input:') + print(fill(text, width=65)) + jumbled = jumbleLetters(text) + print('\nOutput:') + print(fill(jumbled, width=65)) + +print('Example 1:') +solution( + "According to research at Cambridge University, it doesn't " + + "matter in what order the letters in a word are, the only " + + "important thing is that the first and last letter be at " + + "the right place. The rest can be a total mess and you can " + + "still read it without problem. This is because the human " + + "mind does not read every letter by itself, but the word as " + + "a whole." +) + +print('\nExample 2:') +solution("Perl Weekly Challenge") + +print('\nExample 3:') +solution( + "’Twas brillig, and the slithy toves " + + "Did gyre and gimble in the wabe: " + + "All mimsy were the borogoves, " + + "And the mome raths outgrabe." +) + +print('\nExample 4:') +solution( + "My mother-in-law says I shouldn't've have used " + + "so many parentheticals (but I completely disagree!)." +) diff --git a/challenge-289/packy-anderson/raku/ch-1.raku b/challenge-289/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..926984fc2d --- /dev/null +++ b/challenge-289/packy-anderson/raku/ch-1.raku @@ -0,0 +1,26 @@ +#!/usr/bin/env raku +use v6; + +sub thirdMaximum(@ints) { + my %distinct = @ints.Bag; # get distinct integers + if (%distinct.elems < 3) { + # not enough values for a third max, return max + return %distinct.keys.max(); + } + # sort and then get the third from the end + return( (%distinct.keys.sort)[*-3] ); +} + +sub solution(@ints) { + say 'Input: @arr = (' ~ @ints.join(', ') ~ ')'; + say 'Output: ' ~ thirdMaximum(@ints); +} + +say "Example 1:"; +solution([5, 6, 4, 1]); + +say "\nExample 2:"; +solution([4, 5]); + +say "\nExample 3:"; +solution([1, 2, 2, 3]); diff --git a/challenge-289/packy-anderson/raku/ch-2.raku b/challenge-289/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..7e931bded9 --- /dev/null +++ b/challenge-289/packy-anderson/raku/ch-2.raku @@ -0,0 +1,74 @@ +#!/usr/bin/env raku +use v6; + +use Text::Wrap; + +sub rearrange($word is copy) { + return $word unless $word.chars > 2; + my @chars = $word.comb; # break word into characters + my @letters = @chars.grep(/ <:L> /); # only letters + my $first = @letters.shift; # remove the first letter + my $last = @letters.pop; # remove the last letter + @letters .= pick(*); # shuffle the remaining letters + @letters.unshift($first); # put the first letter back + @letters.push($last); # put the last letter back + + # reassemble the word + $word = ''; + # loop over the char list + for 0 .. @chars.end -> $i { + if ( @chars[$i] ~~ / <:L> / ) { + # if it's a letter, pull it's replacement + # off the @letters array + $word ~= @letters.shift; + } + else { + # otherwise, it's punctuation, so + # leave it alone + $word ~= @chars[$i]; + } + } + + return $word; +} + +sub jumbleLetters($text) { + my @words = $text.comb(/\S+/); + for @words <-> $word { + $word = rearrange($word); + } + return @words.join(" "); +} + +sub solution($text) { + say 'Input:'; + say wrap-text($text, :width(65)); + my $jumbled = jumbleLetters($text); + say "\nOutput:"; + say wrap-text($jumbled, :width(65)); + say ""; +} + +say "Example 1:"; +solution( + "According to research at Cambridge University, it doesn't " ~ + "matter in what order the letters in a word are, the only " ~ + "important thing is that the first and last letter be at " ~ + "the right place. The rest can be a total mess and you can " ~ + "still read it without problem. This is because the human " ~ + "mind does not read every letter by itself, but the word as " ~ + "a whole." +); + +say "\nExample 2:"; +solution("Perl Weekly Challenge"); + +say "\nExample 3:"; +solution("’Twas brillig, and the slithy toves +Did gyre and gimble in the wabe: +All mimsy were the borogoves, +And the mome raths outgrabe."); + +say "\nExample 4:"; +solution("My mother-in-law says I shouldn't've have used +so many parentheticals (but I completely disagree!)."); |
