diff options
| author | Walt Mankowski <waltman@pobox.com> | 2020-11-16 10:53:00 -0500 |
|---|---|---|
| committer | Walt Mankowski <waltman@pobox.com> | 2020-11-16 10:53:00 -0500 |
| commit | ab681cef925b5681f5adf5363feba9e7ff3b3813 (patch) | |
| tree | 33d662efdb34365526f9fd6b00af23b2abb7f9b7 | |
| parent | cff21bb0b22502e3b0ef8a4f3946e5f921647115 (diff) | |
| download | perlweeklychallenge-club-ab681cef925b5681f5adf5363feba9e7ff3b3813.tar.gz perlweeklychallenge-club-ab681cef925b5681f5adf5363feba9e7ff3b3813.tar.bz2 perlweeklychallenge-club-ab681cef925b5681f5adf5363feba9e7ff3b3813.zip | |
perl code for challenge 87 task 1
| -rw-r--r-- | challenge-087/walt-mankowski/perl/ch-1.pl | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/challenge-087/walt-mankowski/perl/ch-1.pl b/challenge-087/walt-mankowski/perl/ch-1.pl new file mode 100644 index 0000000000..04501683e5 --- /dev/null +++ b/challenge-087/walt-mankowski/perl/ch-1.pl @@ -0,0 +1,42 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use feature qw(:5.32); +use experimental qw(signatures); + +# TASK #1 › Longest Consecutive Sequence +# Submitted by: Mohammad S Anwar +# +# You are given an unsorted array of integers @N. +# +# Write a script to find the longest consecutive sequence. Print 0 if none sequence found. + +my @n = sort {$a <=> $b} @ARGV; + +my $best_start = 0; +my $best_run = 0; +my $start = -1; + +for my $i (1..$#n) { + # are we in a run? + if ($n[$i] == $n[$i-1] + 1) { + # is this the start of a run? + $start = $i-1 if $start == -1; + + # do we have a new best run? + if ($i - $start > $best_run) { + $best_run = $i - $start; + $best_start = $start; + } + + } else { + # we're not in a run + $start = -1; + } +} + +if ($best_run > 0) { + say "@n[$best_start..$best_start+$best_run]"; +} else { + say 0; +} |
