aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-04-25 08:59:15 +0100
committerGitHub <noreply@github.com>2025-04-25 08:59:15 +0100
commit74a02191703ca480ccd60755665e25a541c03bd8 (patch)
tree3e1bf40492b8840ec5758ae86fb20dd5770c4c41
parentf5a7c7c1d7d63133484f13e59749fd57794d81e7 (diff)
parent07792952c5ec12552fd809d9b796fc8bdb72cfc6 (diff)
downloadperlweeklychallenge-club-74a02191703ca480ccd60755665e25a541c03bd8.tar.gz
perlweeklychallenge-club-74a02191703ca480ccd60755665e25a541c03bd8.tar.bz2
perlweeklychallenge-club-74a02191703ca480ccd60755665e25a541c03bd8.zip
Merge pull request #11929 from packy/master
Challenge 318 solutions by Packy Anderson
-rw-r--r--challenge-318/packy-anderson/README.md26
-rw-r--r--challenge-318/packy-anderson/blog.txt1
-rwxr-xr-xchallenge-318/packy-anderson/elixir/ch-1.exs32
-rwxr-xr-xchallenge-318/packy-anderson/elixir/ch-2.exs61
-rwxr-xr-xchallenge-318/packy-anderson/perl/ch-1.pl30
-rwxr-xr-xchallenge-318/packy-anderson/perl/ch-2.pl47
-rwxr-xr-xchallenge-318/packy-anderson/python/ch-1.py38
-rwxr-xr-xchallenge-318/packy-anderson/python/ch-2.py45
-rwxr-xr-xchallenge-318/packy-anderson/raku/ch-1.raku30
-rwxr-xr-xchallenge-318/packy-anderson/raku/ch-2.raku47
10 files changed, 356 insertions, 1 deletions
diff --git a/challenge-318/packy-anderson/README.md b/challenge-318/packy-anderson/README.md
index dc50d472ee..c2f55fde69 100644
--- a/challenge-318/packy-anderson/README.md
+++ b/challenge-318/packy-anderson/README.md
@@ -1,2 +1,26 @@
-# Solutions by Packy Anderson.
+# Solutions by Packy Anderson
+## Raku
+
+* [Task 1](raku/ch-1.raku)
+* [Task 2](raku/ch-2.raku)
+
+## Perl
+
+* [Task 1](perl/ch-1.pl)
+* [Task 2](perl/ch-2.pl)
+
+## Guest Language: Python
+
+* [Task 1](python/ch-1.py)
+* [Task 2](python/ch-2.py)
+
+## Guest Language: Elixir
+
+* [Task 1](elixir/ch-1.exs)
+* [Task 2](elixir/ch-2.exs)
+
+
+## Blog Post
+
+[Perl Weekly Challenge: Group the Reverse Position](https://packy.dardan.com/b/Tp)
diff --git a/challenge-318/packy-anderson/blog.txt b/challenge-318/packy-anderson/blog.txt
new file mode 100644
index 0000000000..7a24911420
--- /dev/null
+++ b/challenge-318/packy-anderson/blog.txt
@@ -0,0 +1 @@
+https://packy.dardan.com/b/Tp \ No newline at end of file
diff --git a/challenge-318/packy-anderson/elixir/ch-1.exs b/challenge-318/packy-anderson/elixir/ch-1.exs
new file mode 100755
index 0000000000..10891b3ec1
--- /dev/null
+++ b/challenge-318/packy-anderson/elixir/ch-1.exs
@@ -0,0 +1,32 @@
+#!/usr/bin/env elixir
+
+defmodule PWC do
+ def groupPosition(str) do
+ # will return a list of [entire_match, matching_char]
+ # tuples for each group matched
+ groups = Regex.scan(~r/([a-z])\1\1+/i, str)
+ if Enum.empty?(groups) do
+ "\"\""
+ else
+ groups
+ # grab the entire match from each group tuple
+ # and wrap it in quotes
+ |> Enum.map(&( "\"#{ List.first(&1) }\"" ))
+ |> Enum.join(", ")
+ end
+ end
+
+ def solution(str) do
+ IO.puts("Input: $str = \"#{str}\"")
+ IO.puts("Output: " <> groupPosition(str) )
+ end
+end
+
+IO.puts("Example 1:")
+PWC.solution("abccccd")
+
+IO.puts("\nExample 2:")
+PWC.solution("aaabcddddeefff")
+
+IO.puts("\nExample 3:")
+PWC.solution("abcdd")
diff --git a/challenge-318/packy-anderson/elixir/ch-2.exs b/challenge-318/packy-anderson/elixir/ch-2.exs
new file mode 100755
index 0000000000..57a553e874
--- /dev/null
+++ b/challenge-318/packy-anderson/elixir/ch-2.exs
@@ -0,0 +1,61 @@
+#!/usr/bin/env elixir
+
+defmodule PWC do
+ def reverseEquals(source, target, i, j) do
+ reversed = [] ++ if i > 0 do
+ Enum.slice(source, 0 .. i-1)
+ else
+ []
+ end
+ reversed = reversed ++ Enum.reverse(Enum.slice(source, i..j))
+ reversed = reversed ++ if j < length(source)-1 do
+ Enum.slice(source, j+1 .. length(source)-1)
+ else
+ []
+ end
+ cond do
+ reversed == target ->
+ { true, "#{i}-#{j}" }
+ # we've reached the end of the nested loops,
+ # so there's no solution
+ i == length(source)-2 and j == length(source)-1 ->
+ { false, "" }
+ # we're at the last j value, so let's increment i
+ # and set j = i+1
+ j == length(source)-1 ->
+ reverseEquals(source, target, i+1, i+2)
+ # just increment j
+ true ->
+ reverseEquals(source, target, i, j+1)
+ end
+ end
+
+ def reverseEquals(source, target)
+ when length(source) != length(target), do: { false, "" }
+
+ def reverseEquals(source, target)
+ when source == target, do: { true, "" }
+
+ def reverseEquals(source, target) do
+ reverseEquals(source, target, 0, 1)
+ end
+
+ def solution(source, target) do
+ IO.puts("Input: @source = (" <> Enum.join(source, ", ") <> ")")
+ IO.puts(" @target = (" <> Enum.join(target, ", ") <> ")")
+ {equals, elements} = reverseEquals(source, target)
+ IO.puts("Output: #{equals}")
+ if elements do
+ IO.puts("\nReverse elements: #{elements}")
+ end
+ end
+end
+
+IO.puts("Example 1:")
+PWC.solution([3, 2, 1, 4], [1, 2, 3, 4])
+
+IO.puts("\nExample 2:")
+PWC.solution([1, 3, 4], [4, 1, 3])
+
+IO.puts("\nExample 3:")
+PWC.solution([2], [2])
diff --git a/challenge-318/packy-anderson/perl/ch-1.pl b/challenge-318/packy-anderson/perl/ch-1.pl
new file mode 100755
index 0000000000..a1ccbee4d5
--- /dev/null
+++ b/challenge-318/packy-anderson/perl/ch-1.pl
@@ -0,0 +1,30 @@
+#!/usr/bin/env perl
+use v5.40;
+
+sub groupPosition($str) {
+ my @groups;
+ while ( $str =~ /([a-z])\1\1+/gip ) {
+ push @groups, ${^MATCH};
+ }
+ if (@groups) {
+ return join(", ", map { qq/"$_"/ } @groups);
+ }
+ else {
+ return qq/""/;
+ }
+}
+
+sub solution($str) {
+ say qq/Input: \$str = "$str"/;
+ my $result = groupPosition($str);
+ say qq/Output: $result/;
+}
+
+say "Example 1:";
+solution("abccccd");
+
+say "\nExample 2:";
+solution("aaabcddddeefff");
+
+say "\nExample 3:";
+solution("abcdd");
diff --git a/challenge-318/packy-anderson/perl/ch-2.pl b/challenge-318/packy-anderson/perl/ch-2.pl
new file mode 100755
index 0000000000..fed9c5fc3b
--- /dev/null
+++ b/challenge-318/packy-anderson/perl/ch-2.pl
@@ -0,0 +1,47 @@
+#!/usr/bin/env perl
+use v5.40;
+
+sub arrayEquals($a, $b) {
+ return join(',', @$a) eq join(',', @$b);
+}
+
+sub reverseEquals($source, $target) {
+ # deal with the trivial cases
+ if (@$source != @$target) {
+ # @source and @target are different lengths
+ return ("False", "");
+ }
+ if (arrayEquals($source, $target)) {
+ # they're already the same
+ return ("True", "");
+ }
+ # ok, now we start checking subarrays
+ foreach my $i ( 0 .. $#{$source} - 1 ) {
+ foreach my $j ( $i+1 .. $#{$source} ) {
+ my @reversed;
+ @reversed = @$source[0..$i-1] if $i > 0;
+ push @reversed, reverse @$source[$i..$j];
+ push @reversed, @$source[$j+1..$#{$source}]
+ if $j < $#{$source};
+ return ("True", "$i-$j") if arrayEquals(\@reversed, $target);
+ }
+ }
+ return ("False", "");
+}
+
+sub solution($source, $target) {
+ say 'Input: @source = (' . join(', ', @$source) . ')';
+ say ' @target = (' . join(', ', @$target) . ')';
+ my ($equals, $elements) = reverseEquals($source, $target);
+ say 'Output: ' . $equals;
+ say "\nReverse elements: " . $elements if $elements;
+}
+
+say "Example 1:";
+solution([3, 2, 1, 4], [1, 2, 3, 4]);
+
+say "\nExample 2:";
+solution([1, 3, 4], [4, 1, 3]);
+
+say "\nExample 3:";
+solution([2], [2]);
diff --git a/challenge-318/packy-anderson/python/ch-1.py b/challenge-318/packy-anderson/python/ch-1.py
new file mode 100755
index 0000000000..966e662963
--- /dev/null
+++ b/challenge-318/packy-anderson/python/ch-1.py
@@ -0,0 +1,38 @@
+#!/usr/bin/env python
+
+import re
+
+def groupPosition(str):
+ # python doesn't support nested groups,
+ # so let's match just the first occurence
+ hasGroups = re.compile(r"([a-z])\1\1+", re.I)
+ groups = []
+
+ match = hasGroups.search(str)
+ while match:
+ # push the occurrence onto a list of groups
+ # the 0th group is the entire match
+ groups.append(match.group(0))
+ # then remove from the start of the string to
+ # the end of the first occurence
+ str = str[match.end():]
+ # and match against the new string
+ match = hasGroups.search(str)
+
+ if groups:
+ return ', '.join([ f'"{s}"' for s in groups ])
+ else:
+ return '""'
+
+def solution(str):
+ print(f'Input: $str = "{str}"')
+ print(f'Output: { groupPosition(str) }')
+
+print('Example 1:')
+solution("abccccd")
+
+print('\nExample 2:')
+solution("aaabcddddeefff")
+
+print('\nExample 3:')
+solution("abcdd")
diff --git a/challenge-318/packy-anderson/python/ch-2.py b/challenge-318/packy-anderson/python/ch-2.py
new file mode 100755
index 0000000000..2c129b2aec
--- /dev/null
+++ b/challenge-318/packy-anderson/python/ch-2.py
@@ -0,0 +1,45 @@
+#!/usr/bin/env python
+
+def reverseEquals(source, target):
+ # deal with the trivial cases
+ if len(source) != len(target):
+ # @source and @target are different lengths
+ return (False, "")
+
+ if source == target:
+ # they're already the same
+ return (True, "")
+
+ # ok, now we start checking subarrays
+ for i in range(len(source)-1):
+ for j in range(i+1, len(source)):
+ rev = []
+ if i > 0:
+ rev = source[0:i]
+ rev.extend(list(reversed(source[i:j+1])))
+ if j < len(source)-1:
+ rev.extend(source[j+1:])
+ if rev == target:
+ return (True, f'{i}-{j}')
+
+ return (False, "")
+
+def comma_join(arr):
+ return ', '.join(map(lambda i: str(i), arr))
+
+def solution(source, target):
+ print(f'Input: @source = ({comma_join(source)})')
+ print(f' @target = ({comma_join(target)})')
+ (equals, elements) = reverseEquals(source, target)
+ print(f'Output: {equals}')
+ if elements:
+ print(f'\nReverse elements: {elements}')
+
+print('Example 1:')
+solution([3, 2, 1, 4], [1, 2, 3, 4])
+
+print('\nExample 2:')
+solution([1, 3, 4], [4, 1, 3])
+
+print('\nExample 3:')
+solution([2], [2])
diff --git a/challenge-318/packy-anderson/raku/ch-1.raku b/challenge-318/packy-anderson/raku/ch-1.raku
new file mode 100755
index 0000000000..e524cd6c63
--- /dev/null
+++ b/challenge-318/packy-anderson/raku/ch-1.raku
@@ -0,0 +1,30 @@
+#!/usr/bin/env raku
+use v6;
+
+sub groupPosition($str) {
+ my @groups;
+ while ( $str ~~ m:i:c/ ((<[a..z]>)$0$0+) / ) {
+ @groups.push($0);
+ }
+ if (@groups) {
+ return @groups.map({ qq/"$_"/ }).join(", ");
+ }
+ else {
+ return qq/""/;
+ }
+}
+
+sub solution($str) {
+ say qq/Input: \$str = "$str"/;
+ my $result = groupPosition($str);
+ say qq/Output: $result/;
+}
+
+say "Example 1:";
+solution("abccccd");
+
+say "\nExample 2:";
+solution("aaabcddddeefff");
+
+say "\nExample 3:";
+solution("abcdd");
diff --git a/challenge-318/packy-anderson/raku/ch-2.raku b/challenge-318/packy-anderson/raku/ch-2.raku
new file mode 100755
index 0000000000..9948416dbd
--- /dev/null
+++ b/challenge-318/packy-anderson/raku/ch-2.raku
@@ -0,0 +1,47 @@
+#!/usr/bin/env raku
+use v6;
+
+sub arrayEquals(@a, @b) {
+ return @a.join(',') eq @b.join(',');
+}
+
+sub reverseEquals(@source, @target) {
+ # deal with the trivial cases
+ if (@source.elems != @target.elems) {
+ # @source and @target are different lengths
+ return (False, "");
+ }
+ if (arrayEquals(@source, @target)) {
+ # they're already the same
+ return (True, "");
+ }
+ # ok, now we start checking subarrays
+ for 0..@source.end - 1 -> $i {
+ for $i+1..@source.end -> $j {
+ my @reversed;
+ @reversed = @source[0..$i-1] if $i > 0;
+ @reversed.append(@source[$i..$j].reverse);
+ @reversed.append(@source[$j+1..@source.end])
+ if $j < @source.end;
+ return (True, "$i-$j") if arrayEquals(@reversed, @target);
+ }
+ }
+ return (False, "");
+}
+
+sub solution(@source, @target) {
+ say 'Input: @source = (' ~ @source.join(', ') ~ ')';
+ say ' @target = (' ~ @target.join(', ') ~ ')';
+ my ($equals, $elements) = reverseEquals(@source, @target);
+ say 'Output: ' ~ $equals;
+ say "\nReverse elements: " ~ $elements if $elements;
+}
+
+say "Example 1:";
+solution([3, 2, 1, 4], [1, 2, 3, 4]);
+
+say "\nExample 2:";
+solution([1, 3, 4], [4, 1, 3]);
+
+say "\nExample 3:";
+solution([2], [2]);