diff options
| -rw-r--r-- | challenge-275/packy-anderson/README.md | 2 | ||||
| -rw-r--r-- | challenge-275/packy-anderson/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-275/packy-anderson/elixir/ch-1.exs | 29 | ||||
| -rwxr-xr-x | challenge-275/packy-anderson/elixir/ch-2.exs | 36 | ||||
| -rwxr-xr-x | challenge-275/packy-anderson/perl/ch-1.pl | 26 | ||||
| -rwxr-xr-x | challenge-275/packy-anderson/perl/ch-2.pl | 35 | ||||
| -rwxr-xr-x | challenge-275/packy-anderson/python/ch-1.py | 27 | ||||
| -rwxr-xr-x | challenge-275/packy-anderson/python/ch-2.py | 28 | ||||
| -rwxr-xr-x | challenge-275/packy-anderson/raku/ch-1.raku | 26 | ||||
| -rwxr-xr-x | challenge-275/packy-anderson/raku/ch-2.raku | 35 |
10 files changed, 244 insertions, 1 deletions
diff --git a/challenge-275/packy-anderson/README.md b/challenge-275/packy-anderson/README.md index 30f565cde1..0975c778da 100644 --- a/challenge-275/packy-anderson/README.md +++ b/challenge-275/packy-anderson/README.md @@ -22,4 +22,4 @@ ## Blog Post -[Bus Route, Bus Goat, Under My Umbrellamaaaaaaaaaaaa](https://packy.dardan.com/b/Mq) +[Digitally Replace Broken Keys](https://packy.dardan.com/b/NA) diff --git a/challenge-275/packy-anderson/blog.txt b/challenge-275/packy-anderson/blog.txt new file mode 100644 index 0000000000..25f66a020d --- /dev/null +++ b/challenge-275/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/b/NA
\ No newline at end of file diff --git a/challenge-275/packy-anderson/elixir/ch-1.exs b/challenge-275/packy-anderson/elixir/ch-1.exs new file mode 100755 index 0000000000..1d8c332d04 --- /dev/null +++ b/challenge-275/packy-anderson/elixir/ch-1.exs @@ -0,0 +1,29 @@ +#!/usr/bin/env elixir + +defmodule PWC do + def brokenKeys(sentence, keys) do + {:ok, reg} = Regex.compile("[" <> Enum.join(keys) <> "]", [:caseless]) + String.split(sentence) + |> Enum.filter(fn word -> not Regex.match?(reg, word) end) + |> Enum.count + |> to_string + end + + def solution(sentence, keys) do + keysStr = "'" <> Enum.join(keys, "', '") <> "'" + IO.puts("Input: $sentence = \"#{sentence}\", @keys = (#{keysStr})") + IO.puts("Output: " <> brokenKeys(sentence, keys) ) + end +end + +IO.puts("Example 1:") +PWC.solution("Perl Weekly Challenge", ["l", "a"]) + +IO.puts("\nExample 2:") +PWC.solution("Perl and Raku", ["a"]) + +IO.puts("\nExample 3:") +PWC.solution("Well done Team PWC", ["l", "o"]) + +IO.puts("\nExample 4:") +PWC.solution("The joys of polyglottism", ["T"]) diff --git a/challenge-275/packy-anderson/elixir/ch-2.exs b/challenge-275/packy-anderson/elixir/ch-2.exs new file mode 100755 index 0000000000..c426dc0662 --- /dev/null +++ b/challenge-275/packy-anderson/elixir/ch-2.exs @@ -0,0 +1,36 @@ +#!/usr/bin/env elixir + +defmodule PWC do + def replaceDigits([], _, out), do: out + + def replaceDigits([c | rest], last, out) do + if Regex.match?(~r/[0-9]/, c) do + <<chrval::utf8>> = last + c = List.to_string([ chrval + String.to_integer(c) ]) + replaceDigits(rest, last, out <> c) + else + replaceDigits(rest, c, out <> c) + end + end + + def replaceDigits(str) do + replaceDigits(String.graphemes(str), nil, "") + end + + def solution(str) do + IO.puts("Input: $str = '#{str}'") + IO.puts("Output: '#{replaceDigits(str)}'") + end +end + +IO.puts("Example 1:") +PWC.solution("a1c1e1") + +IO.puts("\nExample 2:") +PWC.solution("a1b2c3d4") + +IO.puts("\nExample 3:") +PWC.solution("b2b") + +IO.puts("\nExample 4:") +PWC.solution("a16z") diff --git a/challenge-275/packy-anderson/perl/ch-1.pl b/challenge-275/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..a3cb69a88e --- /dev/null +++ b/challenge-275/packy-anderson/perl/ch-1.pl @@ -0,0 +1,26 @@ +#!/usr/bin/env perl +use v5.40; + +sub brokenKeys($sentence, @keys) { + my @words = split /\s+/, $sentence; + my $regex = '[' . join('', @keys) . ']'; + return ( scalar( grep {! /$regex/i } @words) ); +} + +sub solution($sentence, $keys) { + say 'Input: $sentence = "' . $sentence + . '", @keys = (\'' . join("', '", @$keys) . '\')'; + say 'Output: ' . brokenKeys($sentence, @$keys); +} + +say "Example 1:"; +solution("Perl Weekly Challenge", ['l', 'a']); + +say "\nExample 2:"; +solution("Perl and Raku", ['a']); + +say "\nExample 3:"; +solution("Well done Team PWC", ['l', 'o']); + +say "\nExample 4:"; +solution("The joys of polyglottism", ['T']); diff --git a/challenge-275/packy-anderson/perl/ch-2.pl b/challenge-275/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..47b4111bed --- /dev/null +++ b/challenge-275/packy-anderson/perl/ch-2.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl +use v5.40; + +sub replaceDigits($str) { + my @chars = split //, $str; + my $last_letter = $chars[0]; + my $out; + foreach my $c ( @chars ) { + if ($c =~ /[0-9]/) { + $out .= chr( ord($last_letter) + $c ); + } + else { + $out .= $c; + $last_letter = $c; + } + } + return $out; +} + +sub solution($str) { + say "Input: \$str = '$str'"; + say 'Output: \'' . replaceDigits($str) . '\''; +} + +say "Example 1:"; +solution("a1c1e1"); + +say "\nExample 2:"; +solution("a1b2c3d4"); + +say "\nExample 3:"; +solution("b2b"); + +say "\nExample 4:"; +solution("a16z");
\ No newline at end of file diff --git a/challenge-275/packy-anderson/python/ch-1.py b/challenge-275/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..d552f7192b --- /dev/null +++ b/challenge-275/packy-anderson/python/ch-1.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python + +import re + +def brokenKeys(sentence, keys): + words = sentence.split() + regex = re.compile('[' + ''.join(keys) + ']', re.I) + return( len([ + word for word in words if not re.search(regex, word) + ]) ) + +def solution(sentence, keys): + keysStr = "'" + "', '".join(keys) + "'" + print(f'Input: $sentence = "{sentence}", @keys = ({keysStr})') + print(f'Output: {brokenKeys(sentence, keys)}') + +print('Example 1:') +solution("Perl Weekly Challenge", ['l', 'a']) + +print('\nExample 2:') +solution("Perl and Raku", ['a']) + +print('\nExample 3:') +solution("Well done Team PWC", ['l', 'o']) + +print('\nExample 4:') +solution("The joys of polyglottism", ['T']) diff --git a/challenge-275/packy-anderson/python/ch-2.py b/challenge-275/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..0b5a0d57ce --- /dev/null +++ b/challenge-275/packy-anderson/python/ch-2.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python + +def replaceDigits(str): + last_letter = str[0:1] + out = '' + for c in str: + if c.isnumeric(): + out += chr( ord(last_letter) + int(c) ) + else: + out += c + last_letter = c + return out + +def solution(str): + print(f"Input: $str = '{str}'") + print(f"Output: '{replaceDigits(str)}'") + +print('Example 1:') +solution("a1c1e1") + +print('\nExample 2:') +solution("a1b2c3d4") + +print('\nExample 3:') +solution("b2b") + +print('\nExample 4:') +solution("a16z") diff --git a/challenge-275/packy-anderson/raku/ch-1.raku b/challenge-275/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..41d6e1fdbc --- /dev/null +++ b/challenge-275/packy-anderson/raku/ch-1.raku @@ -0,0 +1,26 @@ +#!/usr/bin/env raku +use v6; + +sub brokenKeys($sentence, @keys) { + my @words = $sentence.comb(/\S+/); + my $regex = '<[' ~ @keys.join ~ ']>'; + return ( ( @words.grep({! /:i <$regex>/ }) ).elems ) +} + +sub solution($sentence, @keys) { + say 'Input: $sentence = "' ~ $sentence + ~ '", @keys = (\'' ~ @keys.join("', '") ~ '\')'; + say 'Output: ' ~ brokenKeys($sentence, @keys); +} + +say "Example 1:"; +solution("Perl Weekly Challenge", ['l', 'a']); + +say "\nExample 2:"; +solution("Perl and Raku", ['a']); + +say "\nExample 3:"; +solution("Well done Team PWC", ['l', 'o']); + +say "\nExample 4:"; +solution("The joys of polyglottism", ['T']); diff --git a/challenge-275/packy-anderson/raku/ch-2.raku b/challenge-275/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..d5429c68d2 --- /dev/null +++ b/challenge-275/packy-anderson/raku/ch-2.raku @@ -0,0 +1,35 @@ +#!/usr/bin/env raku +use v6; + +sub replaceDigits($str) { + my @chars = $str.comb; + my $last_letter = @chars[0]; + my $out; + for @chars -> $c { + if ($c ~~ /<[0..9]>/) { + $out ~= ( $last_letter.ord + $c.Int ).chr; + } + else { + $out ~= $c; + $last_letter = $c; + } + } + return $out; +} + +sub solution($str) { + say "Input: \$str = '$str'"; + say 'Output: \'' ~ replaceDigits($str) ~ '\''; +} + +say "Example 1:"; +solution("a1c1e1"); + +say "\nExample 2:"; +solution("a1b2c3d4"); + +say "\nExample 3:"; +solution("b2b"); + +say "\nExample 4:"; +solution("a16z"); |
