aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Jacoby <jacoby.david@gmail.com>2023-02-15 11:51:39 -0500
committerDave Jacoby <jacoby.david@gmail.com>2023-02-15 11:51:39 -0500
commit714bd7fa84dc5532ddc15cbe577669cdb3a70fae (patch)
tree785a4db6399016186d50c0de7325297fa416fc87
parent24d339929a25ed203e44ea5d46fcdcc552d2e139 (diff)
parent6051a217ebf6f4804bf3015fc670971a489aecb0 (diff)
downloadperlweeklychallenge-club-714bd7fa84dc5532ddc15cbe577669cdb3a70fae.tar.gz
perlweeklychallenge-club-714bd7fa84dc5532ddc15cbe577669cdb3a70fae.tar.bz2
perlweeklychallenge-club-714bd7fa84dc5532ddc15cbe577669cdb3a70fae.zip
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
-rw-r--r--challenge-204/ash/blog-1.txt1
-rw-r--r--challenge-204/ash/blog-2.txt1
-rw-r--r--challenge-204/ash/chatgpt/README7
-rw-r--r--challenge-204/ash/chatgpt/blog-1.txt1
-rw-r--r--challenge-204/ash/chatgpt/blog-2.txt1
-rw-r--r--challenge-204/ash/chatgpt/raku/ch-1.raku33
-rw-r--r--challenge-204/ash/chatgpt/raku/ch-2.raku56
-rw-r--r--challenge-204/ash/raku/ch-1.raku23
-rw-r--r--challenge-204/ash/raku/ch-2.raku48
-rw-r--r--challenge-204/carlos-oliveira/perl/ch-1.pl20
-rw-r--r--challenge-204/carlos-oliveira/perl/ch-2.pl42
-rwxr-xr-xchallenge-204/e-choroba/perl/ch-1.pl26
-rwxr-xr-xchallenge-204/e-choroba/perl/ch-2.pl25
-rwxr-xr-xchallenge-204/eric-cheung/python/ch-1.py33
-rwxr-xr-xchallenge-204/eric-cheung/python/ch-2.py32
-rw-r--r--challenge-204/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-204/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-204/jeanluc2020/perl/ch-1.pl86
-rwxr-xr-xchallenge-204/jeanluc2020/perl/ch-2.pl136
-rw-r--r--challenge-204/lubos-kolouch/perl/ch-1.pl38
-rw-r--r--challenge-204/lubos-kolouch/perl/ch-2.pl38
-rw-r--r--challenge-204/lubos-kolouch/python/ch-1.py32
-rw-r--r--challenge-204/lubos-kolouch/python/ch-2.py28
-rw-r--r--challenge-204/luca-ferrari/blog-1.txt1
-rw-r--r--challenge-204/luca-ferrari/blog-2.txt1
-rw-r--r--challenge-204/luca-ferrari/blog-3.txt1
-rw-r--r--challenge-204/luca-ferrari/blog-4.txt1
-rw-r--r--challenge-204/luca-ferrari/blog-5.txt1
-rw-r--r--challenge-204/luca-ferrari/blog-6.txt1
-rw-r--r--challenge-204/luca-ferrari/postgresql/ch-1.plperl30
-rw-r--r--challenge-204/luca-ferrari/postgresql/ch-1.sql43
-rw-r--r--challenge-204/luca-ferrari/postgresql/ch-2.plperl39
-rw-r--r--challenge-204/luca-ferrari/postgresql/ch-2.sql37
-rw-r--r--challenge-204/luca-ferrari/raku/ch-1.p625
-rw-r--r--challenge-204/luca-ferrari/raku/ch-2.p632
-rw-r--r--challenge-204/mark-anderson/raku/ch-1.raku13
-rw-r--r--challenge-204/mark-anderson/raku/ch-2.raku12
-rwxr-xr-xchallenge-204/mattneleigh/perl/ch-1.pl88
-rwxr-xr-xchallenge-204/mattneleigh/perl/ch-2.pl160
-rw-r--r--challenge-204/robbie-hatley/blog.txt1
-rwxr-xr-xchallenge-204/robbie-hatley/perl/ch-1.pl59
-rwxr-xr-xchallenge-204/robbie-hatley/perl/ch-2.pl118
-rw-r--r--challenge-204/robert-dicicco/julia/ch-1.jl101
-rw-r--r--challenge-204/robert-dicicco/raku/ch-1.raku91
-rw-r--r--challenge-204/robert-dicicco/ruby/ch-1.rb79
-rw-r--r--challenge-204/wlmb/blog.txt2
-rwxr-xr-xchallenge-204/wlmb/perl/ch-1.pl25
-rwxr-xr-xchallenge-204/wlmb/perl/ch-2.pl22
-rw-r--r--stats/pwc-challenge-203.json570
-rw-r--r--stats/pwc-current.json551
-rw-r--r--stats/pwc-language-breakdown-summary.json56
-rw-r--r--stats/pwc-language-breakdown.json1401
-rw-r--r--stats/pwc-leaders.json468
-rw-r--r--stats/pwc-summary-1-30.json114
-rw-r--r--stats/pwc-summary-121-150.json120
-rw-r--r--stats/pwc-summary-151-180.json98
-rw-r--r--stats/pwc-summary-181-210.json98
-rw-r--r--stats/pwc-summary-211-240.json42
-rw-r--r--stats/pwc-summary-241-270.json114
-rw-r--r--stats/pwc-summary-271-300.json34
-rw-r--r--stats/pwc-summary-31-60.json36
-rw-r--r--stats/pwc-summary-61-90.json100
-rw-r--r--stats/pwc-summary-91-120.json102
-rw-r--r--stats/pwc-summary.json644
64 files changed, 4096 insertions, 2144 deletions
diff --git a/challenge-204/ash/blog-1.txt b/challenge-204/ash/blog-1.txt
new file mode 100644
index 0000000000..59e520d49a
--- /dev/null
+++ b/challenge-204/ash/blog-1.txt
@@ -0,0 +1 @@
+https://andrewshitov.com/2023/02/13/dialogues-with-chatpgp-about-the-raku-programming-languages-solving-the-weekly-challange-204/
diff --git a/challenge-204/ash/blog-2.txt b/challenge-204/ash/blog-2.txt
new file mode 100644
index 0000000000..8fabcdca67
--- /dev/null
+++ b/challenge-204/ash/blog-2.txt
@@ -0,0 +1 @@
+https://andrewshitov.com/2023/02/13/solving-task-2-of-the-weekly-challenge-204-with-the-help-of-chatgpt/
diff --git a/challenge-204/ash/chatgpt/README b/challenge-204/ash/chatgpt/README
new file mode 100644
index 0000000000..7735e06a24
--- /dev/null
+++ b/challenge-204/ash/chatgpt/README
@@ -0,0 +1,7 @@
+Solutions generated by ChatGPT.
+
+The solutions are based on the description and the examples that the Weekly Challenge offers. It was sent to the chat as they are published on the website without any modifications.
+
+The solution located in this directory are the first versions that ChatGPT generated. The code is taken as soon as it compiles and gives correct results. Optimised versions with a more natural Raku syntax are placed at ../ash. All those were also done in a dialogue with the machine with some hints from a human.
+
+Submitted by Andrew Shitov (ash).
diff --git a/challenge-204/ash/chatgpt/blog-1.txt b/challenge-204/ash/chatgpt/blog-1.txt
new file mode 100644
index 0000000000..59e520d49a
--- /dev/null
+++ b/challenge-204/ash/chatgpt/blog-1.txt
@@ -0,0 +1 @@
+https://andrewshitov.com/2023/02/13/dialogues-with-chatpgp-about-the-raku-programming-languages-solving-the-weekly-challange-204/
diff --git a/challenge-204/ash/chatgpt/blog-2.txt b/challenge-204/ash/chatgpt/blog-2.txt
new file mode 100644
index 0000000000..8fabcdca67
--- /dev/null
+++ b/challenge-204/ash/chatgpt/blog-2.txt
@@ -0,0 +1 @@
+https://andrewshitov.com/2023/02/13/solving-task-2-of-the-weekly-challenge-204-with-the-help-of-chatgpt/
diff --git a/challenge-204/ash/chatgpt/raku/ch-1.raku b/challenge-204/ash/chatgpt/raku/ch-1.raku
new file mode 100644
index 0000000000..cbc18bdd63
--- /dev/null
+++ b/challenge-204/ash/chatgpt/raku/ch-1.raku
@@ -0,0 +1,33 @@
+# This program has been created by ChatGPT
+#
+# Test run:
+# $ raku ch-1.raku
+# 1
+# 0
+# 1
+
+sub is_monotonic(@nums) {
+ my $increasing = True;
+ my $decreasing = True;
+ for (0 ..^ @nums.end) -> $i {
+ for ($i + 1 .. @nums.end) -> $j {
+ if (@nums[$i] > @nums[$j]) {
+ $increasing = False;
+ }
+ if (@nums[$i] < @nums[$j]) {
+ $decreasing = False;
+ }
+ }
+ }
+ return 1 if $increasing || $decreasing;
+ return 0;
+ }
+
+my @nums = (1, 2, 2, 3);
+say is_monotonic(@nums);
+
+@nums = (1, 3, 2);
+say is_monotonic(@nums);
+
+@nums = (6, 5, 5, 4);
+say is_monotonic(@nums);
diff --git a/challenge-204/ash/chatgpt/raku/ch-2.raku b/challenge-204/ash/chatgpt/raku/ch-2.raku
new file mode 100644
index 0000000000..20375d1164
--- /dev/null
+++ b/challenge-204/ash/chatgpt/raku/ch-2.raku
@@ -0,0 +1,56 @@
+# This program has been created by ChatGPT
+#
+# Test run:
+# $ raku ch-2.raku
+# [1 2 3 4]
+# [[1 2] [3 4] [5 6]]
+# [[1] [2]]
+#
+# Notice that if you change $r and $c to 3 and 2 in the last example,
+# the program prints 0 as expected.
+
+sub reshape-matrix (@matrix, $r, $c) {
+ my $total_elements = 0;
+ for @matrix -> @row {
+ $total_elements += @row.elems;
+ }
+ if $total_elements != $r * $c {
+ return 0;
+ }
+
+ my @result = [];
+ my $element_index = 0;
+ for (0 .. $r - 1) -> $i {
+ my @new_row = [];
+ for (0 .. $c - 1) -> $j {
+ @new_row.push(@matrix[$element_index div @matrix[0].elems][$element_index % @matrix[0].elems]);
+ $element_index++;
+ }
+ @result.push(@new_row);
+ }
+
+ if @result.elems == 1 {
+ return @result[0];
+ }
+ else {
+ return @result;
+ }
+}
+
+my @matrix = [ [ 1, 2 ], [ 3, 4 ] ];
+my $r = 1;
+my $c = 4;
+my $result = reshape-matrix(@matrix, $r, $c);
+say $result;
+
+@matrix = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ];
+$r = 3;
+$c = 2;
+$result = reshape-matrix(@matrix, $r, $c);
+say $result;
+
+@matrix = [ [ 1 ], [ 2 ] ];
+$r = 2;
+$c = 1;
+$result = reshape-matrix(@matrix, $r, $c);
+say $result;
diff --git a/challenge-204/ash/raku/ch-1.raku b/challenge-204/ash/raku/ch-1.raku
new file mode 100644
index 0000000000..ad2c0b4410
--- /dev/null
+++ b/challenge-204/ash/raku/ch-1.raku
@@ -0,0 +1,23 @@
+# A Raku solution to the Task 1 "Monotonic Array" of the Weekly Challenge 204
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-204/#TASK1
+# Solved together with ChatGPT, the dialogue published at
+# https://andrewshitov.com/2023/02/13/dialogues-with-chatpgp-about-the-raku-programming-languages-solving-the-weekly-challange-204/
+
+# Test run:
+# $ raku ch-1.raku
+# 1
+# 0
+# 1
+
+sub is-monotonic(@nums) {
+ [>=] @nums or [<=] @nums
+}
+
+my @nums = (1, 2, 2, 3);
+say +is-monotonic(@nums);
+
+@nums = (1, 3, 2);
+say +is-monotonic(@nums);
+
+@nums = (6, 5, 5, 4);
+say +is-monotonic(@nums);
diff --git a/challenge-204/ash/raku/ch-2.raku b/challenge-204/ash/raku/ch-2.raku
new file mode 100644
index 0000000000..e6a2b13241
--- /dev/null
+++ b/challenge-204/ash/raku/ch-2.raku
@@ -0,0 +1,48 @@
+# A Raku solution to the Task 2 "Reshape Matrix" of the Weekly Challenge 204
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-204/#TASK2
+# Solved together with ChatGPT. The whole dialogue with the machine is published in my blog:
+# https://andrewshitov.com/2023/02/13/solving-task-2-of-the-weekly-challenge-204-with-the-help-of-chatgpt/
+
+# Test run:
+# $ raku ch-2.raku
+# [1 2 3 4]
+# [[1 2] [3 4] [5 6]]
+# [[1] [2]]
+# 0
+
+sub reshape-matrix (@matrix, $r, $c) {
+ my @flat = @matrix.map(*.flat).flat;
+ my $total_elements = @flat.elems;
+ return 0 if $total_elements != $r * $c;
+
+ my @result;
+ for ^$r -> $i {
+ push @result, [@flat[$c * $i .. $c * $i + $c - 1]];
+ }
+
+ return @result.elems == 1 ?? @result[0] !! @result;
+}
+
+my @matrix = [ [ 1, 2 ], [ 3, 4 ] ];
+my $r = 1;
+my $c = 4;
+my $result = reshape-matrix(@matrix, $r, $c);
+say $result;
+
+@matrix = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ];
+$r = 3;
+$c = 2;
+$result = reshape-matrix(@matrix, $r, $c);
+say $result;
+
+@matrix = [ [ 1 ], [ 2 ] ];
+$r = 2;
+$c = 1;
+$result = reshape-matrix(@matrix, $r, $c);
+say $result;
+
+@matrix = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ];
+$r = 2;
+$c = 4;
+$result = reshape-matrix(@matrix, $r, $c);
+say $result;
diff --git a/challenge-204/carlos-oliveira/perl/ch-1.pl b/challenge-204/carlos-oliveira/perl/ch-1.pl
new file mode 100644
index 0000000000..08e87559d2
--- /dev/null
+++ b/challenge-204/carlos-oliveira/perl/ch-1.pl
@@ -0,0 +1,20 @@
+use strict;
+use warnings;
+use v5.36;
+
+use List::MoreUtils qw(slide);
+use List::Util qw(none);
+
+use Test::More;
+
+sub is_monotonic_array (@array) {
+ my @diffs = slide { $a <=> $b } @array;
+ # Perl is returning '' instead of 0 ?
+ return (none { $_ == -1 } @diffs) || (none { $_ == 1 } @diffs) ? 1 : 0;
+}
+
+is is_monotonic_array (1,2,2,3), 1;
+is is_monotonic_array (1,3,2), 0;
+is is_monotonic_array (6,5,5,4), 1;
+
+done_testing;
diff --git a/challenge-204/carlos-oliveira/perl/ch-2.pl b/challenge-204/carlos-oliveira/perl/ch-2.pl
new file mode 100644
index 0000000000..8f5be7be1d
--- /dev/null
+++ b/challenge-204/carlos-oliveira/perl/ch-2.pl
@@ -0,0 +1,42 @@
+use strict;
+use warnings;
+use v5.36;
+
+use experimental qw(refaliasing);
+
+use Test::More;
+
+sub reshape_matrix ($matrix, $nr, $nc) {
+ # get old rows and columns counts
+ my $or = $matrix->@*;
+ my $oc;
+ if (ref $matrix->[0] eq 'ARRAY') {
+ $oc = $matrix->[0]->@*;
+ } else {
+ $oc = 1;
+ $matrix = [$matrix];
+ }
+ return 0 unless $nr * $nc == $or * $oc;
+ my @new_matrix;
+ my @new_row;
+ my $col = 0;
+ for \my @row (@$matrix) {
+ for my $elem (@row) {
+ push @new_row, $elem;
+ if (++$col == $nc) {
+ push @new_matrix, [@new_row];
+ @new_row = ();
+ $col = 0;
+ }
+ }
+ }
+ return $nr == 1 ? $new_matrix[0] : \@new_matrix;
+}
+
+is_deeply reshape_matrix ([ [ 1, 2 ], [ 3, 4 ] ], 1, 4), [ 1, 2, 3, 4 ];
+is_deeply reshape_matrix ([ [ 1, 2, 3 ] , [ 4, 5, 6 ] ], 3, 2), [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ];
+is reshape_matrix ([ 1, 2 ], 3, 2), 0;
+is_deeply reshape_matrix ([ [ 1, 2, 3, 4 ] ], 1, 4), [ 1, 2, 3, 4 ];
+is_deeply reshape_matrix ([ 1, 2, 3, 4 ], 2, 2), [ [ 1, 2 ], [ 3, 4 ] ];
+
+done_testing;
diff --git a/challenge-204/e-choroba/perl/ch-1.pl b/challenge-204/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..e3e964e256
--- /dev/null
+++ b/challenge-204/e-choroba/perl/ch-1.pl
@@ -0,0 +1,26 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental 'signatures';
+
+sub monotonic_array($arr) {
+ my $first_cmp;
+ for my $i (1 .. $#$arr) {
+ my $cmp = $arr->[ $i - 1 ] <=> $arr->[$i];
+ return 0 if $first_cmp && $cmp && $first_cmp != $cmp;
+ $first_cmp //= $cmp;
+ }
+ return 1
+}
+
+use Test::More tests => 3 + 5;
+
+is monotonic_array([1, 2, 2, 3]), 1, 'Example 1';
+is monotonic_array([1, 3, 2]), 0, 'Example 2';
+is monotonic_array([6, 5, 5, 4]), 1, 'Example 3';
+
+is monotonic_array([5, 5, 4, 3]), 1, 'Starts flat';
+is monotonic_array([2, 2, 2]), 1, 'All flat';
+is monotonic_array([1, 2]), 1, 'Two elements';
+is monotonic_array([1]), 1, 'Single element';
+is monotonic_array([]), 1, 'Empty';
diff --git a/challenge-204/e-choroba/perl/ch-2.pl b/challenge-204/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..c02cfdbc9f
--- /dev/null
+++ b/challenge-204/e-choroba/perl/ch-2.pl
@@ -0,0 +1,25 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental 'signatures';
+
+use PDL;
+
+sub reshape_matrix($matrix, $r, $c) {
+ my $p = pdl($matrix);
+ die 'Two dimensional matrix expected' unless $p->ndims == 2;
+
+ return 0 unless $r * $c == $p->getdim(0) * $p->getdim(1);
+
+ return unpdl($p->reshape($c, $r))
+}
+
+# Import explicitly, some functions (e.g. float) conflict with PDL's ones.
+use Test2::V0 qw{ is plan };
+plan 3;
+
+is reshape_matrix([[1, 2], [3, 4]], 1, 4), [[1, 2, 3, 4]], 'Example 1';
+is reshape_matrix([[1, 2, 3], [4, 5, 6]], 3, 2),
+ [[1, 2], [3, 4], [5, 6]],
+ 'Example 2';
+is reshape_matrix([[1, 2]], 3, 2), 0, 'Example 3';
diff --git a/challenge-204/eric-cheung/python/ch-1.py b/challenge-204/eric-cheung/python/ch-1.py
new file mode 100755
index 0000000000..e4399c7369
--- /dev/null
+++ b/challenge-204/eric-cheung/python/ch-1.py
@@ -0,0 +1,33 @@
+
+def IsMonotone(arrInput):
+
+ bConstantArr = True
+ bMontoneInc = False
+
+ for nLastIndx in range(1, len(arrInput)):
+ if arrInput[nLastIndx - 1] == arrInput[nLastIndx]:
+ continue
+
+ bConstantArr = False
+
+ if arrInput[nLastIndx - 1] < arrInput[nLastIndx]:
+ bMontoneInc = True
+ break
+
+ break
+
+ if bConstantArr:
+ return "1"
+
+ for nLoopIndx in range(nLastIndx + 1, len(arrInput)):
+ if bMontoneInc and arrInput[nLoopIndx - 1] > arrInput[nLoopIndx] or not bMontoneInc and arrInput[nLoopIndx - 1] < arrInput[nLoopIndx]:
+ return "0"
+
+ return "1"
+
+
+## nInputArr = [1, 2, 2, 3] ## Example 1
+## nInputArr = [1, 3, 2] ## Example 2
+nInputArr = [6, 5, 5, 4] ## Example 3
+
+print (IsMonotone(nInputArr))
diff --git a/challenge-204/eric-cheung/python/ch-2.py b/challenge-204/eric-cheung/python/ch-2.py
new file mode 100755
index 0000000000..5c6194a631
--- /dev/null
+++ b/challenge-204/eric-cheung/python/ch-2.py
@@ -0,0 +1,32 @@
+
+import numpy as np
+
+## Example 1
+## matrixInput = np.array([[1, 2], [3, 4]])
+## nMatrixReShapeRow = 1
+## nMatrixReShapeCol = 4
+
+## Example 2
+## matrixInput = np.array([[1, 2, 3], [4, 5, 6]])
+## nMatrixReShapeRow = 3
+## nMatrixReShapeCol = 2
+
+## Example 3
+matrixInput = np.array([1, 2])
+nMatrixReShapeRow = 3
+nMatrixReShapeCol = 2
+
+
+nMatrixRow = len(matrixInput)
+
+try:
+ nMatrixCol = len(matrixInput[0])
+except:
+ nMatrixCol = 0
+
+
+if nMatrixRow * nMatrixCol != nMatrixReShapeRow * nMatrixReShapeCol:
+ print ("0")
+else:
+ matrixOutput = matrixInput.reshape(nMatrixReShapeRow, nMatrixReShapeCol)
+ print (matrixOutput)
diff --git a/challenge-204/jeanluc2020/blog-1.txt b/challenge-204/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..ecda755aca
--- /dev/null
+++ b/challenge-204/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-204-1.html
diff --git a/challenge-204/jeanluc2020/blog-2.txt b/challenge-204/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..b687d7ff97
--- /dev/null
+++ b/challenge-204/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-204-2.html
diff --git a/challenge-204/jeanluc2020/perl/ch-1.pl b/challenge-204/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..c4db878e83
--- /dev/null
+++ b/challenge-204/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,86 @@
+#!/usr/bin/perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-204/#TASK1
+# Task 1: Monotonic Array
+#
+# You are given an array of integers.
+#
+# Write a script to find out if the given array is Monotonic. Print 1 if it is otherwise 0.
+#
+## An array is Monotonic if it is either monotone increasing or decreasing.
+#
+## Monotone increasing: for i <= j , nums[i] <= nums[j]
+## Monotone decreasing: for i <= j , nums[i] >= nums[j]
+#
+#
+## Example 1
+##
+## Input: @nums = (1,2,2,3)
+## Output: 1
+#
+## Example 2
+##
+## Input: @nums (1,3,2)
+## Output: 0
+#
+## Example 3
+##
+## Input: @nums = (6,5,5,4)
+## Output: 1
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# While walking the array we have to check in each step if
+# we're still monotone. In order to achieve this we need to
+# know if we're monotone increasing or monotone decreasing
+# so far (or still constant, in which case both are still
+# possible). If we were increasing and switch to decreasing
+# or vice versa the whole array is not monotone and we can
+# return 0 right away. If we reach the end of the array
+# without any such switch we are monotone and can return 1.
+
+use strict;
+use warnings;
+
+my @examples = (
+ [1, 2, 2, 3],
+ [1, 3, 2],
+ [6, 5, 5, 4]
+);
+
+foreach my $nums (@examples) {
+ print "Input: (" . join(", ", @$nums) . ")\n";
+ print "Output: " . is_monotone(@$nums) . "\n";
+}
+
+# given an array, return 1 if it is monotone and 0 otherwise
+sub is_monotone {
+ # put the first element of the array into $last, the rest into @nums
+ my ($last, @nums) = @_;
+ my $direction = 0; # so far we're neither increasing nor decreasing
+ foreach my $elem (@nums) {
+ if($direction > 0) { # we're monotone increasing so far
+ if($elem < $last) {
+ # we're no longer monotone
+ return 0;
+ }
+ } elsif ($direction < 0) { # we're monotone decreasing so far
+ if($elem > $last) {
+ # we're no longer monotone
+ return 0;
+ }
+ } else { # still constant, we can still be increasing or decreasing
+ if($elem > $last) {
+ $direction = 1; # now we know we're increasing
+ } elsif ($elem < $last) {
+ $direction = -1; # now we know we're decreasing
+ }
+ }
+ # make sure we have $last set to the previous element in the next step
+ $last = $elem;
+ }
+ return 1;
+}
diff --git a/challenge-204/jea