aboutsummaryrefslogtreecommitdiff
path: root/challenge-196
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-12-20 09:39:57 +0000
committerGitHub <noreply@github.com>2022-12-20 09:39:57 +0000
commitce490234a8f068946e980dcfce5f9baaf1f136fc (patch)
tree763a50191aa68db55ffcfe56b32913e6ebd76c3e /challenge-196
parentac6dcbcba1247019b8d038afd5cbf1b8025e5fcb (diff)
parentbf684dde6d9020f7fa7db0065fb5173696a89c53 (diff)
downloadperlweeklychallenge-club-ce490234a8f068946e980dcfce5f9baaf1f136fc.tar.gz
perlweeklychallenge-club-ce490234a8f068946e980dcfce5f9baaf1f136fc.tar.bz2
perlweeklychallenge-club-ce490234a8f068946e980dcfce5f9baaf1f136fc.zip
Merge pull request #7289 from carlos157oliveira/challenge-196
feat: solution for challenge 196
Diffstat (limited to 'challenge-196')
-rw-r--r--challenge-196/carlos-oliveira/perl/ch-1.pl37
-rw-r--r--challenge-196/carlos-oliveira/perl/ch-2.pl33
2 files changed, 70 insertions, 0 deletions
diff --git a/challenge-196/carlos-oliveira/perl/ch-1.pl b/challenge-196/carlos-oliveira/perl/ch-1.pl
new file mode 100644
index 0000000000..6d109d302b
--- /dev/null
+++ b/challenge-196/carlos-oliveira/perl/ch-1.pl
@@ -0,0 +1,37 @@
+use strict;
+use warnings;
+use Data::Dump;
+
+sub findThirdValue {
+ my ($arrayRef, $firstValueIndex, $secondValueIndex) = @_;
+ my @array = @$arrayRef;
+ for my $i ($secondValueIndex+1 .. $#array) {
+ return $array[$i] if $array[$i] < $array[$secondValueIndex]
+ && $array[$i] > $array[$firstValueIndex];
+ }
+ return undef
+}
+
+sub findSecondAndThirdValues {
+ my ($arrayRef, $firstValueIndex) = @_;
+ my @array = @$arrayRef;
+ for my $i ($firstValueIndex+1 .. $#array) {
+ next unless $array[$firstValueIndex] < $array[$i];
+ my $value3 = findThirdValue $arrayRef, $firstValueIndex, $i;
+ return ($array[$i], $value3) if defined $value3;
+ }
+ return ();
+}
+
+sub pattern132 {
+ for my $i (0 .. $#_) {
+ my @remainingValues = findSecondAndThirdValues \@_, $i;
+ return ($_[$i], @remainingValues) if @remainingValues > 0
+ }
+ return ();
+}
+
+dd pattern132(3, 1, 4, 2);
+dd pattern132(1, 2, 3, 4);
+dd pattern132(1, 3, 2, 4, 6, 5);
+dd pattern132(1, 3, 4, 2);
diff --git a/challenge-196/carlos-oliveira/perl/ch-2.pl b/challenge-196/carlos-oliveira/perl/ch-2.pl
new file mode 100644
index 0000000000..9354b6942e
--- /dev/null
+++ b/challenge-196/carlos-oliveira/perl/ch-2.pl
@@ -0,0 +1,33 @@
+use strict;
+use warnings;
+use Data::Dump;
+use List::MoreUtils qw(slideatatime);
+
+
+sub intervals {
+ my $isRegistering = 0;
+ my $begin;
+ my @intervals;
+ my $iterator = slideatatime 1, 2, @_;
+
+ # iterator fills with undef the free slots of the last window
+ while (my ($firstItem, $secondItem) = $iterator->()) {
+ unless (defined $secondItem && $firstItem + 1 == $secondItem) {
+ if ($isRegistering) {
+ push @intervals, [$begin, $firstItem];
+ }
+ $isRegistering = 0;
+ next;
+ }
+ unless ($isRegistering) {
+ $isRegistering = 1;
+ $begin = $firstItem;
+ }
+ }
+
+ return @intervals;
+}
+
+dd intervals(1,3,4,5,7);
+dd intervals(1,2,3,6,7,9);
+dd intervals(0,1,2,4,5,6,8,9);