# RogerBW's Blog

The Weekly Challenge 267: The Line is the Sign That Your Product Counts 05 May 2024

I’ve been doing the Weekly Challenges. The latest involved series multiplication and text fitting. (Note that this ends today.)

You are given an array of `@ints`.

Write a script to find the sign of product of all integers in the given array. The sign is 1 if the product is positive, -1 if the product is negative and 0 if product is zero.

One obvious way to do this would be to calculate the product of the series (e.g. with a `reduce`-like function) and then apply what `BASIC` used to call `SGN`. But it occurred to me that I'm not being asked for the actual product, only its sign, so there's a less computationally intensive way of doing it. Kotlin:

``````fun productsign(a: List<Int>): Int {
var out = 1
for (t in a) {
if (t < 0) {
out = -out
} else if (t == 0) {
out = 0
}
}
return out
}
``````

In PostScript I can get away with no variables at all.

``````/productsign {
1 exch
{
dup 0 lt {
pop neg
} {
0 eq {
pop 0
exit
} if
} ifelse
} forall
} bind def
``````

You are given a string, `\$str`, and a 26-items array `@widths` containing the width of each character from a to z.

Write a script to find out the number of lines and the width of the last line needed to display the given string, assuming you can only fit 100 width units on a line.

Since I don't need to know the widths of the lines except for the last, I don't bother to record them; just track the current width and the total number of lines. Raku:

``````sub linecounts(\$a, @w) {
my \$linecount = 1;
my \$linewidth = 0;
my \$asize = ord('a');
for \$a.comb -> \$c {
my \$wd = @w[ord(\$c) - \$asize];
if (\$linewidth + \$wd > 100) {
\$linecount++;
\$linewidth = \$wd;
} else {
\$linewidth += \$wd;
}
}
return [\$linecount, \$linewidth];
}
``````

and all the other languages work essentially the same way.

Full code on github.