diff options
| author | Packy Anderson <packy@cpan.org> | 2025-09-30 20:15:20 -0400 |
|---|---|---|
| committer | Packy Anderson <packy@cpan.org> | 2025-09-30 20:15:20 -0400 |
| commit | 9103f0d2752aafd44d250aea368defe79e2c22cd (patch) | |
| tree | 829fa930eb635f98f6651aaefed1b8f6e2f2a86b | |
| parent | b240c9f118f051cc9716f673a592295719c62034 (diff) | |
| download | perlweeklychallenge-club-9103f0d2752aafd44d250aea368defe79e2c22cd.tar.gz perlweeklychallenge-club-9103f0d2752aafd44d250aea368defe79e2c22cd.tar.bz2 perlweeklychallenge-club-9103f0d2752aafd44d250aea368defe79e2c22cd.zip | |
Challenge 341 solutions by Packy Anderson
* Raku that is finally Raku-ish
* Perl
* Python that kinda looks like Raku
* Elixir that is starting to look like Elixir
1 blog post
| -rw-r--r-- | challenge-341/packy-anderson/README.md | 2 | ||||
| -rw-r--r-- | challenge-341/packy-anderson/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-341/packy-anderson/elixir/ch-1.exs | 38 | ||||
| -rwxr-xr-x | challenge-341/packy-anderson/elixir/ch-2.exs | 22 | ||||
| -rwxr-xr-x | challenge-341/packy-anderson/perl/ch-1.pl | 28 | ||||
| -rwxr-xr-x | challenge-341/packy-anderson/perl/ch-2.pl | 22 | ||||
| -rwxr-xr-x | challenge-341/packy-anderson/python/ch-1.py | 38 | ||||
| -rwxr-xr-x | challenge-341/packy-anderson/python/ch-2.py | 26 | ||||
| -rwxr-xr-x | challenge-341/packy-anderson/raku/ch-1.raku | 28 | ||||
| -rwxr-xr-x | challenge-341/packy-anderson/raku/ch-2.raku | 22 |
10 files changed, 144 insertions, 83 deletions
diff --git a/challenge-341/packy-anderson/README.md b/challenge-341/packy-anderson/README.md index 1013ff24f7..704729b966 100644 --- a/challenge-341/packy-anderson/README.md +++ b/challenge-341/packy-anderson/README.md @@ -23,4 +23,4 @@ ## Blog Post -[Perl Weekly Challenge: Points? What's the diff?](https://packy.dardan.com/b/bN) +[Perl Weekly Challenge: Something just BROKE](https://packy.dardan.com/b/bw) diff --git a/challenge-341/packy-anderson/blog.txt b/challenge-341/packy-anderson/blog.txt index e69de29bb2..3c44456bdb 100644 --- a/challenge-341/packy-anderson/blog.txt +++ b/challenge-341/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/b/bw
\ No newline at end of file diff --git a/challenge-341/packy-anderson/elixir/ch-1.exs b/challenge-341/packy-anderson/elixir/ch-1.exs index 45a0ed2251..398e7b8b91 100755 --- a/challenge-341/packy-anderson/elixir/ch-1.exs +++ b/challenge-341/packy-anderson/elixir/ch-1.exs @@ -1,26 +1,44 @@ #!/usr/bin/env elixir defmodule PWC do + def broken_keys(str, []) do + # if there are no broken keys, + # we can type all the words + String.split(str) + |> Enum.count + end + + def broken_keys(str, keys) do + # build a character class + {:ok, regex} = Regex.compile( + "[" <> Enum.join(keys) <> "]", [:caseless] + ) + # count how many words don't match the class + String.split(str) + |> Enum.filter(fn word -> not Regex.match?(regex, word) end) + |> Enum.count + end - def solution(ints) do - IO.puts("Input: @ints = (" <> Enum.join(ints, ", ") <> ")") - {sign, explain} = PWC.productSign(ints) - IO.puts("Output: " <> to_string(sign) ) - IO.puts("\n" <> explain) + def solution(str, keys) do + keylist = keys + |> Enum.map(fn c -> "'#{c}'" end) + |> Enum.join(",") + IO.puts("Input: $str = '#{str}, @keys = (#{keylist})'") + IO.puts("Output: #{broken_keys(str, keys)}") end end IO.puts("Example 1:") -PWC.solution() +PWC.solution("Hello World", ["d"]) IO.puts("\nExample 2:") -PWC.solution() +PWC.solution("apple banana cherry", ["a", "e"]) IO.puts("\nExample 3:") -PWC.solution() +PWC.solution("Coding is fun", []) IO.puts("\nExample 4:") -PWC.solution() +PWC.solution("The Weekly Challenge", ["a","b"]) IO.puts("\nExample 5:") -PWC.solution() +PWC.solution("Perl and Python", ["p"]) diff --git a/challenge-341/packy-anderson/elixir/ch-2.exs b/challenge-341/packy-anderson/elixir/ch-2.exs index 45a0ed2251..710d80716a 100755 --- a/challenge-341/packy-anderson/elixir/ch-2.exs +++ b/challenge-341/packy-anderson/elixir/ch-2.exs @@ -1,26 +1,28 @@ #!/usr/bin/env elixir defmodule PWC do + def reverse_prefix(str, char) do + [a, b] = String.split(str, char, parts: 2) + char <> String.reverse(a) <> b + end - def solution(ints) do - IO.puts("Input: @ints = (" <> Enum.join(ints, ", ") <> ")") - {sign, explain} = PWC.productSign(ints) - IO.puts("Output: " <> to_string(sign) ) - IO.puts("\n" <> explain) + def solution(str, char) do + IO.puts("Input: $str = \"#{str}\", $char = \"#{char}\"") + IO.puts("Output: \"#{reverse_prefix(str, char)}\"") end end IO.puts("Example 1:") -PWC.solution() +PWC.solution("programming", "g") IO.puts("\nExample 2:") -PWC.solution() +PWC.solution("hello", "h") IO.puts("\nExample 3:") -PWC.solution() +PWC.solution("abcdefghij", "h") IO.puts("\nExample 4:") -PWC.solution() +PWC.solution("reverse", "s") IO.puts("\nExample 5:") -PWC.solution() +PWC.solution("perl","r") diff --git a/challenge-341/packy-anderson/perl/ch-1.pl b/challenge-341/packy-anderson/perl/ch-1.pl index 98a155f02b..5156de78b6 100755 --- a/challenge-341/packy-anderson/perl/ch-1.pl +++ b/challenge-341/packy-anderson/perl/ch-1.pl @@ -1,22 +1,34 @@ #!/usr/bin/env perl use v5.40; -sub solution(@arr) { - say 'Input: @arr = (' . join(', ', @arr) . ')'; - say 'Output: (' . join(', ', @arr) . ')'; +sub brokenKeys($str, @keys) { + my @words = split /\s+/, $str; + # if there are no broken keys, + # we can type all the words + return scalar(@words) if @keys == 0; + # build a character class + my $regex = '[' . join('', @keys) . ']'; + # count how many words don't match the class + return ( scalar( grep {! /$regex/i } @words) ); +} + +sub solution($str, $keys) { + my $keylist = join ",", map {"'$_'"} @$keys; + say "Input: \$str = '$str', \@keys = ($keylist)"; + say "Output: " . brokenKeys($str, @$keys); } say "Example 1:"; -solution(); +solution("Hello World", ["d"]); say "\nExample 2:"; -solution(); +solution("apple banana cherry", ["a", "e"]); say "\nExample 3:"; -solution(); +solution("Coding is fun", []); say "\nExample 4:"; -solution(); +solution("The Weekly Challenge", ["a","b"]); say "\nExample 5:"; -solution(); +solution("Perl and Python", ["p"]);
\ No newline at end of file diff --git a/challenge-341/packy-anderson/perl/ch-2.pl b/challenge-341/packy-anderson/perl/ch-2.pl index 98a155f02b..c925939b15 100755 --- a/challenge-341/packy-anderson/perl/ch-2.pl +++ b/challenge-341/packy-anderson/perl/ch-2.pl @@ -1,22 +1,28 @@ #!/usr/bin/env perl use v5.40; -sub solution(@arr) { - say 'Input: @arr = (' . join(', ', @arr) . ')'; - say 'Output: (' . join(', ', @arr) . ')'; +sub reversePrefix($str, $char) { + my $loc = index($str, $char) + 1; + reverse(substr($str, 0, $loc)) . substr($str, $loc); +} + +sub solution($str, $char) { + say qq{Input: \$str = "$str", \$char = "$char"}; + my $result = reversePrefix($str, $char); + say qq{Output: "$result"}; } say "Example 1:"; -solution(); +solution("programming", "g"); say "\nExample 2:"; -solution(); +solution("hello", "h"); say "\nExample 3:"; -solution(); +solution("abcdefghij", "h"); say "\nExample 4:"; -solution(); +solution("reverse", "s"); say "\nExample 5:"; -solution(); +solution("perl","r"); diff --git a/challenge-341/packy-anderson/python/ch-1.py b/challenge-341/packy-anderson/python/ch-1.py index 60b68c6163..ecd60e168f 100755 --- a/challenge-341/packy-anderson/python/ch-1.py +++ b/challenge-341/packy-anderson/python/ch-1.py @@ -1,28 +1,36 @@ #!/usr/bin/env python -def distinctAverages(nums): - pass - -def int_join(joiner, arr): - return joiner.join(map(lambda i: str(i), arr)) - -def solution(nums): - print(f'Input: @nums = ({int_join(", ", nums)})') - count, explain = distinctAverages(nums) - print(f'Output: {count}\n\n{explain}') +import re + +def broken_keys(mystr, keys): + words = mystr.split() + # if there are no broken keys, + # we can type all the words + if len(keys) == 0: return len(words) + # build a character class + regex = re.compile('[' + ''.join(keys) + ']', re.I) + # count how many words don't match the class + return( len([ + word for word in words if not re.search(regex, word) + ]) ) + +def solution(mystr, keys): + keylist = ",".join([ f"'{c}'" for c in keys ]) + print(f"Input: $str = '{mystr}', @keys = ({keylist})") + print(f"Output: {broken_keys(mystr, keys)}") print('Example 1:') -solution() +solution("Hello World", ["d"]) print('\nExample 2:') -solution() +solution("apple banana cherry", ["a", "e"]) print('\nExample 3:') -solution() +solution("Coding is fun", []) print('\nExample 4:') -solution() +solution("The Weekly Challenge", ["a","b"]) print('\nExample 5:') -solution() +solution("Perl and Python", ["p"]) diff --git a/challenge-341/packy-anderson/python/ch-2.py b/challenge-341/packy-anderson/python/ch-2.py index 60b68c6163..0265f90a2a 100755 --- a/challenge-341/packy-anderson/python/ch-2.py +++ b/challenge-341/packy-anderson/python/ch-2.py @@ -1,28 +1,24 @@ #!/usr/bin/env python -def distinctAverages(nums): - pass - -def int_join(joiner, arr): - return joiner.join(map(lambda i: str(i), arr)) - -def solution(nums): - print(f'Input: @nums = ({int_join(", ", nums)})') - count, explain = distinctAverages(nums) - print(f'Output: {count}\n\n{explain}') +def reverse_prefix(mystr, char): + loc = mystr.find(char) + 1 + return mystr[0:loc][::-1] + mystr[loc:] +def solution(mystr, char): + print(f'Input: $str = "{mystr}", $char = "{char}"') + print(f'Output: "{reverse_prefix(mystr, char)}"') print('Example 1:') -solution() +solution("programming", "g") print('\nExample 2:') -solution() +solution("hello", "h") print('\nExample 3:') -solution() +solution("abcdefghij", "h") print('\nExample 4:') -solution() +solution("reverse", "s") print('\nExample 5:') -solution() +solution("perl","r") diff --git a/challenge-341/packy-anderson/raku/ch-1.raku b/challenge-341/packy-anderson/raku/ch-1.raku index 53bd47e1a7..f400d13c4c 100755 --- a/challenge-341/packy-anderson/raku/ch-1.raku +++ b/challenge-341/packy-anderson/raku/ch-1.raku @@ -1,22 +1,34 @@ #!/usr/bin/env raku use v6; -sub solution(@arr) { - say 'Input: @arr = (' ~ @arr.join(', ') ~ ')'; - say 'Output: (' ~ @arr.join(', ') ~ ')'; +sub brokenKeys($str, @keys) { + my @words = $str.comb(/\S+/); + # if there are no broken keys, + # we can type all the words + return @words.elems if @keys == 0; + # build a character class + my $regex = '<[' ~ @keys.join ~ ']>'; + # count how many words don't match the class + return ( ( @words.grep({! /:i <$regex>/ }) ).elems ); +} + +sub solution($str, @keys) { + my $keys = @keys.map({"'$_'"}).join(","); + say "Input: \$str = '$str', \@keys = ($keys)"; + say "Output: " ~ brokenKeys($str, @keys); } say "Example 1:"; -solution(); +solution("Hello World", ["d"]); say "\nExample 2:"; -solution(); +solution("apple banana cherry", ["a", "e"]); say "\nExample 3:"; -solution(); +solution("Coding is fun", []); say "\nExample 4:"; -solution(); +solution("The Weekly Challenge", ["a","b"]); say "\nExample 5:"; -solution(); +solution("Perl and Python", ["p"]);
\ No newline at end of file diff --git a/challenge-341/packy-anderson/raku/ch-2.raku b/challenge-341/packy-anderson/raku/ch-2.raku index 53bd47e1a7..739c2d8fed 100755 --- a/challenge-341/packy-anderson/raku/ch-2.raku +++ b/challenge-341/packy-anderson/raku/ch-2.raku @@ -1,22 +1,28 @@ #!/usr/bin/env raku use v6; -sub solution(@arr) { - say 'Input: @arr = (' ~ @arr.join(', ') ~ ')'; - say 'Output: (' ~ @arr.join(', ') ~ ')'; +sub reversePrefix($str, $char) { + my $loc = $str.index($char) + 1; + $str.substr(0, $loc).flip ~ $str.substr($loc); +} + +sub solution($str, $char) { + say qq{Input: \$str = "$str", \$char = "$char"}; + my $result = reversePrefix($str, $char); + say qq{Output: "$result"}; } say "Example 1:"; -solution(); +solution("programming", "g"); say "\nExample 2:"; -solution(); +solution("hello", "h"); say "\nExample 3:"; -solution(); +solution("abcdefghij", "h"); say "\nExample 4:"; -solution(); +solution("reverse", "s"); say "\nExample 5:"; -solution(); +solution("perl","r"); |
