diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-09-29 05:32:03 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-09-29 05:32:03 +0100 |
| commit | dd10294e222e6feec8ee5e852d8dfbd29a01b191 (patch) | |
| tree | 94e160c0e742c7d8291dd824e29bbb4f0115ab61 | |
| parent | 2b82921f120b74af3be1f4abead86ccdd2262cfd (diff) | |
| parent | 74c8dfaa898bdeb5968c218686860bfcfc334140 (diff) | |
| download | perlweeklychallenge-club-dd10294e222e6feec8ee5e852d8dfbd29a01b191.tar.gz perlweeklychallenge-club-dd10294e222e6feec8ee5e852d8dfbd29a01b191.tar.bz2 perlweeklychallenge-club-dd10294e222e6feec8ee5e852d8dfbd29a01b191.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
| -rw-r--r-- | challenge-080/lubos-kolouch/perl/ch-1.pl | 49 | ||||
| -rw-r--r-- | challenge-080/lubos-kolouch/perl/ch-2.pl | 41 |
2 files changed, 90 insertions, 0 deletions
diff --git a/challenge-080/lubos-kolouch/perl/ch-1.pl b/challenge-080/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..b25e40390f --- /dev/null +++ b/challenge-080/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,49 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: ch_1.pl +# +# USAGE: ./ch_1.pl +# +# DESCRIPTION: https://perlweeklychallenge.org/blog/perl-weekly-challenge-080/ +# Challenge #1 +# Smallest Positive Number Bits +# +# AUTHOR: Lubos Kolouch +#=============================================================================== + +use strict; +use warnings; +use List::Util qw/min max/; +use feature qw/say/; + +sub get_smallest_missing { + my $arr = shift; + + # 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 0 as per Twitter confirmation + + while ($arr_min < max(@$arr)) { + $arr_min++; + + return $arr_min unless grep { $_ == $arr_min } @$arr; + } + + return 0; +} + +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]), 0); +is(get_smallest_missing([2, 0, 1, 3, 4, 5]), 0); + +done_testing; diff --git a/challenge-080/lubos-kolouch/perl/ch-2.pl b/challenge-080/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..3c01308c37 --- /dev/null +++ b/challenge-080/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,41 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: ch_2.pl +# +# USAGE: ./ch_2.pl +# +# DESCRIPTION: https://perlweeklychallenge.org/blog/perl-weekly-challenge-080/ +# Challenge #1 +# Count Candies +# +# AUTHOR: Lubos Kolouch +#=============================================================================== + +use strict; +use warnings; +use List::Util qw/uniq/; + +sub get_candle_count { + my $arr = shift; + + # We need to give 1 candy to everyone + my $count = scalar @$arr; + + # and then find out number of unique elements as they will be certainly + # bigger than neighbor... -1 (the initial poor one) + + $count += scalar uniq @$arr; + $count--; + + return $count; + + +} + +use Test::More; + +is(get_candle_count([1, 2, 2]), 4); +is(get_candle_count([1, 4, 3, 2]), 7); + +done_testing; |
