diff options
| -rw-r--r-- | challenge-181/bob-lied/README | 4 | ||||
| -rw-r--r-- | challenge-181/bob-lied/perl/ch-1.pl | 55 | ||||
| -rw-r--r-- | challenge-181/bob-lied/perl/ch-2.pl | 99 | ||||
| -rw-r--r-- | challenge-181/bob-lied/perl/input-1.txt | 5 |
4 files changed, 161 insertions, 2 deletions
diff --git a/challenge-181/bob-lied/README b/challenge-181/bob-lied/README index c231e3a589..ff71c2787c 100644 --- a/challenge-181/bob-lied/README +++ b/challenge-181/bob-lied/README @@ -1,3 +1,3 @@ -Solutions to weekly challenge 138 by Bob Lied +Solutions to weekly challenge 181 by Bob Lied -https://perlweeklychallenge.org/blog/perl-weekly-challenge-138/ +https://perlweeklychallenge.org/blog/perl-weekly-challenge-181/ diff --git a/challenge-181/bob-lied/perl/ch-1.pl b/challenge-181/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..6cbbd76e99 --- /dev/null +++ b/challenge-181/bob-lied/perl/ch-1.pl @@ -0,0 +1,55 @@ +#!/bin/env 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. +# +# Ouput: +# 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. + +use v5.30; +use strict; +use warnings; + +use List::Util qw(maxstr); +use Text::Wrap; + +my @inputParagraph = <>; # slurp + +# We want to format the output to the same width as the input +# so find the longest line. +my $maxLineLength = maxstr(map length, @inputParagraph); + +# Split sentences. For simplicity, assume a period (or question mark +# or exclamation point) followed by white space is the end of a sentence. +# This can be fooled by abbrevations, of course, but we aren't going +# into the rabbit hole of parsing English. See Text::Sentence or +# Lingua::EN::Sentence for better. +my @sentenceCollection = split(/[.?!]\s+/, join(" ", @inputParagraph) ); + +my @output; +for my $sentence ( @sentenceCollection ) +{ + # Split words on white space + my @wordCollection = split(/\s+/, $sentence ); + + # End each "sentence" with a period. + push @output, join(" ", sort { lc($a) cmp lc($b) } @wordCollection) . "."; +} + +# Text::Wrap is core perl +$Text::Wrap::columns = $maxLineLength; +say Text::Wrap::wrap('', '', join(" ", @output) ); diff --git a/challenge-181/bob-lied/perl/ch-2.pl b/challenge-181/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..1137770a95 --- /dev/null +++ b/challenge-181/bob-lied/perl/ch-2.pl @@ -0,0 +1,99 @@ +#!/bin/env perl +# 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) Output: +# +# 2022-08-01, 20 2022-08-02 +# 2022-08-09, 10 2022-08-05 +# 2022-08-03, 19 2022-08-06 +# 2022-08-06, 24 2022-08-08 +# 2022-08-05, 22 2022-08-10 +# 2022-08-10, 28 +# 2022-08-07, 20 +# 2022-08-04, 18 +# 2022-08-08, 21 +# 2022-08-02, 25 + +use v5.36; +use strict; +use warnings; + +use Time::Piece; +use Time::Seconds; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +my %tempData; +while (<>) +{ + chomp; + my ($day, $temp) = split /, /; + $tempData{$day} = $temp; +} + + +sub findTempIncrease($tempData) +{ + my @tempHistory; + my @tempIncrease; + my $dayIndex = 0; + for my $day (sort keys %$tempData ) + { + $tempHistory[$dayIndex] = { date => $day, temp => $tempData->{$day} }; + $dayIndex++; + } + + my ($prevDay, $nextDay) = (0, 1); + while ( $nextDay < @tempHistory ) + { + my $prevTemp = $tempHistory[$prevDay]->{temp}; + my $nextTemp = $tempHistory[$nextDay]->{temp}; + + push @tempIncrease, $tempHistory[$nextDay]->{date} if ( $nextTemp > $prevTemp ); + $prevDay++; + $nextDay++; + } + + return \@tempIncrease; +} + + +sub runTest +{ + my @TestCase = ( + { data => { "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 , + }, + result => [ "2022-08-02", + "2022-08-05", + "2022-08-06", + "2022-08-08", + "2022-08-10" + ] + }, + ); + + use Test::More; + + for my $tc ( @TestCase ) + { + is_deeply( findTempIncrease( $tc->{data} ), $tc->{result} ); + } + + done_testing; +} diff --git a/challenge-181/bob-lied/perl/input-1.txt b/challenge-181/bob-lied/perl/input-1.txt new file mode 100644 index 0000000000..af2d76e73b --- /dev/null +++ b/challenge-181/bob-lied/perl/input-1.txt @@ -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. |
