aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-219/peter-meszaros/perl/ch-1.pl45
-rwxr-xr-xchallenge-219/peter-meszaros/perl/ch-2.pl98
2 files changed, 143 insertions, 0 deletions
diff --git a/challenge-219/peter-meszaros/perl/ch-1.pl b/challenge-219/peter-meszaros/perl/ch-1.pl
new file mode 100755
index 0000000000..d74df4f83a
--- /dev/null
+++ b/challenge-219/peter-meszaros/perl/ch-1.pl
@@ -0,0 +1,45 @@
+#!/usr/bin/env perl
+#
+=head1 Task 1: Sorted Squares
+
+Submitted by: Mohammad S Anwar
+
+You are given a list of numbers.
+
+Write a script to square each number in the list and return the sorted list,
+increasing order.
+
+=head2 Example 1
+
+ Input: @list = (-2, -1, 0, 3, 4)
+ Output: (0, 1, 4, 9, 16)
+
+=head2 Example 2
+
+ Input: @list = (5, -4, -1, 3, 6)
+ Output: (1, 9, 16, 25, 36)
+
+=cut
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+
+my $cases = [
+ [[-2, -1, 0, 3, 4], [0, 1, 4, 9, 16], 'Example 1'],
+ [[ 5, -4, -1, 3, 6], [1, 9, 16, 25, 36], 'Example 2'],
+];
+
+sub sorted_squares
+{
+ my $l = shift;
+ return [sort { $a <=> $b } map { $_ ** 2 } @$l];
+}
+
+for (@$cases) {
+ is(sorted_squares($_->[0]), $_->[1], $_->[2]);
+}
+done_testing();
+
+exit 0;
diff --git a/challenge-219/peter-meszaros/perl/ch-2.pl b/challenge-219/peter-meszaros/perl/ch-2.pl
new file mode 100755
index 0000000000..dcf566890f
--- /dev/null
+++ b/challenge-219/peter-meszaros/perl/ch-2.pl
@@ -0,0 +1,98 @@
+#!/usr/bin/env perl
+#
+=head1 Task 2: Travel Expenditure
+
+Submitted by: Mohammad S Anwar
+
+You are given two list, @costs and @days.
+
+The list @costs contains the cost of three different types of travel cards you
+can buy.
+
+For example @costs = (5, 30, 90)
+
+ Index 0 element represent the cost of 1 day travel card.
+ Index 1 element represent the cost of 7 days travel card.
+ Index 2 element represent the cost of 30 days travel card.
+
+The list @days contains the day number you want to travel in the year.
+
+For example: @days = (1, 3, 4, 5, 6)
+
+The above example means you want to travel on day 1, day 3, day 4, day 5 and
+day 6 of the year.
+
+Write a script to find the minimum travel cost.
+
+=head2 Example 1:
+
+ Input: @costs = (2, 7, 25)
+ @days = (1, 5, 6, 7, 9, 15)
+ Output: 11
+
+ On day 1, we buy a one day pass for 2 which would cover the day 1.
+ On day 5, we buy seven days pass for 7 which would cover days 5 - 9.
+ On day 15, we buy a one day pass for 2 which would cover the day 15.
+
+ So the total cost is 2 + 7 + 2 => 11.
+
+=head2 Example 2:
+
+ Input: @costs = (2, 7, 25)
+ @days = (1, 2, 3, 5, 7, 10, 11, 12, 14, 20, 30, 31)
+ Output: 20
+
+ On day 1, we buy a seven days pass for 7 which would cover days 1 - 7.
+ On day 10, we buy a seven days pass for 7 which would cover days 10 - 14.
+ On day 20, we buy a one day pass for 2 which would cover day 20.
+ On day 30, we buy a one day pass for 2 which would cover day 30.
+ On day 31, we buy a one day pass for 2 which would cover day 31.
+
+ So the total cost is 7 + 7 + 2 + 2 + 2 => 20.
+=cut
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+use List::Util qw/min/;;
+
+my $cases = [
+ [[[2, 7, 25], [1, 5, 6, 7, 9, 15]], 11, 'Example 1'],
+ [[[2, 7, 25], [1, 2, 3, 5, 7, 10, 11, 12, 14, 20, 30, 31]], 20, 'Example 2'],
+ [[[3, 8, 20], [2, 4, 6, 7, 8, 10, 17]], 14, 'Example 3'],
+ [[[3, 108, 20], [2, 4, 6, 7, 8, 10, 15]], 20, 'Example 4'],
+];
+
+# Based on https://www.geeksforgeeks.org/minimum-cost-to-complete-given-tasks-if-cost-of-1-7-and-30-days-are-given/
+sub travel_expenditure
+{
+ my $costs = $_[0]->[0];
+ my $days = $_[0]->[1];
+
+ my @cards = (1, 7, 30);
+ my (@qmonth, @qweek);
+ my $ans = 0;
+
+ for my $day (@$days) {
+ while (@qmonth && ($qmonth[0]->[0] + 30) <= $day) {
+ shift @qmonth;
+ }
+ while (@qweek && ($qweek[0]->[0] + 7) <= $day) {
+ shift @qweek;
+ }
+ push @qmonth, [$day, $ans + $costs->[2]];
+ push @qweek, [$day, $ans + $costs->[1]];
+ $ans = min($ans + $costs->[0],
+ $qmonth[0]->[1],
+ $qweek[0]->[1]);
+ }
+ return $ans;
+}
+
+for (@$cases) {
+ is(travel_expenditure($_->[0]), $_->[1], $_->[2]);
+}
+done_testing();
+
+exit 0;