diff options
| author | Ruben Westerberg <drclaw@mac.com> | 2019-08-02 13:28:34 +1000 |
|---|---|---|
| committer | Ruben Westerberg <drclaw@mac.com> | 2019-08-02 13:28:34 +1000 |
| commit | 699f1cbc9d4710ff680f1844987f16853f5693ca (patch) | |
| tree | 89af49409741f338ab53a388b17178b4c3e6ee43 /challenge-019 | |
| parent | b158fcdb8d745e91499c4e1f1e3b320e1974c508 (diff) | |
| download | perlweeklychallenge-club-699f1cbc9d4710ff680f1844987f16853f5693ca.tar.gz perlweeklychallenge-club-699f1cbc9d4710ff680f1844987f16853f5693ca.tar.bz2 perlweeklychallenge-club-699f1cbc9d4710ff680f1844987f16853f5693ca.zip | |
Finished ch-2 solutions
Updated readme
Diffstat (limited to 'challenge-019')
| -rw-r--r-- | challenge-019/ruben-westerberg/README | 12 | ||||
| -rwxr-xr-x | challenge-019/ruben-westerberg/perl5/ch-2.pl | 40 | ||||
| -rwxr-xr-x | challenge-019/ruben-westerberg/perl6/ch-2.p6 | 9 |
3 files changed, 58 insertions, 3 deletions
diff --git a/challenge-019/ruben-westerberg/README b/challenge-019/ruben-westerberg/README index 60f1fb8f9a..21f3332128 100644 --- a/challenge-019/ruben-westerberg/README +++ b/challenge-019/ruben-westerberg/README @@ -2,8 +2,16 @@ Solution by Ruben Westerberg ch-1.pl and ch-1.p6 === -Run the program with two arguments to find the longest sub string common to both. alpha numeric characters only. +Run the program to find months with 5 weekends ch-2.pl and ch-2.p6 === -Run the program to demonstrate a priority queue inserting, listing and pulling data until empty +Example usage + perl5 version: + ./ch-2.pl -c 20 < input.txt + + perl6 version + ./ch-2.p6 --col==20 < inputs.txt + +If -c or --col are not specifed a default width of 80 is used + diff --git a/challenge-019/ruben-westerberg/perl5/ch-2.pl b/challenge-019/ruben-westerberg/perl5/ch-2.pl new file mode 100755 index 0000000000..e555bc72b8 --- /dev/null +++ b/challenge-019/ruben-westerberg/perl5/ch-2.pl @@ -0,0 +1,40 @@ +#!/usr/bin/env perl +# +use strict; +use warnings; +use Getopt::Long; +use v5.26; + +my @lines=<STDIN>; + +my @words; + +for (@lines) { + for ( split " ", $_, ) { + push @words, $_, " "; + } +} +my $col=80; +GetOptions("col=i" => \$col ); + + +while (@words) { + state $rem = $col; + my $w =$words[0]; + if (length $w > $col) { + print $w,"\n"; + shift @words; + shift @words; + next; + } + if (length $w <= $rem) { + print $w; + $rem-=length $w; + shift @words; + } + else { + print "\n"; + shift @words if $words[0] eq " "; + $rem=$col; + } +} diff --git a/challenge-019/ruben-westerberg/perl6/ch-2.p6 b/challenge-019/ruben-westerberg/perl6/ch-2.p6 index c24ead4e88..742c736c40 100755 --- a/challenge-019/ruben-westerberg/perl6/ch-2.p6 +++ b/challenge-019/ruben-westerberg/perl6/ch-2.p6 @@ -4,6 +4,13 @@ sub MAIN(:$col=80) { while @words { state $rem=$col; my $w=@words[0]; + + if $w.chars > $col { + print $w,"\n"; + @words.shift; + @words.shift; + next; + } if $w.chars <= $rem { print $w; $rem-=$w.chars; @@ -12,7 +19,7 @@ sub MAIN(:$col=80) { else { print "\n"; - @words.shift if @words[0] eq " "; + @words.shift if @words[0] eq " "; $rem=$col; } } |
