aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPacky Anderson <packy@cpan.org>2025-07-05 00:03:35 -0400
committerPacky Anderson <packy@cpan.org>2025-07-05 00:03:35 -0400
commit78f2af30aefdcdb9276d0edbd0449f78c78275f4 (patch)
tree86d067c5a9b5f628e3502d423b840d295807ffa4
parent5ea606f19ac8c1dfa89419f67b0f0989fd0fad79 (diff)
downloadperlweeklychallenge-club-78f2af30aefdcdb9276d0edbd0449f78c78275f4.tar.gz
perlweeklychallenge-club-78f2af30aefdcdb9276d0edbd0449f78c78275f4.tar.bz2
perlweeklychallenge-club-78f2af30aefdcdb9276d0edbd0449f78c78275f4.zip
Challenge 328 solutions by Packy Anderson
* Raku that maybe looks like Raku, but mostly like Perl * Perl * Python that definitely looks like Perl * Elixir that looks kinda like Elixir 1 blog post
-rw-r--r--challenge-328/packy-anderson/README.md2
-rw-r--r--challenge-328/packy-anderson/blog.txt1
-rwxr-xr-xchallenge-328/packy-anderson/elixir/ch-1.exs45
-rwxr-xr-xchallenge-328/packy-anderson/elixir/ch-2.exs71
-rwxr-xr-xchallenge-328/packy-anderson/perl/ch-1.pl46
-rwxr-xr-xchallenge-328/packy-anderson/perl/ch-2.pl31
-rwxr-xr-xchallenge-328/packy-anderson/python/ch-1.py50
-rwxr-xr-xchallenge-328/packy-anderson/python/ch-2.py55
-rwxr-xr-xchallenge-328/packy-anderson/raku/ch-1.raku42
-rwxr-xr-xchallenge-328/packy-anderson/raku/ch-2.raku32
10 files changed, 374 insertions, 1 deletions
diff --git a/challenge-328/packy-anderson/README.md b/challenge-328/packy-anderson/README.md
index e2ebc8142c..84a2aa80e3 100644
--- a/challenge-328/packy-anderson/README.md
+++ b/challenge-328/packy-anderson/README.md
@@ -23,4 +23,4 @@
## Blog Post
-[Perl Weekly Challenge: Missing Absolute Distance](https://packy.dardan.com/b/Wy)
+[Perl Weekly Challenge: Good Old-Fashioned Replacement Boy?](https://packy.dardan.com/b/XE)
diff --git a/challenge-328/packy-anderson/blog.txt b/challenge-328/packy-anderson/blog.txt
new file mode 100644
index 0000000000..33ad91a6a7
--- /dev/null
+++ b/challenge-328/packy-anderson/blog.txt
@@ -0,0 +1 @@
+https://packy.dardan.com/b/XE \ No newline at end of file
diff --git a/challenge-328/packy-anderson/elixir/ch-1.exs b/challenge-328/packy-anderson/elixir/ch-1.exs
new file mode 100755
index 0000000000..e396a61f9f
--- /dev/null
+++ b/challenge-328/packy-anderson/elixir/ch-1.exs
@@ -0,0 +1,45 @@
+#!/usr/bin/env elixir
+
+defmodule PWC do
+ def replaceAllQuestion(str) do
+ match = Regex.named_captures(
+ ~r/(?<before>.)\?+(?<after>.)/,
+ str
+ )
+ if match do
+ # since we matched something, let's replace the first ?
+ # with a character that won't produce a consecutive
+ # repeating character
+ replace = ?a..?z |> Enum.take_random(1) |> List.to_string
+ str = Regex.replace(~r/\?/, str, replace, global: false)
+ # recursively call this function to replace any remaining ?s
+ replaceAllQuestion(str)
+ else
+ # return the unmodified string if there are no ? characters
+ str
+ end
+ end
+
+ def solution(str) do
+ IO.puts("Input: $str = \"#{str}\"")
+ IO.puts("Output: \"#{replaceAllQuestion(str)}\"")
+ end
+end
+
+IO.puts("Example 1:")
+PWC.solution("a?z")
+
+IO.puts("\nExample 2:")
+PWC.solution("pe?k")
+
+IO.puts("\nExample 3:")
+PWC.solution("gra?te")
+
+IO.puts("\nExample 4:")
+PWC.solution("so?c?r")
+
+IO.puts("\nExample 5:")
+PWC.solution("mi??i??i??i")
+
+IO.puts("\nExample 6:")
+PWC.solution("i??????????????????n")
diff --git a/challenge-328/packy-anderson/elixir/ch-2.exs b/challenge-328/packy-anderson/elixir/ch-2.exs
new file mode 100755
index 0000000000..8e15ce534e
--- /dev/null
+++ b/challenge-328/packy-anderson/elixir/ch-2.exs
@@ -0,0 +1,71 @@
+#!/usr/bin/env elixir
+
+defmodule PWC do
+ # sanity check: only check if it's long enough
+ def has_bad_match(str) when length(str) < 2, do: str
+
+ def has_bad_match(str) do
+ # match a sequence of two of the same character,
+ # but be case-insensitive
+ match = Regex.run(~r/(\p{L})\1/i, str, capture: :first)
+ if match do
+ # pull the match out of the list returned by Regex.run/3
+ match = match |> List.first
+ # in case we need to search later in the string, get
+ # the portion of the string AFTER the match
+ after_match = String.split(str, match, parts: 2)
+ |> List.last
+ # get the characters matched
+ char1 = match |> String.first
+ char2 = match |> String.last
+ if char1 != char2 do
+ # it's not the same character, same case
+ if char1 == String.downcase(char2) or
+ char1 == String.upcase(char2) do
+ # they're the same character with different case!
+ match
+ else
+ # they're different characters
+ has_bad_match(after_match)
+ end
+ else
+ # it IS the same character, same case
+ has_bad_match(after_match)
+ end
+ else
+ # we didn't match, so return false
+ nil
+ end
+ end
+
+ def good_string(str) do
+ # see if the string has a sequence of the same character twice,
+ # once upper case and once lower case, in any order
+ match = has_bad_match(str)
+
+ if match do
+ # we don't need a regular expression, since we have the
+ # character pair in match, we can use String.replace/4
+ #
+ # recursively call the function with the new string
+ good_string(String.replace(str, match, "", global: false))
+ else
+ # return the unmodified string if there are no matches
+ str
+ end
+ end
+
+ def solution(str) do
+ IO.puts("Input: $str = \"#{str}\"")
+ IO.puts("Output: \"#{good_string(str)}\"")
+ end
+end
+
+IO.puts("Example 1:")
+PWC.solution("WeEeekly")
+
+IO.puts("\nExample 2:")
+PWC.solution("abBAdD")
+
+IO.puts("\nExample 3:")
+PWC.solution("abc")
diff --git a/challenge-328/packy-anderson/perl/ch-1.pl b/challenge-328/packy-anderson/perl/ch-1.pl
new file mode 100755
index 0000000000..70a49afb17
--- /dev/null
+++ b/challenge-328/packy-anderson/perl/ch-1.pl
@@ -0,0 +1,46 @@
+#!/usr/bin/env perl
+use v5.40;
+
+use List::AllUtils qw( shuffle );
+
+sub replaceAllQuestion($str) {
+ # return the unmodified string if there are no ? characters
+ return $str unless $str =~ /(?<before> .) \?+ (?<after> .)/x;
+
+ # since we matched something, let's replace the first ? with a
+ # character that won't produce a consecutive repeating character
+ my $replace = (
+ shuffle ( # let's not always use the first character
+ grep {$_ ne $+{before} && $_ ne $+{after} } ('a' .. 'z')
+ )
+ )[0]; # but pull the first character off the shuffled list
+
+ # we're EXPLICITLY only replacing the first ?
+ $str =~ s/\?/$replace/;
+
+ # recursively call this function to replace any remaining ?s
+ return replaceAllQuestion($str);
+}
+
+sub solution($str) {
+ say "Input: \$str = \"$str\"";
+ say "Output: \"@{[replaceAllQuestion($str)]}\"";
+}
+
+say "Example 1:";
+solution("a?z");
+
+say "\nExample 2:";
+solution("pe?k");
+
+say "\nExample 3:";
+solution("gra?te");
+
+say "\nExample 4:";
+solution("so?c?r");
+
+say "\nExample 5:";
+solution("mi??i??i??i");
+
+say "\nExample 6:";
+solution("i??????????????????n"); \ No newline at end of file
diff --git a/challenge-328/packy-anderson/perl/ch-2.pl b/challenge-328/packy-anderson/perl/ch-2.pl
new file mode 100755
index 0000000000..92d0c3a765
--- /dev/null
+++ b/challenge-328/packy-anderson/perl/ch-2.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/env perl
+use v5.40;
+
+sub goodString($str) {
+ # return the unmodified string if there are no matches
+ return $str
+ unless $str =~ /(?<match> (\p{Ll}) (??{ uc($^N) }) |
+ (\p{Lu}) (??{ lc($^N) }) )/x;
+
+ # replace the match with the empty string
+ my $replace = $+{match};
+ # when I try to use $<match> in the replacement, it doesn't work
+ $str =~ s/$replace//;
+
+ # recursively call to eliminate additional pairs
+ goodString($str);
+}
+
+sub solution($str) {
+ say "Input: \$str = \"$str\"";
+ say "Output: \"@{[goodString($str)]}\"";
+}
+
+say "Example 1:";
+solution("WeEeekly");
+
+say "\nExample 2:";
+solution("abBAdD");
+
+say "\nExample 3:";
+solution("abc"); \ No newline at end of file
diff --git a/challenge-328/packy-anderson/python/ch-1.py b/challenge-328/packy-anderson/python/ch-1.py
new file mode 100755
index 0000000000..6585aaa583
--- /dev/null
+++ b/challenge-328/packy-anderson/python/ch-1.py
@@ -0,0 +1,50 @@
+#!/usr/bin/env python
+
+from random import choice
+import re
+from string import ascii_lowercase
+
+hasQuestion = re.compile(
+ r'(?P<before> . ) \?+ (?P<after> . )',
+ re.X # ignore whitespace
+)
+
+def replaceAllQuestion(str):
+ # return the unmodified string if there are no ? characters
+ m = re.search(hasQuestion, str)
+ if not m: return str
+
+ # since we matched something, let's replace the first ? with a
+ # character that won't produce a consecutive repeating character
+ replace = choice([ # let's not always use the first character
+ c for c in ascii_lowercase
+ if c != m.group('before') and c != m.group('after')
+ ])
+
+ # we're EXPLICITLY only replacing the first ?
+ str = re.sub(r'\?', replace, str, count=1)
+
+ # recursively call this function to replace any remaining ?s
+ return replaceAllQuestion(str)
+
+def solution(str):
+ print(f'Input: $str = "{str}"')
+ print(f'Output: "{replaceAllQuestion(str)}"')
+
+print('Example 1:')
+solution("a?z")
+
+print('\nExample 2:')
+solution("pe?k")
+
+print('\nExample 3:')
+solution("gra?te")
+
+print('\nExample 4:')
+solution("so?c?r")
+
+print('\nExample 5:')
+solution("mi??i??i??i")
+
+print('\nExample 6:')
+solution("i??????????????????n")
diff --git a/challenge-328/packy-anderson/python/ch-2.py b/challenge-328/packy-anderson/python/ch-2.py
new file mode 100755
index 0000000000..4e3ad5a102
--- /dev/null
+++ b/challenge-328/packy-anderson/python/ch-2.py
@@ -0,0 +1,55 @@
+#!/usr/bin/env python
+
+import regex
+
+def hasBadMatch(str):
+ # sanity check: only check if it's long enough
+ if len(str) < 2: return False
+
+ # match a sequence of two of the same character,
+ # but be case-insensitive
+ m = regex.search(r'(\p{L})\1', str, regex.I)
+
+ # we didn't match, so return false
+ if not m: return False
+
+ # get the characters matched
+ char1 = m.group(0)[0:1]
+ char2 = m.group(0)[1:]
+
+ if (char1 != char2 # it's not the same character, same case
+ and
+ # they're the same character with different case!
+ (char1 == char2.lower() or char1 == char2.upper())):
+ return m
+
+ # the match wasn't of differeing case, so search the
+ # rest of the string AFTER the match
+ hasBadMatch(str[m.end():])
+
+def goodString(str):
+ # see if the string has a sequence of the same character twice,
+ # once upper case and once lower case, in any order
+ m = hasBadMatch(str)
+
+ # return the unmodified string if there are no matches
+ if not m: return str
+
+ # we don't need a regular expression, since we have the
+ # character pair in m.group(0), we can use str.replace()
+ #
+ # recursively call the function with the new string
+ return goodString(str.replace(m.group(0), '', count=1))
+
+def solution(str):
+ print(f'Input: $str = "{str}"')
+ print(f'Output: "{goodString(str)}"')
+
+print('Example 1:')
+solution("WeEeekly")
+
+print('\nExample 2:')
+solution("abBAdD")
+
+print('\nExample 3:')
+solution("abc")
diff --git a/challenge-328/packy-anderson/raku/ch-1.raku b/challenge-328/packy-anderson/raku/ch-1.raku
new file mode 100755
index 0000000000..441acbc322
--- /dev/null
+++ b/challenge-328/packy-anderson/raku/ch-1.raku
@@ -0,0 +1,42 @@
+#!/usr/bin/env raku
+use v6;
+
+sub replaceAllQuestion($str is copy) {
+ # return the unmodified string if there are no ? characters
+ return $str unless $str ~~ /$<before> = . \?+ $<after> = ./;
+
+ # since we matched something, let's replace the first ? with a
+ # character that won't produce a consecutive repeating character
+ my $replace = ('a' .. 'z')
+ .grep({$_ ne $<before> && $_ ne $<after> })
+ .pick(); # let's not always use the first character
+
+ # we're EXPLICITLY only replacing the first ?
+ $str ~~ s/\?/$replace/;
+
+ # recursively call this function to replace any remaining ?s
+ return replaceAllQuestion($str);
+}
+
+sub solution($str) {
+ say "Input: \$str = \"$str\"";
+ say "Output: \"{replaceAllQuestion($str)}\"";
+}
+
+say "Example 1:";
+solution("a?z");
+
+say "\nExample 2:";
+solution("pe?k");
+
+say "\nExample 3:";
+solution("gra?te");
+
+say "\nExample 4:";
+solution("so?c?r");
+
+say "\nExample 5:";
+solution("mi??i??i??i");
+
+say "\nExample 6:";
+solution("i??????????????????n"); \ No newline at end of file
diff --git a/challenge-328/packy-anderson/raku/ch-2.raku b/challenge-328/packy-anderson/raku/ch-2.raku
new file mode 100755
index 0000000000..a2d833b43c
--- /dev/null
+++ b/challenge-328/packy-anderson/raku/ch-2.raku
@@ -0,0 +1,32 @@
+#!/usr/bin/env raku
+use v6;
+
+sub goodString($str is copy) {
+ my $c;
+ # return the unmodified string if there are no matches
+ return $str
+ unless $str ~~ /$<match> = ( (<lower>) {$c = $0.uc;} $c ||
+ (<upper>) {$c = $0.lc;} $c )/;
+
+ # replace the match with the empty string
+ my $replace = $<match>;
+ # when I try to use $<match> in the replacement, it doesn't work
+ $str ~~ s/$replace//;
+
+ # recursively call to eliminate additional pairs
+ goodString($str);
+}
+
+sub solution($str) {
+ say "Input: \$str = \"$str\"";
+ say "Output: \"{goodString($str)}\"";
+}
+
+say "Example 1:";
+solution("WeEeekly");
+
+say "\nExample 2:";
+solution("abBAdD");
+
+say "\nExample 3:";
+solution("abc");