diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-02-17 19:49:35 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-02-17 19:49:35 +0000 |
| commit | 940883782ca3254659d6e744aaafdd9d30d1a451 (patch) | |
| tree | e52045893de6f98b3020521c591e93a290a17d29 | |
| parent | 2c82176edf3cf13e85f8e47108ac3aae7d8640c7 (diff) | |
| parent | 60b928ba34da9f2a02e98ea273f574edf777e91c (diff) | |
| download | perlweeklychallenge-club-940883782ca3254659d6e744aaafdd9d30d1a451.tar.gz perlweeklychallenge-club-940883782ca3254659d6e744aaafdd9d30d1a451.tar.bz2 perlweeklychallenge-club-940883782ca3254659d6e744aaafdd9d30d1a451.zip | |
Merge pull request #7581 from pjcs00/wk204
Week 204 added
| -rw-r--r-- | challenge-204/peter-campbell-smith/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-204/peter-campbell-smith/perl/ch-1.pl | 43 | ||||
| -rwxr-xr-x | challenge-204/peter-campbell-smith/perl/ch-2.pl | 75 |
3 files changed, 119 insertions, 0 deletions
diff --git a/challenge-204/peter-campbell-smith/blog.txt b/challenge-204/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..92820ee774 --- /dev/null +++ b/challenge-204/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/204 diff --git a/challenge-204/peter-campbell-smith/perl/ch-1.pl b/challenge-204/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..2c9d47c086 --- /dev/null +++ b/challenge-204/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,43 @@ +#!/usr/bin/perl + +# Peter Campbell Smith - 2023-02-14 + +use v5.28; +use utf8; +use warnings; +use Time::HiRes qw(time); + +# Task: 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 monotonically increasing or decreasing: +# Monotonically increasing if for i <= j , nums[i] <= nums[j] +# Monotonically decreasing if for i <= j , nums[i] >= nums[j] + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge/204/1 + +my (@tests, $test, @array, $direction, $next, $result, $j); + +@tests = ([1, 2, 2, 3], [1, 3, 2], [6, 5, 5, 4], [7, 7, 7, 6, 8], [5, 5, 5, 5, 5]); + +for $test (@tests) { + + @array = @$test; + + $result = 1; + $direction = 0; + for $j (0 .. scalar(@array) - 2) { + $next = $array[$j + 1] <=> $array[$j]; + unless ($direction) { + $direction = $next; + } else { + unless ($next == $direction or $next == 0) { + $result = 0; + last; + } + } + } + say qq[\nInput: (] . join (', ', @array) . qq[)]; + + say qq[Output: $result] . ($result == 0 ? '' : + ($direction > 0 ? ' increasing' : ($direction < 0 ? ' decreasing' : ' level'))); +} + diff --git a/challenge-204/peter-campbell-smith/perl/ch-2.pl b/challenge-204/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..4f56b27d05 --- /dev/null +++ b/challenge-204/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,75 @@ +#!/usr/bin/perl + +# Peter Campbell Smith - 2023-02-14 + +use v5.28; +use utf8; +use warnings; +use Time::HiRes qw(time); + +# 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 values in the given matrix. If you can’t reshape print 0. + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge/204/2 + +my (@linear, $width, $x); + +reshape([[1, 2, 3], [4, 5, 6]], 3, 2); +reshape([[1, 2, 3, 4, 5, 6], [11, 12, 13, 14, 15, 16]], 3, 4); +reshape([[1, 2, 3], [4, 5, 6]], 3, 7); +reshape([[1, 12, 123, 1234], [4321, 321, 21, 1]], 4, 2); + +sub reshape { + + my ($m_old, $r_new, $c_new) = @_; + my ($c_old, $r_old, $j, $r, $c, $x); + + # get rows and cols in original array + $r_old = scalar(@$m_old); + $c_old = scalar(@{$m_old->[0]}); + + # collapse old matrix into linear array and check width needed to display + $j = 0; + $width = 0; + for $r (0 .. $r_old - 1) { + for $c (0 .. $c_old - 1) { + $x = $m_old->[$r]->[$c]; + $width = length($x) if length($x) > $width; + $linear[$j ++] = $x; + } + } + $width ++; + + # show old matrix + show("\nInput: ", $r_old, $c_old); + + # check whether transform is possible + if ($r_old * $c_old != $r_new * $c_new) { + say qq[\nOutput: 0\n \$r = $r_new, \$c = $c_new]; + return; + } + + # show new matrix + show("\nOutput: ", $r_new, $c_new); +} + +sub show { + + my ($text, $r, $c) = @_; + my ($j, $row, $col); + + # display matrix + $j = 0; + for $col (0 .. $r - 1) { + for $r (0 .. $c - 1) { + $row .= sprintf("%${width}d ", $linear[$j ++]); + } + say qq[${text}[$row]]; + $row = ''; + $text = ' '; + } + say qq[ \$r = $r, \$c = $c]; +} + + + |
