aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorwanderdoc <wanderdoc@users.noreply.github.com>2025-06-15 18:22:16 +0200
committerGitHub <noreply@github.com>2025-06-15 18:22:16 +0200
commit3016d31ffc7c86802424ee471157a68d2e0e262d (patch)
treea075b8ebfe4ac6200e9916d8b55ad7ced5f5121d
parent524816db225e920442b51674f7c622314892ddc0 (diff)
downloadperlweeklychallenge-club-3016d31ffc7c86802424ee471157a68d2e0e262d.tar.gz
perlweeklychallenge-club-3016d31ffc7c86802424ee471157a68d2e0e262d.tar.bz2
perlweeklychallenge-club-3016d31ffc7c86802424ee471157a68d2e0e262d.zip
Create ch-1.pl
-rw-r--r--challenge-324/wanderdoc/perl/ch-1.pl45
1 files changed, 45 insertions, 0 deletions
diff --git a/challenge-324/wanderdoc/perl/ch-1.pl b/challenge-324/wanderdoc/perl/ch-1.pl
new file mode 100644
index 0000000000..811938bf32
--- /dev/null
+++ b/challenge-324/wanderdoc/perl/ch-1.pl
@@ -0,0 +1,45 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+You are given an array of integers and two integers $r amd $c.
+Write a script to create two dimension array having $r rows and $c columns using the given array.
+
+Example 1
+
+Input: @ints = (1, 2, 3, 4), $r = 2, $c = 2
+Output: ([1, 2], [3, 4])
+
+
+Example 2
+
+Input: @ints = (1, 2, 3), $r = 1, $c = 3
+Output: ([1, 2, 3])
+
+
+Example 3
+
+Input: @ints = (1, 2, 3, 4), $r = 4, $c = 1
+Output: ([1], [2], [3], [4])
+
+=cut
+
+use Test2::V0 -no_srand => 1;
+
+is(my_natatime([1, 2, 3, 4], 2, 2), [[1, 2], [3, 4]], 'Example 1');
+is(my_natatime([1, 2, 3], 1, 3), [[1, 2, 3]], 'Example 2');
+is(my_natatime([1, 2, 3, 4], 4, 1), [[1], [2], [3], [4]], 'Example 3');
+done_testing();
+
+sub my_natatime
+{
+ my ($aref, $r, $c) = @_;
+ die "Rows and columns mismatch!" if ( $r * $c != @$aref );
+ my @output;
+ while ( my @chunk = splice(@$aref, 0, $c) )
+ {
+ push @output, [@chunk];
+ }
+ return \@output;
+}