aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-181/duncan-c-white/C/.cbuild4
-rw-r--r--challenge-181/duncan-c-white/README109
-rwxr-xr-xchallenge-181/duncan-c-white/perl/ch-1.pl95
-rwxr-xr-xchallenge-181/duncan-c-white/perl/ch-2.pl69
-rw-r--r--challenge-181/duncan-c-white/perl/input15
-rw-r--r--challenge-181/duncan-c-white/perl/temperature.txt10
6 files changed, 239 insertions, 53 deletions
diff --git a/challenge-181/duncan-c-white/C/.cbuild b/challenge-181/duncan-c-white/C/.cbuild
deleted file mode 100644
index a14ec76520..0000000000
--- a/challenge-181/duncan-c-white/C/.cbuild
+++ /dev/null
@@ -1,4 +0,0 @@
-BUILD = ch-1 ch-2
-CFLAGS = -Wall -g
-#LDFLAGS = -lm
-#CFLAGS = -g
diff --git a/challenge-181/duncan-c-white/README b/challenge-181/duncan-c-white/README
index 3a551d6972..c27d4a4e7f 100644
--- a/challenge-181/duncan-c-white/README
+++ b/challenge-181/duncan-c-white/README
@@ -1,49 +1,60 @@
-Task 1: First Unique Character
-
-You are given a string, $s.
-Write a script to find out the first unique character in the given string
-and print its index (0-based).
-
-Example 1
-
-Input: $s = "Perl Weekly Challenge"
-Output: 0 as 'P' is the first unique character
-
-Example 2
-
-Input: $s = "Long Live Perl"
-Output: 1 as 'o' is the first unique character
-
-MY NOTES: ok, sounds extremely easy, first pass: build a freq hash (a bag),
-second pass: sequence through the string, print index of first char
-with freq 1. There is one case where the behaviour is undefined: what if
-there are no unique characters in the string? I've decided to produce pos -1
-in that case.
-
-GUEST LANGUAGE: As a bonus, I also had a go at translating ch-1.pl
-into C (look in the C directory for that).
-
-
-Task 2: Trim List
-
-You are given list of numbers, @n and an integer $i.
-Write a script to trim the given list where element is less than or
-equal to the given integer.
-
-Example 1
-
-Input: @n = (1,4,2,3,5) and $i = 3
-Output: (4,5)
-
-Example 2
-
-Input: @n = (9,0,6,2,3,8,5) and $i = 4
-Output: (9,6,8,5)
-
-MY NOTES: also looks incredibly easy, basically grep { $_ >= $i }.
-Just to bulk the task up a little, I allowed the input list to be
-spread across multiple arguments, each of which can either store a
-simple +ve int, or a comma-separated list of one or more +ve ints.
-
-GUEST LANGUAGE: As a bonus, I also had a go at translating ch-2.pl
-into C (look in the C directory for that).
+Task 1: Sentence Order
+
+You are given a paragraph.
+
+Write a script to order each sentence alphanumerically and print the
+whole paragraph.
+
+Example
+
+Input:
+ All he could think about was how it would all end. There was
+ still a bit of uncertainty in the equation, but the basics
+ were there for anyone to see. No matter how much he tried to
+ see the positive, it wasn't anywhere to be seen. The end was
+ coming and it wasn't going to be pretty.
+
+Output:
+ about All all could end he how it think was would. a anyone
+ basics bit but equation, for in of see still the the There
+ there to uncertainty was were. anywhere be he how it matter
+ much No positive, see seen the to to tried wasn't. and be
+ coming end going it pretty The to was wasn't.
+
+MY NOTES: hmm, sounds surprisingly tricky, we have to read all the input,
+identify all the sentences (a sentence can span multiple lines), for each
+sentence split off the trailing '. ', sort the words in the sentence in
+case-insensitive order, add the '. ' back again, accumulate all the
+output words in a single array, and then print it out with width formatting.
+Stole width formatting code from challenge 177.
+
+
+Task 2: Hot Day
+
+You are given file with daily temperature record in random order.
+Write a script to find out days hotter than previous day.
+
+Example
+
+Input File: (temperature.txt)
+
+2022-08-01, 20
+2022-08-09, 10
+2022-08-03, 19
+2022-08-06, 24
+2022-08-05, 22
+2022-08-10, 28
+2022-08-07, 20
+2022-08-04, 18
+2022-08-08, 21
+2022-08-02, 25
+
+Output:
+2022-08-02
+2022-08-05
+2022-08-06
+2022-08-08
+2022-08-10
+
+MY NOTES: looks pretty easy (easier than first task). Could parse dates, but
+technically as the dates are all ISO dates, they can be sorted as strings.
diff --git a/challenge-181/duncan-c-white/perl/ch-1.pl b/challenge-181/duncan-c-white/perl/ch-1.pl
new file mode 100755
index 0000000000..83fb6b6a5a
--- /dev/null
+++ b/challenge-181/duncan-c-white/perl/ch-1.pl
@@ -0,0 +1,95 @@
+#!/usr/bin/perl
+#
+# Task 1: Sentence Order
+#
+# You are given a paragraph.
+#
+# Write a script to order each sentence alphanumerically and print the
+# whole paragraph.
+#
+# Example
+#
+# Input:
+# All he could think about was how it would all end. There was
+# still a bit of uncertainty in the equation, but the basics
+# were there for anyone to see. No matter how much he tried to
+# see the positive, it wasn't anywhere to be seen. The end was
+# coming and it wasn't going to be pretty.
+#
+# Output:
+# about All all could end he how it think was would. a anyone
+# basics bit but equation, for in of see still the the There
+# there to uncertainty was were. anywhere be he how it matter
+# much No positive, see seen the to to tried wasn't. and be
+# coming end going it pretty The to was wasn't.
+#
+# MY NOTES: hmm, sounds surprisingly tricky, we have to read all the input,
+# identify all the sentences (a sentence can span multiple lines), for each
+# sentence split off the trailing '. ', sort the words in the sentence in
+# case-insensitive order, add the '. ' back again, accumulate all the
+# output words in a single array, and then print it out with width formatting.
+# Stole width formatting code from challenge 177.
+#
+
+use strict;
+use warnings;
+use feature 'say';
+use Getopt::Long;
+use Data::Dumper;
+
+
+#
+# my $sortsent = sort_sentence($sentence);
+# Sort the words in a given sentence, return the sorted sentence.
+#
+sub sort_sentence
+{
+ my( $sentence ) = @_;
+ my @word = split( /\s+/, $sentence );
+ @word = sort { lc($a) cmp lc($b) } @word;
+ return join( ' ', @word );
+}
+
+
+my $debug=0;
+die "Usage: sentence-order [--debug] < input\n"
+ unless GetOptions( "debug"=>\$debug );
+
+# read all the input into a single long line.
+my @input;
+while( $_ = <> )
+{
+ chomp;
+ s/^\s+//;
+ s/\s+$//;
+ push @input, $_;
+}
+my $input = join( ' ', @input );
+
+# now split into sentences
+my @sentence;
+while( $input =~ s/^([^.]+)\.\s?// )
+{
+ say "S:$1" if $debug;
+ push @sentence, $1;
+}
+if( $input )
+{
+ say "S:$input" if $debug;
+ push @sentence, $input;
+}
+
+my $str = join( ' ', map { sort_sentence($_) } @sentence ) . '.';
+
+# format $str with lines <= maxw chars long.
+my $maxw = 62;
+while( length($str) >= $maxw )
+{
+ my $pos;
+ for( $pos=$maxw; $pos>0 && substr($str,$pos,1) ne ' '; $pos-- )
+ {
+ }
+ say substr($str,0,$pos);
+ $str = substr($str,$pos+1);
+}
+say $str;
diff --git a/challenge-181/duncan-c-white/perl/ch-2.pl b/challenge-181/duncan-c-white/perl/ch-2.pl
new file mode 100755
index 0000000000..425463e0c7
--- /dev/null
+++ b/challenge-181/duncan-c-white/perl/ch-2.pl
@@ -0,0 +1,69 @@
+#!/usr/bin/perl
+#
+# Task 2: Hot Day
+#
+# You are given file with daily temperature record in random order.
+# Write a script to find out days hotter than previous day.
+#
+# Example
+#
+# Input File: (temperature.txt)
+#
+# 2022-08-01, 20
+# 2022-08-09, 10
+# 2022-08-03, 19
+# 2022-08-06, 24
+# 2022-08-05, 22
+# 2022-08-10, 28
+# 2022-08-07, 20
+# 2022-08-04, 18
+# 2022-08-08, 21
+# 2022-08-02, 25
+#
+# Output:
+# 2022-08-02
+# 2022-08-05
+# 2022-08-06
+# 2022-08-08
+# 2022-08-10
+#
+# MY NOTES: looks pretty easy (easier than first task). Could parse dates, but
+# technically as the dates are all ISO dates, they can be sorted as strings.
+#
+# GUEST LANGUAGE: As a bonus, I also had a go at translating ch-2.pl
+# into C (look in the C directory for that).
+#
+
+use strict;
+use warnings;
+use feature 'say';
+use Getopt::Long;
+use Data::Dumper;
+
+my $debug=0;
+die "Usage: hot-days [--debug] < inputfile\n"
+ unless GetOptions( "debug"=>\$debug );
+
+# build @dt, a list of pairs
+my @dt;
+while( <> )
+{
+ chomp;
+ s/\s+//g;
+ my( $date, $temp ) = split( /,/, $_, 2 );
+ #say "date=$date, temp=$temp";
+ push @dt, [ $date, $temp ];
+}
+
+# sort @dt by dates (as strings)
+@dt = sort { $a->[0] cmp $b->[0] } @dt;
+#die Dumper( \@dt );
+
+# find all dates D for which the temperature on date D is hotter than on D-1.
+foreach my $pos (1..$#dt)
+{
+ my $today = $dt[$pos];
+ my $yest = $dt[$pos-1];
+ #say "processing date $today->[0], yest $yest->[0], today temp $today->[1], yest temp $yest->[1]";
+ say $today->[0] if $yest->[1] < $today->[1];
+}
diff --git a/challenge-181/duncan-c-white/perl/input1 b/challenge-181/duncan-c-white/perl/input1
new file mode 100644
index 0000000000..4fe719ed90
--- /dev/null
+++ b/challenge-181/duncan-c-white/perl/input1
@@ -0,0 +1,5 @@
+ All he could think about was how it would all end. There was
+ still a bit of uncertainty in the equation, but the basics
+ were there for anyone to see. No matter how much he tried to
+ see the positive, it wasn't anywhere to be seen. The end was
+coming and it wasn't going to be pretty.
diff --git a/challenge-181/duncan-c-white/perl/temperature.txt b/challenge-181/duncan-c-white/perl/temperature.txt
new file mode 100644
index 0000000000..7b251c9aec
--- /dev/null
+++ b/challenge-181/duncan-c-white/perl/temperature.txt
@@ -0,0 +1,10 @@
+2022-08-01, 20
+2022-08-09, 10
+2022-08-03, 19
+2022-08-06, 24
+2022-08-05, 22
+2022-08-10, 28
+2022-08-07, 20
+2022-08-04, 18
+2022-08-08, 21
+2022-08-02, 25