From 1cacdbb6efed31b4d4bda32d3b548c3ad9205be4 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Mon, 28 Sep 2020 17:10:25 +0200 Subject: file rename --- challenge-080/lubos-kolouch/perl/ch-1.pl | 48 ++++++++++++++++++++++++++++++++ challenge-080/lubos-kolouch/perl/ch-2.pl | 41 +++++++++++++++++++++++++++ challenge-080/lubos-kolouch/perl/ch_1.pl | 46 ------------------------------ challenge-080/lubos-kolouch/perl/ch_2.pl | 41 --------------------------- 4 files changed, 89 insertions(+), 87 deletions(-) create mode 100644 challenge-080/lubos-kolouch/perl/ch-1.pl create mode 100644 challenge-080/lubos-kolouch/perl/ch-2.pl delete mode 100644 challenge-080/lubos-kolouch/perl/ch_1.pl delete mode 100644 challenge-080/lubos-kolouch/perl/ch_2.pl 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..7461ceb1b9 --- /dev/null +++ b/challenge-080/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,48 @@ +#!/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/; +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 + + for (0..scalar @$arr-2) { + $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); + +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; diff --git a/challenge-080/lubos-kolouch/perl/ch_1.pl b/challenge-080/lubos-kolouch/perl/ch_1.pl deleted file mode 100644 index 8362cfa572..0000000000 --- a/challenge-080/lubos-kolouch/perl/ch_1.pl +++ /dev/null @@ -1,46 +0,0 @@ -#!/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/; -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 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]), 3); - -done_testing; diff --git a/challenge-080/lubos-kolouch/perl/ch_2.pl b/challenge-080/lubos-kolouch/perl/ch_2.pl deleted file mode 100644 index 3c01308c37..0000000000 --- a/challenge-080/lubos-kolouch/perl/ch_2.pl +++ /dev/null @@ -1,41 +0,0 @@ -#!/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; -- cgit