From 52e114caced4bd7e9588f3949d46fb3a04705a33 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Mon, 28 Sep 2020 13:17:45 +0200 Subject: Chal 1 empty with tests --- challenge-080/lubos-kolouch/perl/ch_1.pl | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 challenge-080/lubos-kolouch/perl/ch_1.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..0b712a913a --- /dev/null +++ b/challenge-080/lubos-kolouch/perl/ch_1.pl @@ -0,0 +1,29 @@ +#!/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; + +sub get_smallest_missing { + my $arr = shift; + +} + +use Test::More; + +is(get_smallest_missing([5, 2, -2, 0]), 1); +is(get_smallest_missing([1, 8, -1]), 1); +is(get_smallest_missing([2, 0, 1]), 1); + +done_testing; -- cgit From 7e3512ddfeb976911047c993f357f21abdee6761 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Mon, 28 Sep 2020 13:20:28 +0200 Subject: Chal 1 empty with tests --- challenge-080/lubos-kolouch/perl/ch_1.pl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/challenge-080/lubos-kolouch/perl/ch_1.pl b/challenge-080/lubos-kolouch/perl/ch_1.pl index 0b712a913a..e19e7bf626 100644 --- a/challenge-080/lubos-kolouch/perl/ch_1.pl +++ b/challenge-080/lubos-kolouch/perl/ch_1.pl @@ -14,16 +14,21 @@ use strict; use warnings; +use List::Util qw/min/; +use feature qw/say/; sub get_smallest_missing { my $arr = shift; + # map the arr to hash + say min( grep { $_ > 0 } @$arr); + } use Test::More; is(get_smallest_missing([5, 2, -2, 0]), 1); -is(get_smallest_missing([1, 8, -1]), 1); +is(get_smallest_missing([1, 8, -1]), 2); is(get_smallest_missing([2, 0, 1]), 1); done_testing; -- cgit 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 From 98b8162b7f4bcda6060dedd8e4adce1e76988fa3 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Mon, 28 Sep 2020 13:42:17 +0200 Subject: Solution 2 080 LK --- challenge-080/lubos-kolouch/perl/ch_2.pl | 41 ++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 challenge-080/lubos-kolouch/perl/ch_2.pl 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; -- cgit 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 From 70249bd8c70a1b5cdd374d35e42f0eeec79f35d0 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Mon, 28 Sep 2020 17:15:04 +0200 Subject: Corner case 012 --- challenge-080/lubos-kolouch/perl/ch-1.pl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/challenge-080/lubos-kolouch/perl/ch-1.pl b/challenge-080/lubos-kolouch/perl/ch-1.pl index 7461ceb1b9..8decd6a953 100644 --- a/challenge-080/lubos-kolouch/perl/ch-1.pl +++ b/challenge-080/lubos-kolouch/perl/ch-1.pl @@ -14,7 +14,7 @@ use strict; use warnings; -use List::Util qw/min/; +use List::Util qw/min max/; use feature qw/say/; sub get_smallest_missing { @@ -29,7 +29,7 @@ sub get_smallest_missing { # 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) { + while ($arr_min < max(@$arr)) { $arr_min++; return $arr_min unless grep { $_ == $arr_min } @$arr; -- cgit From b4c10c949c9ecade1377baf7f6468a0c0d77bcbe Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Mon, 28 Sep 2020 17:16:27 +0200 Subject: Added one more test --- challenge-080/lubos-kolouch/perl/ch-1.pl | 1 + 1 file changed, 1 insertion(+) diff --git a/challenge-080/lubos-kolouch/perl/ch-1.pl b/challenge-080/lubos-kolouch/perl/ch-1.pl index 8decd6a953..b25e40390f 100644 --- a/challenge-080/lubos-kolouch/perl/ch-1.pl +++ b/challenge-080/lubos-kolouch/perl/ch-1.pl @@ -44,5 +44,6 @@ 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; -- cgit