From df3a980941bef0286bc04ce15fbbbf9bada9a8e6 Mon Sep 17 00:00:00 2001 From: pme Date: Mon, 29 Apr 2024 18:04:58 +0200 Subject: challenge-267 --- challenge-267/peter-meszaros/perl/ch-1.pl | 64 +++++++++++++++++++++++++ challenge-267/peter-meszaros/perl/ch-2.pl | 80 +++++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100755 challenge-267/peter-meszaros/perl/ch-1.pl create mode 100755 challenge-267/peter-meszaros/perl/ch-2.pl diff --git a/challenge-267/peter-meszaros/perl/ch-1.pl b/challenge-267/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..590cf0f3d8 --- /dev/null +++ b/challenge-267/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,64 @@ +#!/usr/bin/env perl +# +=head1 Task 1: Product Sign + +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. + +=head2 Example 1 + +Input: @ints = (-1, -2, -3, -4, 3, 2, 1) + +Output: 1 + +The product -1 x -2 x -3 x -4 x 3 x 2 x 1 => 144 > 0 + +=head2 Example 2 + +Input: @ints = (1, 2, 0, -2, -1) + +Output: 0 + +The product 1 x 2 x 0 x -2 x -1 => 0 + +=head2 Example 3 + +Input: @ints = (-1, -1, 1, -1, 2) + +Output: -1 + +The product -1 x -1 x 1 x -1 x 2 => -2 < 0 + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [[-1, -2, -3, -4, 3, 2, 1], 1], + [[1, 2, 0, -2, -1], 0], + [[-1, -1, 1, -1, 2], -1], +]; + +sub product_sign +{ + my $l = shift; + + my $res = 1; + + $res *= $_ for @$l; + + return $res <=> 0; +} + +for (@$cases) { + is(product_sign($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-267/peter-meszaros/perl/ch-2.pl b/challenge-267/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..7b42412a18 --- /dev/null +++ b/challenge-267/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,80 @@ +#!/usr/bin/env perl +# +=head1 Task 2: Line Counts + +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. + +=head2 Example 1 + +Input: $str = "abcdefghijklmnopqrstuvwxyz" + + @widths = (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) + +Output: (3, 60) + +Line 1: abcdefghij (100 pixels) + +Line 2: klmnopqrst (100 pixels) + +Line 3: uvwxyz (60 pixels) + +=head2 Example 2 + +Input: $str = "bbbcccdddaaa" + + @widths = (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) + +Output: (2, 4) + +Line 1: bbbcccdddaa (98 pixels) + +Line 2: a (4 pixels) + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [["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]], + [["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]], +]; + +sub line_counts +{ + my $text = $_[0]->[0]; + my $widths = $_[0]->[1]; + + my $length = 100; + my $lines = 0; + my $len = 0; + for my $c (split '', $text) { + my $w = $widths->[ord($c) - ord('a')]; + $len += $w; + if ($len > 100) { + ++$lines; + $len = $w; + } + } + ++$lines if $len > 0; + return [$lines, $len]; +} + +for (@$cases) { + is(line_counts($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; + -- cgit