aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-345/packy-anderson/README.md2
-rw-r--r--challenge-345/packy-anderson/blog.txt1
-rwxr-xr-xchallenge-345/packy-anderson/elixir/ch-1.exs42
-rwxr-xr-xchallenge-345/packy-anderson/elixir/ch-2.exs44
-rwxr-xr-xchallenge-345/packy-anderson/perl/ch-1.pl32
-rwxr-xr-xchallenge-345/packy-anderson/perl/ch-2.pl42
-rwxr-xr-xchallenge-345/packy-anderson/python/ch-1.py30
-rwxr-xr-xchallenge-345/packy-anderson/python/ch-2.py35
-rwxr-xr-xchallenge-345/packy-anderson/raku/ch-1.raku32
-rwxr-xr-xchallenge-345/packy-anderson/raku/ch-2.raku42
10 files changed, 301 insertions, 1 deletions
diff --git a/challenge-345/packy-anderson/README.md b/challenge-345/packy-anderson/README.md
index dfffb8191c..b26414818b 100644
--- a/challenge-345/packy-anderson/README.md
+++ b/challenge-345/packy-anderson/README.md
@@ -23,4 +23,4 @@
## Blog Post
-[Perl Weekly Challenge: A-ray Sunshine!](https://packy.dardan.com/b/ct)
+[Perl Weekly Challenge: Teach Them How To Say PWC...](https://packy.dardan.com/b/dD)
diff --git a/challenge-345/packy-anderson/blog.txt b/challenge-345/packy-anderson/blog.txt
new file mode 100644
index 0000000000..2cfc877acc
--- /dev/null
+++ b/challenge-345/packy-anderson/blog.txt
@@ -0,0 +1 @@
+https://packy.dardan.com/b/dD \ No newline at end of file
diff --git a/challenge-345/packy-anderson/elixir/ch-1.exs b/challenge-345/packy-anderson/elixir/ch-1.exs
new file mode 100755
index 0000000000..3668a2aec2
--- /dev/null
+++ b/challenge-345/packy-anderson/elixir/ch-1.exs
@@ -0,0 +1,42 @@
+#!/usr/bin/env elixir
+
+defmodule PWC do
+ def reduce_fn(i, {peaks, ints}) do
+ cond do
+ i != 0 && Enum.at(ints,i-1) > Enum.at(ints,i) ->
+ {i, {peaks, ints}}
+ i != length(ints) && Enum.at(ints,i+1) > Enum.at(ints,i) ->
+ {i, {peaks, ints}}
+ true ->
+ {i, {peaks ++ [i], ints}}
+ end
+ end
+
+ def peak_positions(ints) do
+ {_, {peaks, _}} = 0..length(ints)-1
+ |> Enum.to_list
+ |> Enum.map_reduce({[], ints}, &reduce_fn/2)
+ peaks
+ end
+
+ def solution(ints) do
+ IO.puts("Input: @ints = (" <> Enum.join(ints, ", ") <> ")")
+ peaks = peak_positions(ints)
+ IO.puts("Output: (" <> Enum.join(peaks, ", ") <> ")")
+ end
+end
+
+IO.puts("Example 1:")
+PWC.solution([1, 3, 2])
+
+IO.puts("\nExample 2:")
+PWC.solution([2, 4, 6, 5, 3])
+
+IO.puts("\nExample 3:")
+PWC.solution([1, 2, 3, 2, 4, 1])
+
+IO.puts("\nExample 4:")
+PWC.solution([5, 3, 1])
+
+IO.puts("\nExample 5:")
+PWC.solution([1, 5, 1, 5, 1, 5, 1])
diff --git a/challenge-345/packy-anderson/elixir/ch-2.exs b/challenge-345/packy-anderson/elixir/ch-2.exs
new file mode 100755
index 0000000000..6abe9fcfd3
--- /dev/null
+++ b/challenge-345/packy-anderson/elixir/ch-2.exs
@@ -0,0 +1,44 @@
+#!/usr/bin/env elixir
+
+defmodule PWC do
+ def last_visitor([], _, ans, _), do: ans
+
+ def last_visitor([i | ints], seen, ans, x) do
+ {seen, ans, x} = if i > 0 do
+ {[i] ++ seen, ans, x} # insert at front of @seen
+ else
+ if x < length(seen) do # if $x < len(@seen)
+ # append seen[x] to @ans
+ {seen, ans ++ [Enum.at(seen, x)], x+1}
+ else
+ {seen, ans ++ [-1], x+1} # append -1 to @ans
+ end
+ end
+ last_visitor(ints, seen, ans, x)
+ end
+
+ def last_visitor(ints) do
+ last_visitor(ints, [], [], 0)
+ end
+
+ def solution(ints) do
+ IO.puts("Input: @ints = (" <> Enum.join(ints, ", ") <> ")")
+ out = last_visitor(ints)
+ IO.puts("Output: (" <> Enum.join(out, ", ") <> ")")
+ end
+end
+
+IO.puts("Example 1:")
+PWC.solution([5, -1, -1])
+
+IO.puts("\nExample 2:")
+PWC.solution([3, 7, -1, -1, -1])
+
+IO.puts("\nExample 3:")
+PWC.solution([2, -1, 4, -1, -1])
+
+IO.puts("\nExample 4:")
+PWC.solution([10, 20, -1, 30, -1, -1])
+
+IO.puts("\nExample 5:")
+PWC.solution([-1, -1, 5, -1])
diff --git a/challenge-345/packy-anderson/perl/ch-1.pl b/challenge-345/packy-anderson/perl/ch-1.pl
new file mode 100755
index 0000000000..764a8d3501
--- /dev/null
+++ b/challenge-345/packy-anderson/perl/ch-1.pl
@@ -0,0 +1,32 @@
+#!/usr/bin/env perl
+use v5.40;
+
+sub peakPositions(@ints) {
+ my @peaks;
+ foreach my $i (0 .. $#ints) {
+ next unless $i == 0 || $ints[$i - 1] <= $ints[$i];
+ next unless $i == $#ints || $ints[$i + 1] <= $ints[$i];
+ push @peaks, $i;
+ }
+ return @peaks;
+}
+
+sub solution($ints) {
+ say 'Input: @ints = (' . join(', ', @$ints) . ')';
+ say 'Output: (' . join(', ', peakPositions(@$ints)) . ')';
+}
+
+say "Example 1:";
+solution([1, 3, 2]);
+
+say "\nExample 2:";
+solution([2, 4, 6, 5, 3]);
+
+say "\nExample 3:";
+solution([1, 2, 3, 2, 4, 1]);
+
+say "\nExample 4:";
+solution([5, 3, 1]);
+
+say "\nExample 5:";
+solution([1, 5, 1, 5, 1, 5, 1]);
diff --git a/challenge-345/packy-anderson/perl/ch-2.pl b/challenge-345/packy-anderson/perl/ch-2.pl
new file mode 100755
index 0000000000..3c787c8e4a
--- /dev/null
+++ b/challenge-345/packy-anderson/perl/ch-2.pl
@@ -0,0 +1,42 @@
+#!/usr/bin/env perl
+use v5.40;
+
+sub lastVisitor(@ints) {
+ my (@seen, @ans);
+ my $x = 0;
+ foreach my $i ( @ints ) {
+ if ($i > 0) {
+ unshift @seen, $i; # insert at front of @seen
+ }
+ else {
+ if ($x < @seen) { # if $x < len(@seen)
+ push @ans, $seen[$x]; # append seen[x] to @ans
+ }
+ else {
+ push @ans, -1; # append -1 to @ans
+ }
+ $x++;
+ }
+ }
+ return @ans;
+}
+
+sub solution($ints) {
+ say 'Input: @ints = (' . join(', ', @$ints) . ')';
+ say 'Output: (' . join(', ', lastVisitor(@$ints)) . ')';
+}
+
+say "Example 1:";
+solution([5, -1, -1]);
+
+say "\nExample 2:";
+solution([3, 7, -1, -1, -1]);
+
+say "\nExample 3:";
+solution([2, -1, 4, -1, -1]);
+
+say "\nExample 4:";
+solution([10, 20, -1, 30, -1, -1]);
+
+say "\nExample 5:";
+solution([-1, -1, 5, -1]);
diff --git a/challenge-345/packy-anderson/python/ch-1.py b/challenge-345/packy-anderson/python/ch-1.py
new file mode 100755
index 0000000000..aadf0aba56
--- /dev/null
+++ b/challenge-345/packy-anderson/python/ch-1.py
@@ -0,0 +1,30 @@
+#!/usr/bin/env python
+
+def peak_positions(ints):
+ peaks = []
+ for i in range(len(ints)):
+ if not (i == 0 or ints[i - 1] <= ints[i]):
+ continue
+ if not (i == len(ints)-1 or ints[i + 1] <= ints[i]):
+ continue
+ peaks.append(i)
+ return peaks
+
+def solution(ints):
+ print(f'Input: @ints = ({", ".join(map(str, ints))})')
+ print(f'Output: ({", ".join(map(str, peak_positions(ints)))})')
+
+print('Example 1:')
+solution([1, 3, 2])
+
+print('\nExample 2:')
+solution([2, 4, 6, 5, 3])
+
+print('\nExample 3:')
+solution([1, 2, 3, 2, 4, 1])
+
+print('\nExample 4:')
+solution([5, 3, 1])
+
+print('\nExample 5:')
+solution([1, 5, 1, 5, 1, 5, 1])
diff --git a/challenge-345/packy-anderson/python/ch-2.py b/challenge-345/packy-anderson/python/ch-2.py
new file mode 100755
index 0000000000..a9ac243d17
--- /dev/null
+++ b/challenge-345/packy-anderson/python/ch-2.py
@@ -0,0 +1,35 @@
+#!/usr/bin/env python
+
+def last_visitor(ints):
+ seen = []
+ ans = []
+ x = 0
+ for i in ints:
+ if i > 0:
+ seen.insert(0, i) # insert at front of @seen
+ else:
+ if x < len(seen): # if $x < len(@seen)
+ ans.append( seen[x] ) # append seen[x] to @ans
+ else:
+ ans.append( -1 ) # append -1 to @ans
+ x += 1
+ return ans
+
+def solution(ints):
+ print(f'Input: @ints = ({", ".join(map(str, ints))})')
+ print(f'Output: ({", ".join(map(str, last_visitor(ints)))})')
+
+print('Example 1:')
+solution([5, -1, -1])
+
+print('\nExample 2:')
+solution([3, 7, -1, -1, -1])
+
+print('\nExample 3:')
+solution([2, -1, 4, -1, -1])
+
+print('\nExample 4:')
+solution([10, 20, -1, 30, -1, -1])
+
+print('\nExample 5:')
+solution([-1, -1, 5, -1])
diff --git a/challenge-345/packy-anderson/raku/ch-1.raku b/challenge-345/packy-anderson/raku/ch-1.raku
new file mode 100755
index 0000000000..1acc238c87
--- /dev/null
+++ b/challenge-345/packy-anderson/raku/ch-1.raku
@@ -0,0 +1,32 @@
+#!/usr/bin/env raku
+use v6;
+
+sub peakPositions(@ints) {
+ my @peaks;
+ for 0..@ints.end -> $i {
+ next unless $i == 0 || @ints[$i - 1] <= @ints[$i];
+ next unless $i == @ints.end || @ints[$i + 1] <= @ints[$i];
+ @peaks.push($i);
+ }
+ return @peaks;
+}
+
+sub solution(@ints) {
+ say 'Input: @ints = (' ~ @ints.join(', ') ~ ')';
+ say 'Output: (' ~ peakPositions(@ints).join(', ') ~ ')';
+}
+
+say "Example 1:";
+solution([1, 3, 2]);
+
+say "\nExample 2:";
+solution([2, 4, 6, 5, 3]);
+
+say "\nExample 3:";
+solution([1, 2, 3, 2, 4, 1]);
+
+say "\nExample 4:";
+solution([5, 3, 1]);
+
+say "\nExample 5:";
+solution([1, 5, 1, 5, 1, 5, 1]);
diff --git a/challenge-345/packy-anderson/raku/ch-2.raku b/challenge-345/packy-anderson/raku/ch-2.raku
new file mode 100755
index 0000000000..b48918b18c
--- /dev/null
+++ b/challenge-345/packy-anderson/raku/ch-2.raku
@@ -0,0 +1,42 @@
+#!/usr/bin/env raku
+use v6;
+
+sub lastVisitor(@ints) {
+ my (@seen, @ans);
+ my $x = 0;
+ for @ints -> $i {
+ if ($i > 0) {
+ @seen.unshift($i); # insert at front of @seen
+ }
+ else {
+ if ($x < @seen.elems) { # if $x < len(@seen)
+ @ans.push( @seen[$x] ); # append seen[x] to @ans
+ }
+ else {
+ @ans.push( -1 ); # append -1 to @ans
+ }
+ $x++;
+ }
+ }
+ return @ans;
+}
+
+sub solution(@ints) {
+ say 'Input: @ints = (' ~ @ints.join(', ') ~ ')';
+ say 'Output: (' ~ lastVisitor(@ints).join(', ') ~ ')';
+}
+
+say "Example 1:";
+solution([5, -1, -1]);
+
+say "\nExample 2:";
+solution([3, 7, -1, -1, -1]);
+
+say "\nExample 3:";
+solution([2, -1, 4, -1, -1]);
+
+say "\nExample 4:";
+solution([10, 20, -1, 30, -1, -1]);
+
+say "\nExample 5:";
+solution([-1, -1, 5, -1]);