diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-10-20 18:51:24 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-10-20 18:51:24 +0100 |
| commit | 663a63fa8c79e319d0f5bcef2067d1b83f87d520 (patch) | |
| tree | 87f685cd9fbebacaf6c441722614cf603764b90f | |
| parent | c7cb55e9173ec66d9bd221070d62877e1ed7e1aa (diff) | |
| parent | b112e3588e197cd3aeebd2991b3cdb9968ed95a3 (diff) | |
| download | perlweeklychallenge-club-663a63fa8c79e319d0f5bcef2067d1b83f87d520.tar.gz perlweeklychallenge-club-663a63fa8c79e319d0f5bcef2067d1b83f87d520.tar.bz2 perlweeklychallenge-club-663a63fa8c79e319d0f5bcef2067d1b83f87d520.zip | |
Merge pull request #12887 from PerlBoy1967/branch-for-challenge-344
w344 - Task 1 & 2 (Perl)
| -rwxr-xr-x | challenge-344/perlboy1967/perl/ch1.pl | 35 | ||||
| -rwxr-xr-x | challenge-344/perlboy1967/perl/ch2.pl | 42 |
2 files changed, 77 insertions, 0 deletions
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<https://theweeklychallenge.org/blog/perl-weekly-challenge-344#TASK1> + +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<https://theweeklychallenge.org/blog/perl-weekly-challenge-344#TASK2> + +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; |
