From b112e3588e197cd3aeebd2991b3cdb9968ed95a3 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 20 Oct 2025 17:26:24 +0000 Subject: w344 - Task 1 & 2 (Perl) --- challenge-344/perlboy1967/perl/ch1.pl | 35 +++++++++++++++++++++++++++++ challenge-344/perlboy1967/perl/ch2.pl | 42 +++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100755 challenge-344/perlboy1967/perl/ch1.pl create mode 100755 challenge-344/perlboy1967/perl/ch2.pl diff --git a/challenge-344/perlboy1967/perl/ch1.pl b/challenge-344/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..de180d81b9 --- /dev/null +++ b/challenge-344/perlboy1967/perl/ch1.pl @@ -0,0 +1,35 @@ +#!/bin/perl + +=pod + +L + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Array Form Compute +Submitted by: Mohammad Sajid Anwar + +You are given an array of integers, @ints and an integer, $x. + +Write a script to add $x to the integer in the array-form. + +|| The array form of an integer is a digit-by-digit representation stored +|| as an array, where the most significant digit is at the 0th index. + +=cut + +use Test2::V0 qw(-no_srand); +use exact 'v5.32', -signatures; + +sub arrayFormCompute ($x,@ints) { + split(//,int(join('',@ints))+$x); +} + +is([arrayFormCompute(12,1,2,3,4)],[1,2,4,6],'Example 1'); +is([arrayFormCompute(181,2,7,4)],[4,5,5],'Example 2'); +is([arrayFormCompute(1,9,9,9)],[1,0,0,0],'Example 3'); +is([arrayFormCompute(9999,1,0,0,0,0)],[1,9,9,9,9],'Example 4'); +is([arrayFormCompute(1000,0)],[1,0,0,0],'Example 5'); +is([arrayFormCompute(123,9,8,7)],[1,1,1,0],'Own example'); + +done_testing; diff --git a/challenge-344/perlboy1967/perl/ch2.pl b/challenge-344/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..200aafddbd --- /dev/null +++ b/challenge-344/perlboy1967/perl/ch2.pl @@ -0,0 +1,42 @@ +#!/bin/perl + +=pod + +L + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Array Formation +Submitted by: Mohammad Sajid Anwar + +You are given two list: @source and @target. + +Write a script to see if you can build the exact @target by putting these +smaller lists from @source together in some order. You cannot break apart +or change the order inside any of the smaller lists in @source. + +=cut + +use Test2::V0 qw(-no_srand); +use exact 'v5.32', -signatures; + +use boolean; +use Algorithm::Combinatorics qw(variations); + +sub arrayFormation ($arSrc,$arDst) { + my @src = map { join(',',@$_) } @$arSrc; + my $dst = join(',',@$arDst); + for my $a (variations(\@src,$#src + 1)) { + return true if (join(',',@$a) eq $dst); + } + return false; +} + +is(arrayFormation([[2,3],[1],[4]],[1,2,3,4]),true,'Example 1'); +is(arrayFormation([[1,3],[2,4]],[1,2,3,4]),false,'Example 2'); +is(arrayFormation([[9,1],[5,8],[2]],[5,8,2,9,1]),true,'Example 3'); +is(arrayFormation([[1],[3]],[1,2,3]),false,'Example 4'); +is(arrayFormation([[7,4,6]],[7,4,6]),true,'Example 5'); +is(arrayFormation([[1,1,1],[0,1],[1],[0,0]],[0,1,1,1,1,1,0,0]),true,'Own example'); + +done_testing; -- cgit