diff options
| author | E. Choroba <choroba@matfyz.cz> | 2024-04-29 10:11:56 +0200 |
|---|---|---|
| committer | E. Choroba <choroba@matfyz.cz> | 2024-04-29 10:11:56 +0200 |
| commit | e71828c1ca0388c3f09d0e820863aed1e806acc0 (patch) | |
| tree | c73c87b4a8a270d24e2e5f35da4e8ce732c6dd63 | |
| parent | 8af4e17dc115ce2e7a19f8fa11e70c799d2f6fb9 (diff) | |
| download | perlweeklychallenge-club-e71828c1ca0388c3f09d0e820863aed1e806acc0.tar.gz perlweeklychallenge-club-e71828c1ca0388c3f09d0e820863aed1e806acc0.tar.bz2 perlweeklychallenge-club-e71828c1ca0388c3f09d0e820863aed1e806acc0.zip | |
Add solutions to 267: Product Sign & Line Counts by E. Choroba
| -rwxr-xr-x | challenge-267/e-choroba/perl/ch-1.pl | 17 | ||||
| -rwxr-xr-x | challenge-267/e-choroba/perl/ch-2.pl | 33 |
2 files changed, 50 insertions, 0 deletions
diff --git a/challenge-267/e-choroba/perl/ch-1.pl b/challenge-267/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..aaf8c0266f --- /dev/null +++ b/challenge-267/e-choroba/perl/ch-1.pl @@ -0,0 +1,17 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub product_sign(@ints) { + my %count; + ++$count{ $_ <=> 0 } for @ints; + return 0 if $count{0}; + return $count{-1} % 2 ? -1 : 1 +} + +use Test::More tests => 3; + +is product_sign(-1, -2, -3, -4, 3, 2, 1), 1, 'Example 1'; +is product_sign(1, 2, 0, -2, -1), 0, 'Example 2'; +is product_sign(-1, -1, 1, -1, 2), -1, 'Example 3'; diff --git a/challenge-267/e-choroba/perl/ch-2.pl b/challenge-267/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..1e85e56fcb --- /dev/null +++ b/challenge-267/e-choroba/perl/ch-2.pl @@ -0,0 +1,33 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +use constant WIDTH => 100; + +sub line_counts($str, @widths) { + my %width; + @width{'a' .. 'z'} = @widths; + my $line_tally = 1; + my $line_width = 0; + + for my $char (split //, $str) { + ++$line_tally, $line_width = 0 + if $line_width + $width{$char} > WIDTH; + $line_width += $width{$char}; + } + return $line_tally, $line_width +} + +use Test2::V0; +plan(2); + +is [line_counts('abcdefghijklmnopqrstuvwxyz', + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10)], + [3, 60], 'Example 1'; + +is [line_counts('bbbcccdddaaa', + 4, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10)], + [2, 4], 'Example 2'; |
