aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-341/packy-anderson/README.md2
-rw-r--r--challenge-341/packy-anderson/blog.txt1
-rwxr-xr-xchallenge-341/packy-anderson/elixir/ch-1.exs38
-rwxr-xr-xchallenge-341/packy-anderson/elixir/ch-2.exs22
-rwxr-xr-xchallenge-341/packy-anderson/perl/ch-1.pl28
-rwxr-xr-xchallenge-341/packy-anderson/perl/ch-2.pl22
-rwxr-xr-xchallenge-341/packy-anderson/python/ch-1.py38
-rwxr-xr-xchallenge-341/packy-anderson/python/ch-2.py26
-rwxr-xr-xchallenge-341/packy-anderson/raku/ch-1.raku28
-rwxr-xr-xchallenge-341/packy-anderson/raku/ch-2.raku22
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");