diff options
| author | Peter Campbell Smith <pj.campbell.smith@gmail.com> | 2022-09-05 19:21:12 +0100 |
|---|---|---|
| committer | Peter Campbell Smith <pj.campbell.smith@gmail.com> | 2022-09-05 19:21:12 +0100 |
| commit | 2f9fc3fbc7acf4f5784ac929986b3193e9de6905 (patch) | |
| tree | 0c7993b69a550018e5fd33407cba13855f9f6bd8 | |
| parent | b6d0223da5058ce5c38d1556f9b3457e5b020739 (diff) | |
| download | perlweeklychallenge-club-2f9fc3fbc7acf4f5784ac929986b3193e9de6905.tar.gz perlweeklychallenge-club-2f9fc3fbc7acf4f5784ac929986b3193e9de6905.tar.bz2 perlweeklychallenge-club-2f9fc3fbc7acf4f5784ac929986b3193e9de6905.zip | |
Challenge 181 submission
| -rw-r--r-- | challenge-181/peter-campbell-smith/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-181/peter-campbell-smith/perl/ch-1.pl | 49 | ||||
| -rwxr-xr-x | challenge-181/peter-campbell-smith/perl/ch-2.pl | 39 |
3 files changed, 89 insertions, 0 deletions
diff --git a/challenge-181/peter-campbell-smith/blog.txt b/challenge-181/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..83fdce249f --- /dev/null +++ b/challenge-181/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +https://pjcs-pwc.blogspot.com/2022/09/alphabetical-and-days-hotter-sentences.html diff --git a/challenge-181/peter-campbell-smith/perl/ch-1.pl b/challenge-181/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..5513d3aa9c --- /dev/null +++ b/challenge-181/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,49 @@ +#!/usr/bin/perl + +# Peter Campbell Smith - 2022-09-05 +# PWC 181 task 1 + +use v5.28; +use utf8; +use warnings; + +# You are given a paragraph. Write a script to order each sentence alphanumerically +# and print the whole paragraph. + +# Blog: https://pjcs-pwc.blogspot.com/2022/09/alphabetical-and-days-hotter-sentences.html + +my ($input, $sentence, @words, $line_length, $word, $paragraph); + +$input = q[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.]; + +# loop over sentences +$line_length = 0; +while ($input =~ m|(.+?)\.|gs) { + $sentence = $1; + + # extract words in next sentence and sort them + @words = (); + while ($sentence =~ m|([a-z']+)|gi) { + push(@words, $1); + } + @words = sort {lc($a) cmp lc($b)} @words; + + # output the words with line breaks to keep lines under 60 characters + for $word (@words) { + if ($line_length + length($word) > 59) { + $paragraph .= qq[\n]; + $line_length = 0; + } + $paragraph .= $word . ' '; + $line_length += length($word) + 1; + } + $paragraph =~ s| $|. |; +} + +# and print the result +say $paragraph; +
\ No newline at end of file diff --git a/challenge-181/peter-campbell-smith/perl/ch-2.pl b/challenge-181/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..be3d961c68 --- /dev/null +++ b/challenge-181/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,39 @@ +#!/usr/bin/perl + +# Peter Campbell Smith - 2022-09-05 +# PWC 181 task 2 + +use v5.28; +use utf8; +use warnings; + +# You are given file with daily temperature record in random order. +# Write a script to find out days hotter than previous day. + +# Blog: https://pjcs-pwc.blogspot.com/2022/09/alphabetical-and-days-hotter-sentences.html + +my ($line, %input, $day, $yesterday); + +# read the data into %input such that $input{date} = temp +while ($line = <DATA>) { + $input{$1} = $2 if $line =~ m|(.+), (\d+)|; +} + +# check each day against the previous one +$yesterday = 99; +for $day (sort keys %input) { + say qq[$day was hotter ($input{$day}) than the previous day ($yesterday)] if $input{$day} > $yesterday; + $yesterday = $input{$day}; +} + +__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 |
