From 9bf288b674a30524ceb807c8242bb0cd7443e81a Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Mon, 28 Sep 2020 13:26:19 +0200 Subject: Solution 1 --- challenge-080/lubos-kolouch/perl/ch_1.pl | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/challenge-080/lubos-kolouch/perl/ch_1.pl b/challenge-080/lubos-kolouch/perl/ch_1.pl index e19e7bf626..8362cfa572 100644 --- a/challenge-080/lubos-kolouch/perl/ch_1.pl +++ b/challenge-080/lubos-kolouch/perl/ch_1.pl @@ -20,15 +20,27 @@ use feature qw/say/; sub get_smallest_missing { my $arr = shift; - # map the arr to hash - say min( grep { $_ > 0 } @$arr); + # Let's see if are lucky and the minimum -1 is >0 + my $arr_min = min( grep { $_ > 0 } @$arr); + return $arr_min-1 if $arr_min-1 > 0; + + # Not lucky, arr_min is 0, so need to iterate + # NOTE: the excercise does not say what to do if there is nothing missing + # so let's just return min + 1 and ask on Twitter the author :) + + while (1) { + $arr_min++; + + return $arr_min unless grep { $_ == $arr_min } @$arr; + } } use Test::More; is(get_smallest_missing([5, 2, -2, 0]), 1); is(get_smallest_missing([1, 8, -1]), 2); -is(get_smallest_missing([2, 0, 1]), 1); +is(get_smallest_missing([2, 0, -1]), 1); +is(get_smallest_missing([2, 0, 1]), 3); done_testing; -- cgit