aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPacky Anderson <packy@cpan.org>2025-07-13 12:48:32 -0400
committerPacky Anderson <packy@cpan.org>2025-07-13 12:48:32 -0400
commitb6e1922ba516e56ea41a5f698e39ec8b306a0465 (patch)
tree38672b404a723ba49c5826fdf048f9b0f07e7499
parent67f8df57472ca970259dcaa18a6b61f400df7ff2 (diff)
downloadperlweeklychallenge-club-b6e1922ba516e56ea41a5f698e39ec8b306a0465.tar.gz
perlweeklychallenge-club-b6e1922ba516e56ea41a5f698e39ec8b306a0465.tar.bz2
perlweeklychallenge-club-b6e1922ba516e56ea41a5f698e39ec8b306a0465.zip
Challenge 329 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-329/packy-anderson/README.md2
-rw-r--r--challenge-329/packy-anderson/blog.txt1
-rwxr-xr-xchallenge-329/packy-anderson/elixir/ch-1.exs42
-rwxr-xr-xchallenge-329/packy-anderson/elixir/ch-2.exs68
-rwxr-xr-xchallenge-329/packy-anderson/perl/ch-1.pl37
-rwxr-xr-xchallenge-329/packy-anderson/perl/ch-2.pl59
-rwxr-xr-xchallenge-329/packy-anderson/python/ch-1.py36
-rwxr-xr-xchallenge-329/packy-anderson/python/ch-2.py57
-rwxr-xr-xchallenge-329/packy-anderson/raku/ch-1.raku37
-rwxr-xr-xchallenge-329/packy-anderson/raku/ch-2.raku57
10 files changed, 395 insertions, 1 deletions
diff --git a/challenge-329/packy-anderson/README.md b/challenge-329/packy-anderson/README.md
index 84a2aa80e3..8c6ccb3256 100644
--- a/challenge-329/packy-anderson/README.md
+++ b/challenge-329/packy-anderson/README.md
@@ -23,4 +23,4 @@
## Blog Post
-[Perl Weekly Challenge: Good Old-Fashioned Replacement Boy?](https://packy.dardan.com/b/XE)
+[Perl Weekly Challenge: It MUST be nice!](https://packy.dardan.com/b/Xd)
diff --git a/challenge-329/packy-anderson/blog.txt b/challenge-329/packy-anderson/blog.txt
new file mode 100644
index 0000000000..ba79a7ff98
--- /dev/null
+++ b/challenge-329/packy-anderson/blog.txt
@@ -0,0 +1 @@
+https://packy.dardan.com/b/Xd \ No newline at end of file
diff --git a/challenge-329/packy-anderson/elixir/ch-1.exs b/challenge-329/packy-anderson/elixir/ch-1.exs
new file mode 100755
index 0000000000..73c14ca6cf
--- /dev/null
+++ b/challenge-329/packy-anderson/elixir/ch-1.exs
@@ -0,0 +1,42 @@
+#!/usr/bin/env elixir
+
+defmodule PWC do
+ defp find_distinct(int, {seen, output}) do
+ {int,
+ if not MapSet.member?(seen, int) do
+ # add int to seen and output
+ {MapSet.put(seen, int), output ++ [int]}
+ else
+ {seen, output}
+ end
+ }
+ end
+
+ def counter_integers(str) do
+ # replace lower case letters with space, then
+ # get rid of leading and trailing space, then
+ # split on whitespace, then
+ # build return array of distinct values
+ {_, {_, output}}
+ = Regex.replace(~r/\p{L}+/, str, " ")
+ |> String.trim
+ |> String.split
+ |> Enum.map_reduce({MapSet.new([]), []}, &find_distinct/2)
+
+ output
+ end
+
+ def solution(str) do
+ IO.puts("Input: $str = \"#{str}\"")
+ IO.puts("Output: " <> Enum.join(counter_integers(str), ", "))
+ end
+end
+
+IO.puts("Example 1:")
+PWC.solution("the1weekly2challenge2")
+
+IO.puts("\nExample 2:")
+PWC.solution("go21od1lu5c7k")
+
+IO.puts("\nExample 3:")
+PWC.solution("4p3e2r1l")
diff --git a/challenge-329/packy-anderson/elixir/ch-2.exs b/challenge-329/packy-anderson/elixir/ch-2.exs
new file mode 100755
index 0000000000..a63944fe22
--- /dev/null
+++ b/challenge-329/packy-anderson/elixir/ch-2.exs
@@ -0,0 +1,68 @@
+#!/usr/bin/env elixir
+
+defmodule PWC do
+ def swap_case(l) do
+ if String.downcase(l) == l,
+ do: String.upcase(l),
+ else: String.downcase(l)
+ end
+
+ defp find_longest(string, longest) do
+ longest = if String.length(string) > String.length(longest),
+ do: string, else: longest
+ {string, longest}
+ end
+
+ def nice_string(str) do
+ # convert the string to a Bag, er, MapSet
+ # so we know which characters are in it
+ seen = MapSet.new(String.codepoints(str))
+
+ # filter the letters and make a list
+ # of those that only appear in one case
+ not_both_cases
+ = String.codepoints(str)
+ |> Enum.filter(fn l ->
+ not MapSet.member?(seen, swap_case(l))
+ end)
+
+ # build a regex of the characters we're removing
+ {_, replace} = cond do
+ # no characters to replace, use a regex of chars
+ # we'll never see, like a SPACE
+ Enum.empty?(not_both_cases) -> Regex.compile(" ")
+ true -> ["["] ++ not_both_cases ++ ["]+"]
+ |> Enum.join
+ |> Regex.compile
+ end
+
+ # replace lower case letters with space, then
+ # get rid of leading and trailing space, then
+ # split on whitespace, then
+ # build return array of distinct values
+ {_, output}
+ = Regex.replace(replace, str, " ")
+ |> String.trim
+ |> String.split
+ |> Enum.map_reduce("", &find_longest/2)
+
+ output
+ end
+
+ def solution(str) do
+ IO.puts("Input: $str = \"#{str}\"")
+ IO.puts("Output: \"#{nice_string(str)}\"")
+ end
+end
+
+IO.puts("Example 1:")
+PWC.solution("YaaAho")
+
+IO.puts("\nExample 2:")
+PWC.solution("cC")
+
+IO.puts("\nExample 3:")
+PWC.solution("A")
+
+IO.puts("\nExample 4:")
+PWC.solution("AaBcDEFdefGhIiJjKlm")
diff --git a/challenge-329/packy-anderson/perl/ch-1.pl b/challenge-329/packy-anderson/perl/ch-1.pl
new file mode 100755
index 0000000000..ed4649e4d6
--- /dev/null
+++ b/challenge-329/packy-anderson/perl/ch-1.pl
@@ -0,0 +1,37 @@
+#!/usr/bin/env perl
+use v5.40;
+
+sub counterIntegers($str) {
+ # replace all lower case letters with space
+ $str =~ s/\p{Lowercase_Letter}+/ /g;
+
+ # get rid of leading and trailing whitespace
+ $str =~ s/^\s+|\s+$//g;
+
+ # split on whitespace
+ my @parts = split /\s+/, $str;
+
+ # build return array of distinct values
+ my @output;
+ my %seen;
+ foreach my $i ( @parts ) {
+ push @output, $i unless exists $seen{$i};
+ $seen{$i}++;
+ }
+
+ return @output;
+}
+
+sub solution($str) {
+ say qq{Input: \$str = "$str"};
+ say 'Output: ' . join(', ', counterIntegers($str));
+}
+
+say "Example 1:";
+solution("the1weekly2challenge2");
+
+say "\nExample 2:";
+solution("go21od1lu5c7k");
+
+say "\nExample 3:";
+solution("4p3e2r1l");
diff --git a/challenge-329/packy-anderson/perl/ch-2.pl b/challenge-329/packy-anderson/perl/ch-2.pl
new file mode 100755
index 0000000000..18dc7c2836
--- /dev/null
+++ b/challenge-329/packy-anderson/perl/ch-2.pl
@@ -0,0 +1,59 @@
+#!/usr/bin/env perl
+use v5.40;
+
+sub swapCase($l) {
+ return lc($l) eq $l ? uc($l) : lc($l);
+}
+
+sub niceString($str) {
+ # convert the string to a Bag so we
+ # know which characters are in it
+ my %seen = map { $_ => 1 } split //, $str;
+
+ # loop through the letters and make a list
+ # of those that only appear in one case
+ my @notBothCases;
+ foreach my $l ( keys %seen) {
+ next if exists $seen{ swapCase($l) };
+ push @notBothCases, $l;
+ }
+
+ if (@notBothCases) {
+ # build a regex of the characters we're removing
+ my $replace = '[' . join('', @notBothCases) . ']+';
+
+ # replace those characters with space
+ $str =~ s/$replace/ /g;
+ }
+
+ # get rid of leading and trailing whitespace
+ $str =~ s/^\s+|\s+$//g;
+
+ # split on whitespace
+ my @substrings = split /\s+/, $str;
+
+ # loop through the substrings we've found and find the longest
+ my $longest = '';
+ foreach my $s ( @substrings ) {
+ $longest = $s if length($s) > length($longest);
+ }
+
+ return $longest;
+}
+
+sub solution($str) {
+ say qq{Input: \$str = "$str"};
+ say 'Output: "' . niceString($str) . '"';
+}
+
+say "Example 1:";
+solution("YaaAho");
+
+say "\nExample 2:";
+solution("cC");
+
+say "\nExample 3:";
+solution("A");
+
+say "\nExample 4:";
+solution("AaBcDEFdefGhIiJjKlm");
diff --git a/challenge-329/packy-anderson/python/ch-1.py b/challenge-329/packy-anderson/python/ch-1.py
new file mode 100755
index 0000000000..3f4fc98784
--- /dev/null
+++ b/challenge-329/packy-anderson/python/ch-1.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env python
+
+import regex
+
+def counterIntegers(strVar):
+ # replace all lower case letters with space
+ strVar = regex.sub(r'\p{Lowercase_Letter}+', ' ', strVar)
+
+ # get rid of leading and trailing whitespace
+ strVar = strVar.strip()
+
+ # split on whitespace
+ parts = strVar.split()
+
+ # build return array of distinct values
+ output = []
+ seen = []
+ for i in parts:
+ if i not in seen:
+ output.append(i)
+ seen.append(i)
+
+ return output
+
+def solution(strVar):
+ print(f'Input: $str = "{strVar}"')
+ print(f'Output: {", ".join(counterIntegers(strVar))}')
+
+print('Example 1:')
+solution("the1weekly2challenge2")
+
+print('\nExample 2:')
+solution("go21od1lu5c7k")
+
+print('\nExample 3:')
+solution("4p3e2r1l")
diff --git a/challenge-329/packy-anderson/python/ch-2.py b/challenge-329/packy-anderson/python/ch-2.py
new file mode 100755
index 0000000000..33fca22458
--- /dev/null
+++ b/challenge-329/packy-anderson/python/ch-2.py
@@ -0,0 +1,57 @@
+#!/usr/bin/env python
+
+from collections import Counter
+import re
+
+def swapCase(l):
+ return l.upper() if l.lower() == l else l.lower()
+
+def niceString(strVar):
+ # convert the string to a Bag, er, Counter
+ # so we know which characters are in it
+ seen = Counter(list(strVar))
+
+ # loop through the letters and make a list
+ # of those that only appear in one case
+ notBothCases = []
+ for l in list(seen):
+ if swapCase(l) not in list(seen):
+ notBothCases.append(l)
+
+ if notBothCases:
+ # build a regex of the characters we're removing
+ replace = re.compile('[' + ''.join(notBothCases) + ']+')
+
+ # replace those characters with space
+ strVar = replace.sub(' ', strVar)
+
+ # get rid of leading and trailing whitespace
+ strVar = strVar.strip()
+
+ # split on whitespace
+ substrings = strVar.split()
+
+ # loop through the substrings we've found and find the longest
+ longest = ''
+ for s in substrings:
+ if len(s) > len(longest):
+ longest = s
+
+ return longest
+
+def solution(strVar):
+ print(f'Input: $str = "{strVar}"')
+ print(f'Output: "{niceString(strVar)}"')
+
+
+print('Example 1:')
+solution("YaaAho")
+
+print('\nExample 2:')
+solution("cC")
+
+print('\nExample 3:')
+solution("A")
+
+print('\nExample 4:')
+solution("AaBcDEFdefGhIiJjKlm") \ No newline at end of file
diff --git a/challenge-329/packy-anderson/raku/ch-1.raku b/challenge-329/packy-anderson/raku/ch-1.raku
new file mode 100755
index 0000000000..7e31aaf8eb
--- /dev/null
+++ b/challenge-329/packy-anderson/raku/ch-1.raku
@@ -0,0 +1,37 @@
+#!/usr/bin/env raku
+use v6;
+
+sub counterIntegers($str is copy) {
+ # replace all lower case letters with space
+ $str ~~ s:g/<lower>+/ /;
+
+ # get rid of leading and trailing whitespace
+ $str.=trim;
+
+ # split on whitespace
+ my @parts = $str.split(/\s+/);
+
+ # build return array of distinct values
+ my @output;
+ my $seen = BagHash.new;
+ for @parts -> $i {
+ @output.push($i) if $i ∉ $seen;
+ $seen{$i}++;
+ }
+
+ return @output;
+}
+
+sub solution($str) {
+ say qq{Input: \$str = "$str"};
+ say 'Output: ' ~ counterIntegers($str).join(', ');
+}
+
+say "Example 1:";
+solution("the1weekly2challenge2");
+
+say "\nExample 2:";
+solution("go21od1lu5c7k");
+
+say "\nExample 3:";
+solution("4p3e2r1l");
diff --git a/challenge-329/packy-anderson/raku/ch-2.raku b/challenge-329/packy-anderson/raku/ch-2.raku
new file mode 100755
index 0000000000..ce26ac54e1
--- /dev/null
+++ b/challenge-329/packy-anderson/raku/ch-2.raku
@@ -0,0 +1,57 @@
+#!/usr/bin/env raku
+use v6;
+
+sub swapCase($l) {
+ return $l.lc eq $l ?? $l.uc !! $l.lc;
+}
+
+sub niceString($str is copy) {
+ # convert the string to a Bag so we
+ # know which characters are in it
+ my $seen = Bag.new: $str.comb;
+
+ # loop through the letters and make a list
+ # of those that only appear in one case
+ my @notBothCases;
+ for $seen.keys -> $l {
+ next if swapCase($l) ∈ $seen;
+ @notBothCases.push($l);
+ }
+
+ # build a regex of the characters we're removing
+ my $replace = '<[' ~ @notBothCases.join(' ') ~ ']>+';
+
+ # replace those characters with space
+ $str ~~ s:g/<$replace>/ /;
+
+ # get rid of leading and trailing whitespace
+ $str.=trim;
+
+ # split on whitespace
+ my @substrings = $str.split(/\s+/);
+
+ # loop through the substrings we've found and find the longest
+ my $longest = '';
+ for @substrings -> $s {
+ $longest = $s if $s.chars > $longest.chars;
+ }
+
+ return $longest;
+}
+
+sub solution($str) {
+ say qq{Input: \$str = "$str"};
+ say 'Output: "' ~ niceString($str) ~ '"';
+}
+
+say "Example 1:";
+solution("YaaAho");
+
+say "\nExample 2:";
+solution("cC");
+
+say "\nExample 3:";
+solution("A");
+
+say "\nExample 4:";
+solution("AaBcDEFdefGhIiJjKlm");