aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2025-11-02 22:32:36 +1000
committerSimon Green <mail@simon.green>2025-11-02 22:32:36 +1000
commit2afc3dadbb61378fc86bd2fda57871625dbcb8dd (patch)
treea07f1e2d0d26ff5784725317e862ed78bf864715
parent9b1eba54d5e59d05df44f336120a6a03be62a645 (diff)
downloadperlweeklychallenge-club-2afc3dadbb61378fc86bd2fda57871625dbcb8dd.tar.gz
perlweeklychallenge-club-2afc3dadbb61378fc86bd2fda57871625dbcb8dd.tar.bz2
perlweeklychallenge-club-2afc3dadbb61378fc86bd2fda57871625dbcb8dd.zip
sgreen solutions to challenge 345
-rw-r--r--challenge-345/sgreen/README.md4
-rw-r--r--challenge-345/sgreen/blog.txt1
-rwxr-xr-xchallenge-345/sgreen/perl/ch-1.pl34
-rwxr-xr-xchallenge-345/sgreen/perl/ch-2.pl35
-rwxr-xr-xchallenge-345/sgreen/python/ch-1.py42
-rwxr-xr-xchallenge-345/sgreen/python/ch-2.py34
-rwxr-xr-xchallenge-345/sgreen/python/test.py26
7 files changed, 174 insertions, 2 deletions
diff --git a/challenge-345/sgreen/README.md b/challenge-345/sgreen/README.md
index 41cfdac46a..38d33ba9fb 100644
--- a/challenge-345/sgreen/README.md
+++ b/challenge-345/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 344
+# The Weekly Challenge 345
-Blog: [The one about arrays](https://dev.to/simongreennet/weekly-challenge-the-one-about-arrays-1h8k)
+Blog: [Peak Visitors](https://dev.to/simongreennet/weekly-challenge-peak-visitors-3lei)
diff --git a/challenge-345/sgreen/blog.txt b/challenge-345/sgreen/blog.txt
new file mode 100644
index 0000000000..47daf16457
--- /dev/null
+++ b/challenge-345/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/weekly-challenge-peak-visitors-3lei \ No newline at end of file
diff --git a/challenge-345/sgreen/perl/ch-1.pl b/challenge-345/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..40cf5bfa68
--- /dev/null
+++ b/challenge-345/sgreen/perl/ch-1.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub main (@ints) {
+ my @peaks = ();
+
+ # Check first element
+ if ( $ints[0] > $ints[1] ) {
+ push @peaks, 0;
+ }
+
+ # Check middle elements
+ foreach my $pos ( 1 .. $#ints - 1 ) {
+ if ( $ints[$pos] > $ints[ $pos - 1 ]
+ and $ints[$pos] > $ints[ $pos + 1 ] )
+ {
+ push @peaks, $pos;
+ }
+ }
+
+ # Check last element
+ if ( $ints[-1] > $ints[-2] ) {
+ push @peaks, $#ints;
+ }
+
+ # Print list of peak positions
+ say "(", join( ", ", @peaks ), ")";
+}
+
+main(@ARGV);
diff --git a/challenge-345/sgreen/perl/ch-2.pl b/challenge-345/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..8836c90622
--- /dev/null
+++ b/challenge-345/sgreen/perl/ch-2.pl
@@ -0,0 +1,35 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub main (@ints) {
+ my @seen = ();
+ my @ans = ();
+ my $neg_count = 0;
+
+ foreach my $i (@ints) {
+ if ( $i > 0 ) {
+ unshift @seen, $i;
+ $neg_count = 0;
+ }
+ elsif ( $i == -1 ) {
+ if ( $neg_count < @seen ) {
+ push @ans, $seen[$neg_count];
+ }
+ else {
+ push @ans, -1;
+ }
+ $neg_count++;
+ }
+ else {
+ die "Input integers must be positive or -1.\n";
+ }
+ }
+
+ say "(", join( ", ", @ans ), ")";
+}
+
+main(@ARGV);
diff --git a/challenge-345/sgreen/python/ch-1.py b/challenge-345/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..6211e45304
--- /dev/null
+++ b/challenge-345/sgreen/python/ch-1.py
@@ -0,0 +1,42 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def peak_positions(ints: list[int]) -> list[int]:
+ """Return list of positions of peaks in the input list.
+
+ Args:
+ ints (list[int]): List of integers to analyze.
+
+ Returns:
+ list[int]: List of indices where peaks are located.
+ """
+ peaks = []
+
+ # Check first element
+ if ints[0] > ints[1]:
+ peaks.append(0)
+
+ # Check middle elements
+ for pos in range(1, len(ints) - 1):
+ if ints[pos] > ints[pos - 1] and ints[pos] > ints[pos + 1]:
+ peaks.append(pos)
+
+ # Check last element
+ if ints[-1] > ints[-2]:
+ peaks.append(len(ints) - 1)
+
+ # Return list of peak positions
+ return peaks
+
+
+def main():
+ # Convert input into integers
+ array = [int(n) for n in sys.argv[1:]]
+ result = peak_positions(array)
+ print('(' + ', '.join(map(str, result)) + ')')
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-345/sgreen/python/ch-2.py b/challenge-345/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..93797c9ee8
--- /dev/null
+++ b/challenge-345/sgreen/python/ch-2.py
@@ -0,0 +1,34 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def last_visitor(ints: list[int]) -> list[int]:
+ seen = []
+ ans = []
+ neg_count = 0
+
+ for i in ints:
+ if i > 0:
+ seen.insert(0, i)
+ neg_count = 0
+ elif i == -1:
+ if neg_count < len(seen):
+ ans.append(seen[neg_count])
+ else:
+ ans.append(-1)
+ neg_count += 1
+ else:
+ raise ValueError("Input integers must be positive or -1.")
+
+ return ans
+
+def main():
+ # Convert input into integers
+ array = [int(n) for n in sys.argv[1:]]
+ result = last_visitor(array)
+ print('(' + ', '.join(map(str, result)) + ')')
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-345/sgreen/python/test.py b/challenge-345/sgreen/python/test.py
new file mode 100755
index 0000000000..bbf9c70167
--- /dev/null
+++ b/challenge-345/sgreen/python/test.py
@@ -0,0 +1,26 @@
+#!/usr/bin/env python3
+
+import unittest
+ch_1 = __import__('ch-1')
+ch_2 = __import__('ch-2')
+
+
+class TestClass(unittest.TestCase):
+ def test_ch_1(self):
+ self.assertEqual(ch_1.peak_positions([1, 3, 2]), [1])
+ self.assertEqual(ch_1.peak_positions([2, 4, 6, 5, 3]), [2])
+ self.assertEqual(ch_1.peak_positions([1, 2, 3, 2, 4, 1]), [2, 4])
+ self.assertEqual(ch_1.peak_positions([5, 3, 1]), [0])
+ self.assertEqual(ch_1.peak_positions([1, 5, 1, 5, 1, 5, 1]), [1, 3, 5])
+ self.assertEqual(ch_1.peak_positions([1, 2, 3, 4, 4]), [])
+
+ def test_ch_2(self):
+ self.assertEqual(ch_2.last_visitor([5, -1, -1]), [5, -1])
+ self.assertEqual(ch_2.last_visitor([3, 7, -1, -1, -1]), [7, 3, -1])
+ self.assertEqual(ch_2.last_visitor([2, -1, 4, -1, -1]), [2, 4, 2])
+ self.assertEqual(ch_2.last_visitor([10, 20, -1, 30, -1, -1]), [20, 30, 20])
+ self.assertEqual(ch_2.last_visitor([-1, -1, 5, -1]), [-1, -1, 5])
+
+
+if __name__ == '__main__':
+ unittest.main()