aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2020-12-23 20:30:47 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2020-12-23 20:30:47 +0000
commit2467a0300293088e2b159e01757fc5476fdda0d0 (patch)
treeba00664d80fadd6010bcb99d36f9f01faef3d8da
parent69b8500c975e01c797f74b6b0ccf5431e7f465af (diff)
downloadperlweeklychallenge-club-2467a0300293088e2b159e01757fc5476fdda0d0.tar.gz
perlweeklychallenge-club-2467a0300293088e2b159e01757fc5476fdda0d0.tar.bz2
perlweeklychallenge-club-2467a0300293088e2b159e01757fc5476fdda0d0.zip
Add perl solution to challenge 084
-rw-r--r--challenge-084/paulo-custodio/README1
-rw-r--r--challenge-084/paulo-custodio/perl/ch-1.pl45
-rw-r--r--challenge-084/paulo-custodio/perl/ch-2.pl81
-rw-r--r--challenge-084/paulo-custodio/test.pl59
4 files changed, 186 insertions, 0 deletions
diff --git a/challenge-084/paulo-custodio/README b/challenge-084/paulo-custodio/README
new file mode 100644
index 0000000000..87dc0b2fbd
--- /dev/null
+++ b/challenge-084/paulo-custodio/README
@@ -0,0 +1 @@
+Solution by Paulo Custodio
diff --git a/challenge-084/paulo-custodio/perl/ch-1.pl b/challenge-084/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..4e2a4e7bc2
--- /dev/null
+++ b/challenge-084/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,45 @@
+#!/usr/bin/env perl
+
+# Challenge 084
+#
+# 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
+
+use strict;
+use warnings;
+use 5.030;
+
+my($n) = @ARGV;
+say reverse_int($n);
+
+sub reverse_int {
+ my($n) = @_;
+ return 0 if ($n < -0x80000000 || $n > 0x7fffffff);
+ my $rev = 0;
+ my $sign = ($n < 0) ? -1 : 1;
+ $n = abs($n);
+ while ($n > 0) {
+ $rev = 10 * $rev + ($n % 10);
+ $n = int($n / 10);
+ }
+ $rev *= $sign;
+ return $rev;
+}
+
diff --git a/challenge-084/paulo-custodio/perl/ch-2.pl b/challenge-084/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..bd43abbf6e
--- /dev/null
+++ b/challenge-084/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,81 @@
+#!/usr/bin/env perl
+
+# Challenge 084
+#
+# 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
+
+use strict;
+use warnings;
+use 5.030;
+
+# read matrix from input
+my @m;
+my $ncols;
+while (<>) {
+ my @cols = split(' ', s/\D/ /gr); # ignore all but numbers
+ die "invalid matrix\n" if defined($ncols) && $ncols != scalar(@cols);
+ $ncols = scalar(@cols);
+ push @m, \@cols;
+}
+
+say count_squares(@m);
+
+
+# count squares larger than 1
+sub count_squares {
+ my(@m) = @_;
+ my $nrows = scalar(@m);
+ return 0 if $nrows < 2;
+ my $ncols = scalar(@{$m[0]});
+ return 0 if $ncols < 2;
+
+ my $count = 0;
+ for my $row (0 .. $nrows-1) {
+ for my $col (0 .. $ncols-1) {
+ if ($m[$row][$col]) {
+ for (my $d = 1; $row+$d < $nrows && $col+$d < $ncols; $d++) {
+ if ($m[$row+$d][$col] && $m[$row][$col+$d] && $m[$row+$d][$col+$d]) {
+ $count++;
+ }
+ }
+ }
+ }
+ }
+ return $count;
+}
diff --git a/challenge-084/paulo-custodio/test.pl b/challenge-084/paulo-custodio/test.pl
new file mode 100644
index 0000000000..bfa6187ff9
--- /dev/null
+++ b/challenge-084/paulo-custodio/test.pl
@@ -0,0 +1,59 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use Test::More;
+use 5.030;
+
+my $in = "in.txt";
+
+is capture("perl/ch-1.pl 1"), "1\n";
+is capture("perl/ch-1.pl 0"), "0\n";
+is capture("perl/ch-1.pl -1"), "-1\n";
+is capture("perl/ch-1.pl 1234"), "4321\n";
+is capture("perl/ch-1.pl -1234"), "-4321\n";
+is capture("perl/ch-1.pl -2147483649"), "0\n";
+is capture("perl/ch-1.pl -2147483648"), "-8463847412\n";
+is capture("perl/ch-1.pl 2147483647"), "7463847412\n";
+is capture("perl/ch-1.pl 2147483648"), "0\n";
+
+spew($in, <<END);
+[ 0 1 0 1 ]
+[ 0 0 1 0 ]
+[ 1 1 0 1 ]
+[ 1 0 0 1 ]
+END
+is capture("perl perl/ch-2.pl <$in"), "1\n";
+
+spew($in, <<END);
+[ 1 1 0 1 ]
+[ 1 1 0 0 ]
+[ 0 1 1 1 ]
+[ 1 0 1 1 ]
+END
+is capture("perl perl/ch-2.pl <$in"), "4\n";
+
+spew($in, <<END);
+[ 0 1 0 1 ]
+[ 1 0 1 0 ]
+[ 0 1 0 0 ]
+[ 1 0 0 1 ]
+END
+is capture("perl perl/ch-2.pl <$in"), "0\n";
+
+
+unlink($in);
+done_testing;
+
+sub capture {
+ my($cmd) = @_;
+ my $out = `$cmd`;
+ $out =~ s/[ \t\v\f\r]*\n/\n/g;
+ return $out;
+}
+
+sub spew {
+ my($file, $text) = @_;
+ open(my $fh, ">", $file) or die "write $file: $!\n";
+ print $fh $text;
+}