diff options
| author | boblied <boblied@gmail.com> | 2022-09-22 09:04:44 -0500 |
|---|---|---|
| committer | boblied <boblied@gmail.com> | 2022-09-22 09:04:44 -0500 |
| commit | 4043cebbac94e6cf8775202ec59c31d6cac6a1cd (patch) | |
| tree | e32476b6a40f16045e693460cb7441cc4c00f581 | |
| parent | cb7d3f39bdb91b0c6cfa739e5b6a64f4c263eea9 (diff) | |
| download | perlweeklychallenge-club-4043cebbac94e6cf8775202ec59c31d6cac6a1cd.tar.gz perlweeklychallenge-club-4043cebbac94e6cf8775202ec59c31d6cac6a1cd.tar.bz2 perlweeklychallenge-club-4043cebbac94e6cf8775202ec59c31d6cac6a1cd.zip | |
Week 181 challenge 1 solution
| -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/input-1.txt | 5 |
3 files changed, 62 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/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. |
