aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordcw <d.white@imperial.ac.uk>2019-08-04 18:43:44 +0100
committerdcw <d.white@imperial.ac.uk>2019-08-04 18:43:44 +0100
commiteaecd99da6f2c3f66ec3d3e476761e459fbc8356 (patch)
tree7ea5eca161489c7b11f1c256c84021a5cdf9a19f
parent2a666cfcaf61b6708bfcf9df02c31b4e104a4205 (diff)
downloadperlweeklychallenge-club-eaecd99da6f2c3f66ec3d3e476761e459fbc8356.tar.gz
perlweeklychallenge-club-eaecd99da6f2c3f66ec3d3e476761e459fbc8356.tar.bz2
perlweeklychallenge-club-eaecd99da6f2c3f66ec3d3e476761e459fbc8356.zip
added my solutions to challenge 19
-rw-r--r--challenge-019/duncan-c-white/README35
-rwxr-xr-xchallenge-019/duncan-c-white/perl5/ch-1.pl53
-rwxr-xr-xchallenge-019/duncan-c-white/perl5/ch-2.pl37
3 files changed, 98 insertions, 27 deletions
diff --git a/challenge-019/duncan-c-white/README b/challenge-019/duncan-c-white/README
index 0220538063..d120449c9b 100644
--- a/challenge-019/duncan-c-white/README
+++ b/challenge-019/duncan-c-white/README
@@ -1,33 +1,14 @@
-Challenge 1: "Write a script that takes 2 or more strings as command
-line parameters and print the longest common substring. For example,
-the longest common substring of the strings "ABABC", "ABCA"
-and "BCBA" is string "BC" of length 3.
-"
+Challenge 1: "Write a script to display months from the year 1900 to 2019 where you find 5 weekends i.e. 5 Friday, 5 Saturday and 5 Sunday."
My notes:
-Very clearly described. No obvious clever way of solving this, but the
-basic method is simple.
+Clearly defined, although Friday isn't normally considered part of the weekend
+is it? Anyway, it's clear here that Friday - Sunday is the weekend for the
+purposes of this question. Date::Manip can do this easily enough.
-Challenge 2: "Write a script to implement Priority Queue. It is like
-regular queue except each element has a priority associated with it. In a
-priority queue, an element with high priority is served before an element
-with low priority. It should serve the following operations:
+Challenge 2: "Write a script that can wrap the given paragraph at a specified
+column using the greedy algorithm."
-1) is_empty: check whether the queue has no elements.
-
-2) insert_with_priority: add an element to the queue with an associated
- priority.
-
-3) pull_highest_priority_element: remove the element from the queue that
- has the highest priority, and return it. If two elements have the same
- priority, then return element added first.
-
-"
-
-My notes: At last, a nicely specified problem to implement a nice data
-type. meat and drink to me! I've split out the Priority Queue
-implementation into the module PQ.pm, minimally-OO in order to
-get free magic PQ printing via interpolation and the magic of
-stringification.
+My notes: Another clearly described problem. Greedy algorithm wiki page
+just neans: fit words on the line while they fit:-)
diff --git a/challenge-019/duncan-c-white/perl5/ch-1.pl b/challenge-019/duncan-c-white/perl5/ch-1.pl
new file mode 100755
index 0000000000..e1c833054b
--- /dev/null
+++ b/challenge-019/duncan-c-white/perl5/ch-1.pl
@@ -0,0 +1,53 @@
+#!/usr/bin/perl
+#
+# Challenge 1: "Write a script to display months from the year 1900 to 2019
+# where you find 5 weekends i.e. 5 Friday, 5 Saturday and 5 Sunday."
+#
+# My notes:
+#
+# Clearly defined, although Friday isn't normally considered part of the
+# weekend is it? Anyway, it's clear here that Friday - Sunday is the weekend
+# for the purposes of this question. Date::Manip can do this easily enough.
+#
+
+use strict;
+use warnings;
+use Function::Parameters;
+use List::Util qw(min);
+use Date::Manip;
+use Data::Dumper;
+
+die "Usage: ch-1.pl [STARTYEAR [ENDYEAR]]\n" if @ARGV>2;
+my $startyear = shift // 1900;
+my $endyear = shift // 2019;
+
+#
+# my $n = threedayweekends( $year, $month );
+# Find how many three day weekends (Friday, Saturday and Monday)
+# there are in month $month (1..12) in year $year.
+#
+fun threedayweekends( $year, $month )
+{
+ my $nfri = 0;
+ my $nsat = 0;
+ my $nsun = 0;
+ my $ndays = Date_DaysInMonth( $month,$year );
+ foreach my $dayno (1..$ndays)
+ {
+ my $day = Date_DayOfWeek( $month, $dayno, $year );
+ $nfri++ if $day == 5;
+ $nsat++ if $day == 6;
+ $nsun++ if $day == 7;
+ }
+ return min( $nfri, $nsat, $nsun );
+}
+
+
+foreach my $y ($startyear..$endyear)
+{
+ foreach my $m (1..12)
+ {
+ my $n = threedayweekends( $y, $m );
+ printf "%02d/$y $n three day weekends\n", $m if $n==5;
+ }
+}
diff --git a/challenge-019/duncan-c-white/perl5/ch-2.pl b/challenge-019/duncan-c-white/perl5/ch-2.pl
new file mode 100755
index 0000000000..cbf1672f0e
--- /dev/null
+++ b/challenge-019/duncan-c-white/perl5/ch-2.pl
@@ -0,0 +1,37 @@
+#!/usr/bin/perl
+#
+# Challenge 2: "Write a script that can wrap the given paragraph at a specified
+# column using the greedy algorithm."
+#
+# My notes: Another clearly described problem. Greedy algorithm wiki page
+# just neans: fit words on the line while they fit:-)
+#
+
+use strict;
+use warnings;
+use Function::Parameters;
+use Data::Dumper;
+
+die "Usage: ch-2.pl width sentence\n" if @ARGV<2;
+my $linelen = shift;
+my $sentence = join( ' ', @ARGV );
+
+my @words = split(/\s+/,$sentence);
+my $line = shift @words;
+my $linew = length($line);
+
+foreach my $word (@words)
+{
+ my $len = length($word);
+ if( $linew + $len + 1 < $linelen )
+ {
+ $line .= " $word";
+ $linew += $len+1;
+ } else
+ {
+ print "$line\n";
+ $line = $word;
+ $linew = $len;
+ }
+}
+print "$line\n" if $line;