aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Köhler <jean-luc@picard.franken.de>2023-05-29 19:08:37 +0200
committerThomas Köhler <jean-luc@picard.franken.de>2023-05-29 19:08:37 +0200
commit68cbe769eb6b7de621bbdeaa00f78fbc2114f175 (patch)
tree49c4f8919ad862110159968027861981808807d7
parent979144e452a65703e7845a166d7c94ba6f89e37f (diff)
downloadperlweeklychallenge-club-68cbe769eb6b7de621bbdeaa00f78fbc2114f175.tar.gz
perlweeklychallenge-club-68cbe769eb6b7de621bbdeaa00f78fbc2114f175.tar.bz2
perlweeklychallenge-club-68cbe769eb6b7de621bbdeaa00f78fbc2114f175.zip
Add solution 219
Signed-off-by: Thomas Köhler <jean-luc@picard.franken.de>
-rw-r--r--challenge-219/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-219/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-219/jeanluc2020/perl/ch-1.pl39
-rwxr-xr-xchallenge-219/jeanluc2020/perl/ch-2.pl100
4 files changed, 141 insertions, 0 deletions
diff --git a/challenge-219/jeanluc2020/blog-1.txt b/challenge-219/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..49272dae95
--- /dev/null
+++ b/challenge-219/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-219-1.html
diff --git a/challenge-219/jeanluc2020/blog-2.txt b/challenge-219/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..c047a2bc98
--- /dev/null
+++ b/challenge-219/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-219-2.html
diff --git a/challenge-219/jeanluc2020/perl/ch-1.pl b/challenge-219/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..a6f69f5600
--- /dev/null
+++ b/challenge-219/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,39 @@
+#!/usr/bin/perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-219/#TASK1
+#
+# Task 1: Sorted Squares
+# ======================
+#
+# You are given a list of numbers.
+#
+# Write a script to square each number in the list and return the sorted list, increasing order.
+#
+## Example 1
+##
+## Input: @list = (-2, -1, 0, 3, 4)
+## Output: (0, 1, 4, 9, 16)
+#
+## Example 2
+##
+## Input: @list = (5, -4, -1, 3, 6)
+## Output: (1, 9, 16, 25, 36)
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Just square everything, then sort.
+
+use strict;
+use warnings;
+
+sorted_squares(-2, -1, 0, 3, 4);
+sorted_squares(5, -4, -1, 3, 6);
+
+sub sorted_squares {
+ my @list = @_;
+ print "Input: (" . join(", ", @list) . ")\n";
+ print "Output: (" . join(", ", sort {$a<=>$b} map {$_*$_} @list) . ")\n";
+}
diff --git a/challenge-219/jeanluc2020/perl/ch-2.pl b/challenge-219/jeanluc2020/perl/ch-2.pl
new file mode 100755
index 0000000000..33afa19f84
--- /dev/null
+++ b/challenge-219/jeanluc2020/perl/ch-2.pl
@@ -0,0 +1,100 @@
+#!/usr/bin/perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-219/#TASK2
+#
+# Task 2: Travel Expenditure
+# ==========================
+#
+# 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.
+#
+# 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.
+#
+# 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.
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# First, we transform the @costs array into a hash that uses
+# the number of days as keys and the costs for those number
+# of days as the corresponding values. Then we call a recursive
+# function that tries to buy a x-day travel card for each
+# possible value of x, keep all not-covered days in a temporary
+# array and call itself recursively with the remaining days.
+# In the end, we keep the minimum possible value.
+
+use strict;
+use warnings;
+
+travel_expenditure( [2, 7, 25], [1, 5, 6, 7, 9, 15] );
+travel_expenditure( [2, 7, 25], [1, 2, 3, 5, 7, 10, 11, 12, 14, 20, 30, 31] );
+
+sub travel_expenditure {
+ my ($costs, $days) = @_;
+ print "Input: \@costs = (" . join(", ", @$costs) . ")\n";
+ print " \@days = (" . join(", ", @$days) . ")\n";
+ my $tc = {
+ 1 => $costs->[0],
+ 7 => $costs->[1],
+ 30 => $costs->[2],
+ };
+ print "Output: " . calculate( $tc, $days) . "\n";
+}
+
+sub calculate {
+ my ($tc, $days) = @_;
+ return 0 unless @$days;
+ my $first = $days->[0];
+ my $minimum;
+ foreach my $tc_days ( keys %$tc ) {
+ my $last = $first + $tc_days;
+ my $cost = $tc->{$tc_days};
+ my @tmp;
+ foreach my $day (@$days) {
+ push @tmp, $day if $day >= $last;
+ }
+ $cost += calculate($tc, \@tmp);
+ $minimum //= $cost;
+ $minimum = $cost if $cost < $minimum;
+ }
+ return $minimum;
+}