From 1526588a66a2b61619ea647c6b975907f13588f5 Mon Sep 17 00:00:00 2001 From: Ysmael Ebreo Date: Sun, 30 Aug 2020 23:40:35 +0800 Subject: perl solution for ch#75 --- challenge-075/yet-ebreo/perl/ch-1.pl | 18 ++++++++++++++ challenge-075/yet-ebreo/perl/ch-2.pl | 46 ++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 challenge-075/yet-ebreo/perl/ch-1.pl create mode 100644 challenge-075/yet-ebreo/perl/ch-2.pl diff --git a/challenge-075/yet-ebreo/perl/ch-1.pl b/challenge-075/yet-ebreo/perl/ch-1.pl new file mode 100644 index 0000000000..21db363590 --- /dev/null +++ b/challenge-075/yet-ebreo/perl/ch-1.pl @@ -0,0 +1,18 @@ +#! /usr/bin/perl +use strict; +use warnings; +use feature 'say'; +use Algorithm::Combinatorics 'combinations_with_repetition'; +use List::Util 'sum'; + +my @C = sort {$a-$b} (1, 2, 4); +my $S = 6; + +my $max = 0 | $S / $C[0]; + + +for my $k (1+$S/$C[-1] .. $max) { + for my $v ( combinations_with_repetition(\@C, $k) ) { + ($S == sum @$v) && say "@$v" + } +} \ No newline at end of file diff --git a/challenge-075/yet-ebreo/perl/ch-2.pl b/challenge-075/yet-ebreo/perl/ch-2.pl new file mode 100644 index 0000000000..ed2baf0412 --- /dev/null +++ b/challenge-075/yet-ebreo/perl/ch-2.pl @@ -0,0 +1,46 @@ +#! /usr/bin/perl +use strict; +use warnings; +use feature 'say'; +use List::Util 'max'; + +my @R = (2, 1, 4, 5, 3, 7); + +# 7 # +# 6 # +# 5 # # +# 4 # # # +# 3 # # # # +# 2 # # # # # +# 1 # # # # # # +# _ _ _ _ _ _ _ +# 2 1 4 5 3 7 + +# Result : 12 +my @res; +for my $f (@R) { + my $s = 0; + for my $e (@R) { + if ($e >= $f) { + $s++ + } else { + push @res, $s * $f; + $s = 0; + } + } + push @res, $s * $f; +} + +#Will work nicely on values < 10 +for my $t (-(max @R) .. -1) { + print -$t; + say map { $_ >= -$t ? " #" : " " } @R +} +say "- " x (@R+1); +say " @R\n"; + +say max @res + + + + -- cgit