diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-11-18 20:19:27 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-11-18 20:19:27 +0000 |
| commit | cd0a455239b4087598534b4e54dcaab33a96a03d (patch) | |
| tree | 3c22493a077342901e67221bc9f76c5386fc00a0 /challenge-087 | |
| parent | 3e386e736b3c53f8723ef6b731fb732395e502e4 (diff) | |
| parent | 0d9e7c2c5551e2ab26da70fb1a07f40a06d8e5ba (diff) | |
| download | perlweeklychallenge-club-cd0a455239b4087598534b4e54dcaab33a96a03d.tar.gz perlweeklychallenge-club-cd0a455239b4087598534b4e54dcaab33a96a03d.tar.bz2 perlweeklychallenge-club-cd0a455239b4087598534b4e54dcaab33a96a03d.zip | |
Merge branch 'duanepowell-new-branch'
Diffstat (limited to 'challenge-087')
| -rwxr-xr-x | challenge-087/duane-powell/perl/ch-1.pl | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/challenge-087/duane-powell/perl/ch-1.pl b/challenge-087/duane-powell/perl/ch-1.pl new file mode 100755 index 0000000000..fd1bd968fe --- /dev/null +++ b/challenge-087/duane-powell/perl/ch-1.pl @@ -0,0 +1,43 @@ +#!/usr/bin/perl +use warnings; +use strict; +use feature 'say'; + +# Problem: https://perlweeklychallenge.org/blog/perl-weekly-challenge-087/ TASK #1 +# 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 = @ARGV; +unless (scalar @N) { + # @N is empty so fill with random numbers + push @N, int(rand(100))+1 for (1..40); + print join(',',@N), "\n"; + +} + +@N = sort @N; +my @longest; + +# Init @temp with first elemenet of @N +my @temp = (shift @N); +while (scalar @N) { + + # Is the last element of @temp consecutive with the next element of @N ? + if ( $temp[-1]+1 == $N[0] ) { + # Yes, push element + push @temp, shift @N; + } + else { + # No, re-init @temp with first element of @N + @temp = (shift @N); + } + + # Note: the winner of ties go to the first sequence found + @longest = @temp if (scalar @temp > scalar @longest); + +} + +say scalar(@longest) ? join(',',@longest) : 0; +exit; + + |
