aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-09-29 05:31:24 +0100
committerGitHub <noreply@github.com>2020-09-29 05:31:24 +0100
commit74c8dfaa898bdeb5968c218686860bfcfc334140 (patch)
treee74be6a62b02d5f348f22e09e67979c65d71fe3b
parent7ce6d572e493691000a38b62899d4a59c01200fb (diff)
parentb4c10c949c9ecade1377baf7f6468a0c0d77bcbe (diff)
downloadperlweeklychallenge-club-74c8dfaa898bdeb5968c218686860bfcfc334140.tar.gz
perlweeklychallenge-club-74c8dfaa898bdeb5968c218686860bfcfc334140.tar.bz2
perlweeklychallenge-club-74c8dfaa898bdeb5968c218686860bfcfc334140.zip
Merge pull request #2396 from LubosKolouch/chal_080_LK
Solution 080 Challenge 1 Perl LK
-rw-r--r--challenge-080/lubos-kolouch/perl/ch-1.pl49
-rw-r--r--challenge-080/lubos-kolouch/perl/ch-2.pl41
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;