diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-10-16 22:42:15 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-10-16 22:42:15 +0100 |
| commit | e8d9ea862c9f462727c393e91cae7c72528474f5 (patch) | |
| tree | 155cfd6de90c57f35281bf9004b4e12083f3101e /challenge-186 | |
| parent | 275ce7d9c3229ec3032169fb80bbd10beefda6b1 (diff) | |
| parent | e9928e781d6c1ad6a269bb0dad9bf26043638aa2 (diff) | |
| download | perlweeklychallenge-club-e8d9ea862c9f462727c393e91cae7c72528474f5.tar.gz perlweeklychallenge-club-e8d9ea862c9f462727c393e91cae7c72528474f5.tar.bz2 perlweeklychallenge-club-e8d9ea862c9f462727c393e91cae7c72528474f5.zip | |
Merge pull request #6920 from PerlBoy1967/branch-for-challenge-186
w186 - Task 1 only (no time for task 2)
Diffstat (limited to 'challenge-186')
| -rwxr-xr-x | challenge-186/perlboy1967/perl/ch-1.pl | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/challenge-186/perlboy1967/perl/ch-1.pl b/challenge-186/perlboy1967/perl/ch-1.pl new file mode 100755 index 0000000000..afded44b26 --- /dev/null +++ b/challenge-186/perlboy1967/perl/ch-1.pl @@ -0,0 +1,61 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 185 + - https://theweeklychallenge.org/blog/perl-weekly-challenge-185/#TASK2 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Zip List +Submitted by: Mohammad S Anwar + +You are given two list @a and @b of same size. + +Create a subroutine sub zip(@a, @b) that merge the two list as shown in the example below. +: +Example + +Input: @a = qw/1 2 3/; @b = qw/a b c/; +Output: zip(@a, @b) should return qw/1 a 2 b 3 c/; + zip(@b, @a) should return qw/a 1 b 2 c 3/; + +=cut + +# Comment: In stead of creating an own 'zip' subroutine one is +# maybe better off by using: +# +# use List::MoreUtils qw(zip) +# +# As this one is written in C (XS). And maybe performs better in +# large list usage. +# +# However, the challenge was: Create a subroutine. And so I did. +# The two list 'pure Perl' version seems to beet List::MoreUtils :-) +# But with the note: the lists are assumed equal length (no checks) + +use v5.16; +use warnings; + +use Benchmark qw(:all); + +use List::MoreUtils qw(zip); + +sub Zip (\@\@) { + my $i = 0; + map { $_, $_[1][$i++] } @{$_[0]}; +} + +my @a = qw/1 2 3/; +my @b = qw/a b c/; + +say join(',', zip(@a,@b)); +say join(',', Zip(@a,@b)); +say join(',', zip(@b,@a)); +say join(',', Zip(@b,@a)); + + +cmpthese(1_000_000, { + 'List::MoreUtils' => sub { zip(@a,@b); zip(@b,@a) }, + 'PerlBoy' => sub { Zip(@a,@b); Zip(@b,@a) }, +}); |
