-
Notifications
You must be signed in to change notification settings - Fork 311
/
ch-2.pl
executable file
·33 lines (27 loc) · 877 Bytes
/
ch-2.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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';