aboutsummaryrefslogtreecommitdiff
path: root/challenge-084
diff options
context:
space:
mode:
authorDieter Dobbelaere <dieter.dobbelaere@gmail.com>2020-10-31 14:22:59 +0100
committerDieter Dobbelaere <dieter.dobbelaere@gmail.com>2020-10-31 17:20:09 +0100
commit6d4032aa1f6f81cddd4661343b2d5ae592b9fed6 (patch)
tree61aaaad0bd87f15ac43c39e11fd3e0b88dd97ce3 /challenge-084
parent795ca661fbce66d3f2e571e89da40d58da58900c (diff)
downloadperlweeklychallenge-club-6d4032aa1f6f81cddd4661343b2d5ae592b9fed6.tar.gz
perlweeklychallenge-club-6d4032aa1f6f81cddd4661343b2d5ae592b9fed6.tar.bz2
perlweeklychallenge-club-6d4032aa1f6f81cddd4661343b2d5ae592b9fed6.zip
Add solutions for challenge 084.
Diffstat (limited to 'challenge-084')
-rw-r--r--challenge-084/ddobbelaere/perl/ch-1.pl62
-rw-r--r--challenge-084/ddobbelaere/perl/ch-2.pl130
2 files changed, 192 insertions, 0 deletions
diff --git a/challenge-084/ddobbelaere/perl/ch-1.pl b/challenge-084/ddobbelaere/perl/ch-1.pl
new file mode 100644
index 0000000000..561e95cb82
--- /dev/null
+++ b/challenge-084/ddobbelaere/perl/ch-1.pl
@@ -0,0 +1,62 @@
+#!/usr/bin/env perl
+
+use v5.20;
+use strict;
+use warnings;
+use feature qw(signatures);
+no warnings qw(experimental::signatures);
+
+=pod
+TASK #1 › Reverse Integer
+Submitted by: Mohammad S Anwar
+
+You are given an integer $N.
+
+Write a script to reverse the given integer and print the result. Print 0 if the result doesn’t fit in 32-bit signed integer.
+
+The number 2,147,483,647 is the maximum positive value for a 32-bit signed binary integer in computing.
+Example 1:
+
+Input: 1234
+Output: 4321
+
+Example 2:
+
+Input: -1234
+Output: -4321
+
+Example 3:
+
+Input: 1231230512
+Output: 0
+=cut
+
+#<<<
+sub reverse_integer($N) {
+ my $M = reverse abs $N, '-'x($N<0);
+ $M*(-2**31<=$M<2**31);
+}
+#>>>
+
+my $int = qr/-?\d+/;
+
+if ( @ARGV && $ARGV[0] =~ /^$int$/ ) {
+ say reverse_integer( $ARGV[0] );
+}
+else {
+ say "Usage: $0 <Integer>";
+}
+
+# Tests.
+while (<DATA>) {
+ if (/^($int)\s+($int)$/) {
+ my $actual = reverse_integer($1);
+ $2 eq $actual || die "Error for $1. Expected $2, got $actual.";
+ }
+}
+
+__DATA__
+# Test data of the form <Input> <Expected Output>
+1234 4321
+-1234 -4321
+1231230512 0
diff --git a/challenge-084/ddobbelaere/perl/ch-2.pl b/challenge-084/ddobbelaere/perl/ch-2.pl
new file mode 100644
index 0000000000..314f3b0e97
--- /dev/null
+++ b/challenge-084/ddobbelaere/perl/ch-2.pl
@@ -0,0 +1,130 @@
+#!/usr/bin/env perl
+
+use v5.20;
+use strict;
+use warnings;
+use feature qw(signatures);
+no warnings qw(experimental::signatures);
+
+use Data::Dumper;
+
+=pod
+TASK #2 › Find Square
+Submitted by: Mohammad S Anwar
+
+You are given matrix of size m x n with only 1 and 0.
+
+Write a script to find the count of squares having all four corners set as 1.
+Example 1:
+
+Input: [ 0 1 0 1 ]
+ [ 0 0 1 0 ]
+ [ 1 1 0 1 ]
+ [ 1 0 0 1 ]
+
+Output: 1
+
+Explanation:
+There is one square (3x3) in the given matrix with four corners as 1 starts at r=1;c=2.
+
+[ 1 0 1 ]
+[ 0 1 0 ]
+[ 1 0 1 ]
+
+Example 2:
+
+Input: [ 1 1 0 1 ]
+ [ 1 1 0 0 ]
+ [ 0 1 1 1 ]
+ [ 1 0 1 1 ]
+
+Output: 4
+
+Explanation:
+There is one square (4x4) in the given matrix with four corners as 1 starts at r=1;c=1.
+There is one square (3x3) in the given matrix with four corners as 1 starts at r=1;c=2.
+There are two squares (2x2) in the given matrix with four corners as 1. First starts at r=1;c=1 and second starts at r=3;c=3.
+Example 3:
+
+Input: [ 0 1 0 1 ]
+ [ 1 0 1 0 ]
+ [ 0 1 0 0 ]
+ [ 1 0 0 1 ]
+
+Output: 0
+=cut
+
+#<<<
+sub count_squares($M) {
+ no warnings;
+ my $r; my $i--;
+ while($$M[++$i].'0'){my $j--;
+ while($$M[$i][++$j].'0'){my $k;
+ while($$M[$i][++$k+$j].'0'){
+ $r+=$$M[$i+$k][$j]&$$M[$i][$j+$k]
+ &$$M[$i][$j]&$$M[$i+$k][$j+$k]}}}
+ $r;
+}
+#>>>
+
+# Tests.
+my $M;
+my $row_cnt;
+
+while (<DATA>) {
+ next if /^\s*$/ || /^#/;
+
+ if (/\[/) {
+
+ # Next matrix row.
+ $M->[ $row_cnt++ ] = [m/\b[01]\b/g];
+ }
+ else {
+ # Expected output.
+ my ($expected) = m/\d+/g;
+
+ $Data::Dumper::Terse = 1;
+ $Data::Dumper::Indent = 0;
+
+ my $actual = count_squares($M);
+ $expected == $actual
+ || die "Error for "
+ . Dumper($M)
+ . ". Expected $expected, got $actual.";
+
+ # Reset variables.
+ undef $M;
+ undef $row_cnt;
+ }
+}
+
+__DATA__
+# Test data.
+[ 0 1 0 1 ]
+[ 0 0 1 0 ]
+[ 1 1 0 1 ]
+[ 1 0 0 1 ]
+1
+
+[ 1 1 0 1 ]
+[ 1 1 0 0 ]
+[ 0 1 1 1 ]
+[ 1 0 1 1 ]
+4
+
+[ 0 1 0 1 ]
+[ 1 0 1 0 ]
+[ 0 1 0 0 ]
+[ 1 0 0 1 ]
+0
+
+[ 0 1 0 1 ]
+[ 1 0 1 0 ]
+[ 0 1 0 1 ]
+1
+
+[ 1 0 1 ]
+[ 1 0 0 ]
+[ 1 1 1 ]
+[ 0 1 1 ]
+2