From dfa08da69b588157ee30f7ad572646552a1b65b8 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Wed, 28 Aug 2024 10:45:44 +0100 Subject: Add Perl solution to challenge 267 --- challenge-267/paulo-custodio/Makefile | 2 ++ challenge-267/paulo-custodio/perl/ch-1.pl | 38 +++++++++++++++++++++ challenge-267/paulo-custodio/perl/ch-2.pl | 53 ++++++++++++++++++++++++++++++ challenge-267/paulo-custodio/t/test-1.yaml | 15 +++++++++ challenge-267/paulo-custodio/t/test-2.yaml | 10 ++++++ 5 files changed, 118 insertions(+) create mode 100644 challenge-267/paulo-custodio/Makefile create mode 100644 challenge-267/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-267/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-267/paulo-custodio/t/test-1.yaml create mode 100644 challenge-267/paulo-custodio/t/test-2.yaml diff --git a/challenge-267/paulo-custodio/Makefile b/challenge-267/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-267/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-267/paulo-custodio/perl/ch-1.pl b/challenge-267/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..f09a734174 --- /dev/null +++ b/challenge-267/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,38 @@ +#!/usr/bin/env perl + +# Challenge 267 +# +# Task 1: Product Sign +# Submitted by: Mohammad Sajid Anwar +# +# 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. +# 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 +# +# Example 2 +# +# Input: @ints = (1, 2, 0, -2, -1) +# Output: 0 +# +# The product 1 x 2 x 0 x -2 x -1 => 0 +# +# Example 3 +# +# Input: @ints = (-1, -1, 1, -1, 2) +# Output: -1 +# +# The product -1 x -1 x 1 x -1 x 2 => -2 < 0 + +use Modern::Perl; +use List::Util 'product'; + +my $prod = product(@ARGV); +say $prod>0 ? 1 : $prod==0 ? 0 : -1; diff --git a/challenge-267/paulo-custodio/perl/ch-2.pl b/challenge-267/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..a52703f85b --- /dev/null +++ b/challenge-267/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,53 @@ +#!/usr/bin/env perl + +# Challenge 267 +# +# Task 2: Line Counts +# Submitted by: Mohammad Sajid Anwar +# +# 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. +# 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) +# +# 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) + +use Modern::Perl; + +my $LINE_WIDTH = 100; + +my($str, @widths) = @ARGV; +say join " ", line_counts($str, @widths); + +sub line_counts { + my($str, @widths) = @_; + my $lines = 1; + my $col = 0; + for my $ch (split //, $str) { + my $width = $widths[ord($ch)-ord('a')]; + if ($col + $width > $LINE_WIDTH) { + $lines++; + $col = 0; + } + $col += $width; + } + return ($lines, $col); +} diff --git a/challenge-267/paulo-custodio/t/test-1.yaml b/challenge-267/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..f0c83c5c88 --- /dev/null +++ b/challenge-267/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: -1 -2 -3 -4 3 2 1 + input: + output: 1 +- setup: + cleanup: + args: 1 2 0 -2 -1 + input: + output: 0 +- setup: + cleanup: + args: -1 -1 1 -1 2 + input: + output: -1 diff --git a/challenge-267/paulo-custodio/t/test-2.yaml b/challenge-267/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..b01fa84eea --- /dev/null +++ b/challenge-267/paulo-custodio/t/test-2.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 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 + input: + output: 3 60 +- setup: + cleanup: + args: 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 + input: + output: 2 4 -- cgit