From 78f2af30aefdcdb9276d0edbd0449f78c78275f4 Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Sat, 5 Jul 2025 00:03:35 -0400 Subject: 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 --- challenge-328/packy-anderson/README.md | 2 +- challenge-328/packy-anderson/blog.txt | 1 + challenge-328/packy-anderson/elixir/ch-1.exs | 45 ++++++++++++++++++ challenge-328/packy-anderson/elixir/ch-2.exs | 71 ++++++++++++++++++++++++++++ challenge-328/packy-anderson/perl/ch-1.pl | 46 ++++++++++++++++++ challenge-328/packy-anderson/perl/ch-2.pl | 31 ++++++++++++ challenge-328/packy-anderson/python/ch-1.py | 50 ++++++++++++++++++++ challenge-328/packy-anderson/python/ch-2.py | 55 +++++++++++++++++++++ challenge-328/packy-anderson/raku/ch-1.raku | 42 ++++++++++++++++ challenge-328/packy-anderson/raku/ch-2.raku | 32 +++++++++++++ 10 files changed, 374 insertions(+), 1 deletion(-) create mode 100644 challenge-328/packy-anderson/blog.txt create mode 100755 challenge-328/packy-anderson/elixir/ch-1.exs create mode 100755 challenge-328/packy-anderson/elixir/ch-2.exs create mode 100755 challenge-328/packy-anderson/perl/ch-1.pl create mode 100755 challenge-328/packy-anderson/perl/ch-2.pl create mode 100755 challenge-328/packy-anderson/python/ch-1.py create mode 100755 challenge-328/packy-anderson/python/ch-2.py create mode 100755 challenge-328/packy-anderson/raku/ch-1.raku create mode 100755 challenge-328/packy-anderson/raku/ch-2.raku 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/(?.)\?+(?.)/, + 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 =~ /(? .) \?+ (? .)/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 =~ /(? (\p{Ll}) (??{ uc($^N) }) | + (\p{Lu}) (??{ lc($^N) }) )/x; + + # replace the match with the empty string + my $replace = $+{match}; + # when I try to use $ 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 . ) \?+ (?P . )', + 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 ~~ /$ = . \?+ $ = ./; + + # 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 $ && $_ ne $ }) + .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 ~~ /$ = ( () {$c = $0.uc;} $c || + () {$c = $0.lc;} $c )/; + + # replace the match with the empty string + my $replace = $; + # when I try to use $ 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"); -- cgit From b07cf8c4ccd2cd032e99bc5c14891851fec4dda1 Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Sat, 5 Jul 2025 00:07:49 -0400 Subject: Fix Elixir case. --- challenge-328/packy-anderson/elixir/ch-1.exs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/challenge-328/packy-anderson/elixir/ch-1.exs b/challenge-328/packy-anderson/elixir/ch-1.exs index e396a61f9f..67d0bfd191 100755 --- a/challenge-328/packy-anderson/elixir/ch-1.exs +++ b/challenge-328/packy-anderson/elixir/ch-1.exs @@ -1,7 +1,7 @@ #!/usr/bin/env elixir defmodule PWC do - def replaceAllQuestion(str) do + def replace_all_question(str) do match = Regex.named_captures( ~r/(?.)\?+(?.)/, str @@ -13,7 +13,7 @@ defmodule PWC do 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) + replace_all_question(str) else # return the unmodified string if there are no ? characters str @@ -22,7 +22,7 @@ defmodule PWC do def solution(str) do IO.puts("Input: $str = \"#{str}\"") - IO.puts("Output: \"#{replaceAllQuestion(str)}\"") + IO.puts("Output: \"#{replace_all_question(str)}\"") end end -- cgit