diff options
| author | vamsimeenavilli <vamsi.meenavilli@gmail.com> | 2023-02-19 12:45:56 +0530 |
|---|---|---|
| committer | vamsimeenavilli <vamsi.meenavilli@gmail.com> | 2023-02-19 12:45:56 +0530 |
| commit | c59c542c03eeb5508ec47e4a361081bdeecd480b (patch) | |
| tree | e8ae4f7a1c5be9ffb53ff7672d2c52f43218f248 /challenge-204 | |
| parent | 1806a0df9eb09b480bb4a31fcd048550f209bdf8 (diff) | |
| download | perlweeklychallenge-club-c59c542c03eeb5508ec47e4a361081bdeecd480b.tar.gz perlweeklychallenge-club-c59c542c03eeb5508ec47e4a361081bdeecd480b.tar.bz2 perlweeklychallenge-club-c59c542c03eeb5508ec47e4a361081bdeecd480b.zip | |
Vamsi's Weekly Challenge 204 perl solutions
Diffstat (limited to 'challenge-204')
| -rw-r--r-- | challenge-204/vamsi-meenavilli/perl/ch-1.pl | 47 | ||||
| -rw-r--r-- | challenge-204/vamsi-meenavilli/perl/ch-2.pl | 48 |
2 files changed, 95 insertions, 0 deletions
diff --git a/challenge-204/vamsi-meenavilli/perl/ch-1.pl b/challenge-204/vamsi-meenavilli/perl/ch-1.pl new file mode 100644 index 0000000000..6906bc4097 --- /dev/null +++ b/challenge-204/vamsi-meenavilli/perl/ch-1.pl @@ -0,0 +1,47 @@ +#!/usr/bin/perl +use strict; +use warnings FATAL => 'all'; + +use Test2::V0; + +=head1 AUTHORS + +Vamsi Meenavilli + +=head1 DESCRIPTION + + Week 204: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-204 + + Task 1: Monotonic Array + Submitted by: Mohammad S Anwar + You are given an array of integers. + + Write a script to find out if the given array is Monotonic. Print 1 if it is otherwise 0. + + An array is Monotonic if it is either monotone increasing or decreasing. + +=cut + + +ok(IsMonotonicArray(1,2,2,3), 'Test Case 1 Failed.'); +ok(!IsMonotonicArray(1,3,2), 'Test Case 2 Failed.'); +ok(IsMonotonicArray(6,5,5,4), 'Test Case 2 Failed.'); + +sub IsMonotonicArray { + my @array = @_; + + my $array_size = scalar(@array); + my ($increement, $decreement) = (1, 1); + + for (my $i = 0; $i < $array_size - 1; $i++) { + $increement = 0 unless $array[$i] >= $array[$i + 1]; + $decreement = 0 unless $array[$i] <= $array[$i + 1]; + return 0 unless $increement || $decreement; + } + + return 1; +} + +done_testing(); diff --git a/challenge-204/vamsi-meenavilli/perl/ch-2.pl b/challenge-204/vamsi-meenavilli/perl/ch-2.pl new file mode 100644 index 0000000000..212433a739 --- /dev/null +++ b/challenge-204/vamsi-meenavilli/perl/ch-2.pl @@ -0,0 +1,48 @@ +#!/usr/bin/perl +use strict; +use warnings FATAL => 'all'; + +use Test::More; + +=head1 AUTHORS + +Vamsi Meenavilli + +=head1 DESCRIPTION + + Week 204: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-204 + + Task 2: Reshape Matrix + Submitted by: Mohammad S Anwar + You are given a matrix (m x n) and two integers (r) and (c). + + Write a script to reshape the given matrix in form (r x c) with the original value in the given matrix. If you can’t reshape print 0. + +=cut + + +is_deeply(ReshapeMatrix([[1, 2], [3,4]], 1, 4), [[1, 2, 3, 4]], 'Test Case 1 Failed.'); +is_deeply(ReshapeMatrix([[1, 2, 3], [4, 5, 6]], 3, 2), [[1, 2], [3, 4], [5, 6]], 'Test Case 2 Failed.'); +is_deeply(ReshapeMatrix([[1, 2]], 3, 2), 0, 'Test Case 2 Failed.'); + +sub ReshapeMatrix { + my ($matrix, $row, $column) = @_; + + my @flatted_matrix = map {@{$_}} @{$matrix}; + + return 0 if $row * $column > scalar(@flatted_matrix); + + my @reshaped_matrix = (); + my $index = 0; + + for (my $i = 0; $i < $row; $i++) { + push @reshaped_matrix, [@flatted_matrix[$index..$index+$column-1]]; + $index += $column; + } + + return \@reshaped_matrix; +} + +done_testing(); |
