aboutsummaryrefslogtreecommitdiff
path: root/challenge-084/rage311/perl
diff options
context:
space:
mode:
authorrage311 <rage_311@hotmail.com>2020-10-28 22:13:04 -0600
committerrage311 <rage_311@hotmail.com>2020-10-28 22:13:04 -0600
commit66b62918ae8f44e862d4ea4b2ea8227e690db82c (patch)
tree832de72913c0a105e2b4dc2bd8a46ff9784a6674 /challenge-084/rage311/perl
parent0a4960ccd8b90aa3e4a7d847a470eb6a957495c9 (diff)
downloadperlweeklychallenge-club-66b62918ae8f44e862d4ea4b2ea8227e690db82c.tar.gz
perlweeklychallenge-club-66b62918ae8f44e862d4ea4b2ea8227e690db82c.tar.bz2
perlweeklychallenge-club-66b62918ae8f44e862d4ea4b2ea8227e690db82c.zip
Added perl solutions for challenge 84
Diffstat (limited to 'challenge-084/rage311/perl')
-rw-r--r--challenge-084/rage311/perl/ch-1.pl52
-rw-r--r--challenge-084/rage311/perl/ch-2.pl96
2 files changed, 148 insertions, 0 deletions
diff --git a/challenge-084/rage311/perl/ch-1.pl b/challenge-084/rage311/perl/ch-1.pl
new file mode 100644
index 0000000000..d4a86ee43f
--- /dev/null
+++ b/challenge-084/rage311/perl/ch-1.pl
@@ -0,0 +1,52 @@
+#!/usr/bin/env perl
+
+use 5.030;
+use strict;
+use warnings;
+use feature 'signatures';
+no warnings 'experimental::signatures';
+
+sub reverse_int($int) {
+ # must fit into 32-bit signed integer
+ return 0 unless -2_147_483_648 < $int && $int < 2_147_483_647;
+ my @chars = split //, "$int";
+ my $negative = defined($int < 0 ? shift @chars : undef);
+ return join "", ($negative ? '-' : ''), reverse @chars;
+}
+
+sub main {
+ die 'Integer required as input' unless
+ my $input = int($ARGV[0]);
+
+ say reverse_int($input);
+}
+
+main();
+
+__DATA__
+
+K #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
+
+
+
diff --git a/challenge-084/rage311/perl/ch-2.pl b/challenge-084/rage311/perl/ch-2.pl
new file mode 100644
index 0000000000..d454c693eb
--- /dev/null
+++ b/challenge-084/rage311/perl/ch-2.pl
@@ -0,0 +1,96 @@
+#!/usr/bin/env perl
+
+use 5.030;
+use strict;
+use warnings;
+use feature 'signatures';
+no warnings 'experimental::signatures';
+
+sub mr_anderson ($matrix) {
+ my $max_x = $#{$matrix};
+ my $max_y = $#{$matrix->[0]};
+ return 0 unless $max_x > 0 && $max_y > 0;
+
+ my $one_squares;
+
+ for my $y (0 .. $max_y) {
+ for my $x (0 .. $max_x) {
+ my $max_size = ($max_y - $y) <= ($max_x - $x) ? $max_y - $y : $max_x - $x;
+ for (
+ my $y1 = $y + $max_size, my $x1 = $x + $max_size;
+ $y1 > $y && $x1 > $x;
+ $y1--, $x1--
+ ) {
+ my $one_square =
+ $matrix->[$x][$y]
+ && $matrix->[$x][$y1]
+ && $matrix->[$x1][$y]
+ && $matrix->[$x1][$y1];
+
+ $one_squares += $one_square;
+ if ($one_square) {
+ say $x1 - $x + 1, 'x', $y1 - $y + 1, ' @ ',
+ "$x,$y to $x1,$y1";
+ }
+ }
+ }
+ }
+
+ return $one_squares;
+}
+
+sub main {
+ my $tests = [
+ [
+ [qw( 1 1 0 1 )],
+ [qw( 1 1 0 0 )],
+ [qw( 0 1 1 1 )],
+ [qw( 1 0 1 1 )],
+ ],
+
+ [
+ [qw( 1 1 0 1 1 0 1 )],
+ [qw( 1 1 0 0 1 0 0 )],
+ [qw( 0 1 1 1 1 1 0 )],
+ [qw( 1 0 1 1 1 0 1 )],
+ [qw( 1 1 0 0 1 0 0 )],
+ [qw( 0 1 1 1 1 1 0 )],
+ ],
+
+ [
+ [qw( 0 1 0 1 )],
+ [qw( 1 0 1 0 )],
+ [qw( 0 1 0 0 )],
+ [qw( 1 0 0 1 )],
+ ],
+ ];
+
+ say "one squares: ", mr_anderson($_) for $tests->@*;
+}
+
+main();
+
+
+__DATA__
+
+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 ]