aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-11-18 19:44:40 +0000
committerGitHub <noreply@github.com>2020-11-18 19:44:40 +0000
commit00d02e26ee3fbd30b7e3c0b6faa5d310331e57d8 (patch)
tree3cd4a4d3f33cf7a81e24e3f86b7dd2882ad8154c
parentd72e6e77e85e0e4bd4229c140f0c616f3282e1b8 (diff)
parent495641c2abc7a45d3f09d405caa58cb32cd6f34c (diff)
downloadperlweeklychallenge-club-00d02e26ee3fbd30b7e3c0b6faa5d310331e57d8.tar.gz
perlweeklychallenge-club-00d02e26ee3fbd30b7e3c0b6faa5d310331e57d8.tar.bz2
perlweeklychallenge-club-00d02e26ee3fbd30b7e3c0b6faa5d310331e57d8.zip
Merge pull request #2789 from LubosKolouch/master
Challenge 087 Task 1 LK
-rw-r--r--challenge-087/lubos-kolouch/perl/ch-1.pl51
-rw-r--r--challenge-087/lubos-kolouch/python/ch-1.py40
2 files changed, 91 insertions, 0 deletions
diff --git a/challenge-087/lubos-kolouch/perl/ch-1.pl b/challenge-087/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..0bbaea6d7e
--- /dev/null
+++ b/challenge-087/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,51 @@
+#!/usr/bin/perl
+#===============================================================================
+#
+# FILE: ch-1.pl
+#
+# USAGE: ./ch-1.pl
+#
+# DESCRIPTION: Perl Weekly Challenge 087
+# Task 1 - Longest Consecutive Sequence
+#
+# AUTHOR: Lubos Kolouch
+# CREATED: 11/17/2020 04:34:38 PM
+#===============================================================================
+
+use strict;
+use warnings;
+
+sub get_sequence {
+ my $input = shift;
+
+ my @longest_seq;
+ my @curr_seq;
+
+ # loop through the sorted array
+ for my $v (sort {$a <=> $b} @$input) {
+
+ # if the seq is empty or the next number is consecutive, add it
+ if ( (not @curr_seq) or ($v == $curr_seq[$#curr_seq]+1) ) {
+ push @curr_seq, $v;
+ } else {
+ # sequence broken, remember it if it the longest
+ if ($#curr_seq > $#longest_seq) {
+ @longest_seq = @curr_seq;
+ @curr_seq = [];
+ }
+ }
+ }
+
+ # if longes sequence has just 1 item, we haven't found anything
+ return 0 unless $#longest_seq > 1;
+
+ return \@longest_seq;
+}
+
+use Test::More;
+
+is_deeply(get_sequence([100, 4, 50, 3, 2]), [2, 3, 4]);
+is_deeply(get_sequence([20, 30, 10, 40, 50]), 0);
+is_deeply(get_sequence([20, 19, 9, 11, 10]), [9, 10, 11]);
+
+done_testing;
diff --git a/challenge-087/lubos-kolouch/python/ch-1.py b/challenge-087/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..40fd02d41b
--- /dev/null
+++ b/challenge-087/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,40 @@
+#!/bin/env python
+"""
+#===============================================================================
+#
+# FILE: ch-1.py
+#
+# USAGE: ./ch-1.py
+#
+# DESCRIPTION: Perl Weekly Challenge 087
+# Task 1 - Longest Consecutive Sequence
+#
+# AUTHOR: Lubos Kolouch
+# CREATED: 11/17/2020 04:34:38 PM
+#===============================================================================
+"""
+
+
+def get_sequence(in_list):
+ """ Get the longest sequence """
+ longest_seq = []
+ curr_seq = []
+
+ # loop through the sorted array
+ for item in sorted(in_list):
+ # if the seq is empty or the next number is consecutive, add it
+ if (not curr_seq) or (item == curr_seq[-1]+1):
+ curr_seq.append(item)
+ else:
+ # sequence broken, remember it if it the longest
+ if len(curr_seq) > len(longest_seq):
+ longest_seq = curr_seq
+ curr_seq = []
+
+ # if longes sequence has just 1 item, we haven't found anything
+ return 0 if len(longest_seq) == 1 else longest_seq
+
+
+assert get_sequence([100, 4, 50, 3, 2]) == [2, 3, 4]
+assert get_sequence([20, 30, 10, 40, 50]) == 0
+assert get_sequence([20, 19, 9, 11, 10]) == [9, 10, 11]