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