aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2020-09-28 11:00:50 +0200
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2020-09-29 20:51:32 +0200
commit98cd7793d3806a438785d925b92b1353ef2219e9 (patch)
tree243e9fe94731d338a991c088ac5f4a0fd752cfc9
parent0f50bf1e03970b42514876ebb74e1673a2e9a44b (diff)
downloadperlweeklychallenge-club-98cd7793d3806a438785d925b92b1353ef2219e9.tar.gz
perlweeklychallenge-club-98cd7793d3806a438785d925b92b1353ef2219e9.tar.bz2
perlweeklychallenge-club-98cd7793d3806a438785d925b92b1353ef2219e9.zip
Solution to task 1
-rwxr-xr-xchallenge-080/jo-37/perl/ch-1.pl23
1 files changed, 23 insertions, 0 deletions
diff --git a/challenge-080/jo-37/perl/ch-1.pl b/challenge-080/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..90c9bdce69
--- /dev/null
+++ b/challenge-080/jo-37/perl/ch-1.pl
@@ -0,0 +1,23 @@
+#!/usr/bin/perl
+
+use Test2::V0;
+
+# Find the smallest positive number not in given set.
+sub missing_number {
+
+ # Find the first gap within the sorted positive numbers and zero.
+ my $prev = 0;
+ foreach (sort {$a <=> $b} grep $_ > 0, @_) {
+ last if $_ - $prev > 1;
+ $prev = $_;
+ }
+
+ # Successor is the smallest number missing in set.
+ $prev + 1;
+}
+is missing_number(5, 2, -2, 0), 1, 'first example';
+is missing_number(1, 8, -1), 2, 'second example';
+is missing_number(2, 0, -1), 1, 'third example';
+is missing_number(-1, 1, 2, 3), 4, 'no gap in set';
+
+done_testing;