aboutsummaryrefslogtreecommitdiff
path: root/challenge-325/packy-anderson/perl/ch-2.pl
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-325/packy-anderson/perl/ch-2.pl')
-rwxr-xr-xchallenge-325/packy-anderson/perl/ch-2.pl30
1 files changed, 30 insertions, 0 deletions
diff --git a/challenge-325/packy-anderson/perl/ch-2.pl b/challenge-325/packy-anderson/perl/ch-2.pl
new file mode 100755
index 0000000000..6ddc4cb7a6
--- /dev/null
+++ b/challenge-325/packy-anderson/perl/ch-2.pl
@@ -0,0 +1,30 @@
+#!/usr/bin/env perl
+use v5.40;
+
+use List::AllUtils qw( first );
+
+sub finalPrice(@prices) {
+ my @discounts;
+ while (my $current = shift @prices) {
+ if (my $lower = first { $_ <= $current} @prices) {
+ $current -= $lower;
+ }
+ push @discounts, $current;
+ }
+ return @discounts;
+}
+
+sub solution($prices) {
+ say 'Input: @prices = (' . join(', ', @$prices) . ')';
+ my @discounts = finalPrice(@$prices);
+ say 'Output: (' . join(', ', @discounts) . ')';
+}
+
+say "Example 1:";
+solution([8, 4, 6, 2, 3]);
+
+say "\nExample 2:";
+solution([1, 2, 3, 4, 5]);
+
+say "\nExample 3:";
+solution([7, 1, 1, 5]);