diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2020-09-28 13:26:19 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2020-09-28 13:26:19 +0200 |
| commit | 9bf288b674a30524ceb807c8242bb0cd7443e81a (patch) | |
| tree | d17edb464a81a4f48e82e6e9ce705a83293e9a53 | |
| parent | 7e3512ddfeb976911047c993f357f21abdee6761 (diff) | |
| download | perlweeklychallenge-club-9bf288b674a30524ceb807c8242bb0cd7443e81a.tar.gz perlweeklychallenge-club-9bf288b674a30524ceb807c8242bb0cd7443e81a.tar.bz2 perlweeklychallenge-club-9bf288b674a30524ceb807c8242bb0cd7443e81a.zip | |
Solution 1
| -rw-r--r-- | challenge-080/lubos-kolouch/perl/ch_1.pl | 18 |
1 files 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; |
