aboutsummaryrefslogtreecommitdiff
path: root/challenge-084
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-11-02 02:11:00 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-11-02 02:11:00 +0000
commitc4b6f71f5e996da69749df7cdfa9d32e993868f8 (patch)
treeb78f8fe79c93ff209afce77bb2a115b3a7f79a9d /challenge-084
parent0e6aced9c2b7a782af282c2f8fdd5c18de64ac73 (diff)
downloadperlweeklychallenge-club-c4b6f71f5e996da69749df7cdfa9d32e993868f8.tar.gz
perlweeklychallenge-club-c4b6f71f5e996da69749df7cdfa9d32e993868f8.tar.bz2
perlweeklychallenge-club-c4b6f71f5e996da69749df7cdfa9d32e993868f8.zip
- Added solutions by Colin Crain.
Diffstat (limited to 'challenge-084')
-rw-r--r--challenge-084/colin-crain/perl/ch-1.pl168
-rw-r--r--challenge-084/colin-crain/perl/ch-2.pl152
-rw-r--r--challenge-084/colin-crain/raku/ch-1.raku35
-rw-r--r--challenge-084/colin-crain/raku/ch-2.raku71
4 files changed, 426 insertions, 0 deletions
diff --git a/challenge-084/colin-crain/perl/ch-1.pl b/challenge-084/colin-crain/perl/ch-1.pl
new file mode 100644
index 0000000000..ceb160212e
--- /dev/null
+++ b/challenge-084/colin-crain/perl/ch-1.pl
@@ -0,0 +1,168 @@
+#! /opt/local/bin/perl
+#
+# reverse-and-repeat.pl
+#
+# 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
+
+# method:
+# A complex idea with the abstract idea of a decimal number; a
+# simple dispatch if considered as a string. Fortunately for us,
+# Perl excels at this idea of a character representation holding a
+# dual personality. Mathematically speaking, the glyphs recorded in
+# a text aren't truly the numbers per se, only representations of
+# them. The symbolic representation can change; the numerical
+# essance remains outside of its physically recorded state.
+#
+# The positional decimal system using arabic numerals is globally
+# ubiquitous today but this has hardly been true throughout history.
+# Because of its practical linkage to the use of the abacus, the
+# Roman numeral system maintained dominance in mercantile accounting
+# well into the 14th or 15th century.*
+#
+# The merchants that used calculations daily preferred the physical
+# existence of a strike on a counting board or a bead on an abacus.
+# It was what they knew and it worked. Roman numerals held within
+# them a decimal core -- I, X, C, M for 1, 10, 100, 1000 -- but the
+# abstraction of of the glyphs was simpler, referring back to a
+# unary base: "I" was one stick, "III" was three sticks and "X" was
+# the same as "IIIII IIIII". "V" was half of an "X", quite visually
+# expressed.
+#
+# Positional notation, on the other hand, depended on the idea of
+# the zero as a placeholder, and practical as this idea might be in
+# the long run, the modern methods faced stubborn resistance from
+# the governments that held the merchant classes to account. It is
+# argued that the essence of the zero —- something that exists but
+# holds no value, signifying nothing; something there but also
+# somehow not there at the same time -- all this made the very idea
+# of its use at least suspicious, if not "Saracen sorcery". One
+# Venetian source wrote: "the old figures [i.e. Roman numerals]
+# alone are used because they cannot be falsified as easily as those
+# of the new art of computation"**
+#
+# Consider for a moment the tendered amount written out twice on a
+# check. These ideas even hold out through this day, that somehow
+# the written out words are more real than the numerical notation,
+# more unalterably defined by the sentence describing them than by
+# the numbers themselves. The textual phrasing anchors the abstract
+# and elusive mathematical realm to the real world of here and now.
+#
+# I believe you can still see the vestiges of this concreteness in
+# the COBOL programming language as well, but that's another story.
+
+#
+# ======================================
+#
+# * Durham, John W. “THE INTRODUCTION OF ‘ARABIC’ NUMERALS IN
+# EUROPEAN ACCOUNTING.” The Accounting Historians Journal, vol. 19,
+# no. 2, 1992, pp. 25–55. JSTOR, www.jstor.org/stable/40698081.
+# Accessed 27 Oct. 2020.
+#
+# ** Kaplan, Robert "The Nothing That Is" pp. 102 Oxford University
+# Press 1999
+#
+# 2020 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use warnings;
+use strict;
+use feature ":5.26";
+
+## ## ## ## ## MAIN:
+##
+## turning the tables served three ways:
+##
+
+## 1. reverse
+
+say '';
+say "=" x 25;
+say '';
+say "the right way. Reverse the string:\n";
+
+my $num = $ARGV[0] // 9271;
+
+my $rev = reverse $num;
+
+say "input: $num";
+say "output: ", $rev > 2147483647 ? 0 : $rev;
+
+##--------------------------
+## 2. positionally
+
+say '';
+say "=" x 25;
+say '';
+say "hard mode, done positionally:\n";
+
+my $input = shift @ARGV // 2147483647;
+ $num = $input;
+my @positions;
+
+while ($num) {
+ unshift @positions, $num % 10;
+ $num = int $num/10;
+}
+
+my $reversed = 0;
+say "starting output = 0";
+for my $power ( 0..$#positions ) {
+ say "--> adding $positions[0] x 10^$power = ",
+ sprintf "%10d", $reversed + $positions[0] * (10 ** $power);
+ $reversed += (shift @positions) * (10 ** $power);
+}
+
+if ($reversed >> 31 > 0) {
+ say "\nthe number $reversed cannot be fit into a 32-bit signed int\n";
+}
+
+
+say '';
+say "input : $input";
+say "output: ", $reversed >> 31 > 0 ? 0 : $reversed;
+
+
+##----------------------------
+## 3. mathematically
+
+say '';
+say "=" x 25;
+say '';
+say "hard mode, done mathematically:\n";
+
+$input = shift @ARGV // 2147483647;
+$num = $input;
+my $output = 0;
+
+my $power = 0;
+$power++ while int $num / 10 ** $power > 0;
+
+while ($power--) {
+ $output += $num % 10 * 10 ** $power ;
+ say $output;
+ $num = int $num/10;
+}
+
+say '';
+say "input : $input";
+say "output: ", $output >> 31 > 0 ? 0 : $output;
diff --git a/challenge-084/colin-crain/perl/ch-2.pl b/challenge-084/colin-crain/perl/ch-2.pl
new file mode 100644
index 0000000000..e883c99ff8
--- /dev/null
+++ b/challenge-084/colin-crain/perl/ch-2.pl
@@ -0,0 +1,152 @@
+#! /opt/local/bin/perl
+#
+# four_corners_off_a_twenty.pl
+#
+# 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
+
+
+# method:
+# We need to methodically examine quads of points, each expressing
+# the four corners of an internal square. Each square in turn can be
+# expressed as a base corner closest to the origin and an edge size.
+# If all four corners are 1s, then we have a match and the square is
+# logged. The squares to be looked at range in size from edge length
+# 2 to the shorter dimension of the enclosing matrix. Every square
+# group of a given size starts with the first instance in the upper
+# left corner at (0,0) and from there gets translated righwards and
+# down to map all possible placements. The number of repetions in
+# each direction will be the length of the matrix dimension minus
+# the square edge dimension.
+#
+# The number of squares to be evaluated is defined by the
+# sequence of the square pyrimidal numbers*, A00330 in the OEIS.
+# Using the formula for generating the n-th number in the
+# sequence
+#
+# a(n) = n*(n+1)*(2*n+1)/6
+#
+# As we are only considering squares of minimum edge length 2,
+# the number of squares S contained within an (M x M) matrix is
+#
+# S = a(M-1)
+#
+# We walk through every point in the matrix, and if it's a 1 we can then
+# check it for squares of increasing size until a dimension goes out of bounds.
+# This prunes evaluation of many squares immediately.
+#
+#
+# -----------------------------------------------------
+# * OEIS A00330, Kristie Smith, Apr 16 2002
+
+
+# 2020 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use warnings;
+use strict;
+use feature ":5.26";
+
+## ## ## ## ## MAIN:
+
+our $squarecount = 0;
+
+srand(1010);
+
+my $r = 5 ;
+my $c = 10;
+my $matrix = [ map { [ map { int rand 2 == 0 ? 1 : 0 } (1..$c) ] } (1..$r) ] ;
+
+
+
+say "@$_" for $matrix->@*;
+say '';
+
+
+my $cols = $matrix->[0]->@*;
+my $rows = $matrix->@*;
+
+my $min_dimension = $cols < $rows ? $cols : $rows;
+
+my @output;
+
+for my $c ( 0..$cols-2 ) {
+ for my $r ( 0..$rows-2) {
+ next unless $matrix->[$r][$c];
+ for my $size ( 2..$min_dimension ) { ## for each square size group
+ last if $c + $size > $cols || $r + $size > $rows;
+ if (four_corners($r, $c, $size, $matrix)) {
+ push @output, [ $r, $c, $size ];
+ }
+ }
+ }
+}
+
+# say "square at row $_->[0] column $_->[1] with size $_->[2]" for @output;
+say "found ", scalar @output, " squares:";
+
+for my $s ( sort { $a->[2] <=> $b->[2] } @output ) {
+ printf "column %d row %d from top - square of size %d \n", $s->[1]+1, $s->[0]+1, $s->[2];
+}
+
+say "\nevaluated $squarecount squares";
+
+## ## ## ## ## SUBS:
+
+sub four_corners {
+ $squarecount++;
+
+ my ($r, $c, $size, $matrix) = @_;
+ $size--;
+
+ ## short-circuits out
+ return 1 if $matrix->[$r][$c] == 1
+ && $matrix->[$r][$c+$size] == 1
+ && $matrix->[$r+$size][$c] == 1
+ && $matrix->[$r+$size][$c+$size] == 1 ;
+ return 0;
+}
+
diff --git a/challenge-084/colin-crain/raku/ch-1.raku b/challenge-084/colin-crain/raku/ch-1.raku
new file mode 100644
index 0000000000..3a6aa39244
--- /dev/null
+++ b/challenge-084/colin-crain/raku/ch-1.raku
@@ -0,0 +1,35 @@
+#!/usr/bin/env perl6
+#
+#
+# reverse-and-repeat.raku
+#
+# 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
+
+#
+# 2020 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+unit sub MAIN (Int $num where $num > 0 = 1729) ;
+
+say $num.flip +> 31 > 0 ?? 0
+ !! $num.flip;
diff --git a/challenge-084/colin-crain/raku/ch-2.raku b/challenge-084/colin-crain/raku/ch-2.raku
new file mode 100644
index 0000000000..1fc8ebe1c1
--- /dev/null
+++ b/challenge-084/colin-crain/raku/ch-2.raku
@@ -0,0 +1,71 @@
+#!/usr/bin/env perl6
+#
+#
+# four-corners-off-a-twenty.raku
+#
+# 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.
+
+#
+# 2020 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+unit sub MAIN ($r = 5, $c = 6) ;
+
+srand 20201031; ## anchor this for development or comparison
+ ## its a good example
+
+sub four_corners ($r, $c, $size is copy, @matrix) {
+ $size--; ## square side includes corner element
+
+ return 1 if ( @matrix[$r;$c] == 1
+ && @matrix[$r+$size;$c] == 1
+ && @matrix[$r;$c+$size] == 1
+ && @matrix[$r+$size;$c+$size] == 1 );
+}
+
+my @matrix.push: (0,1).roll($c) for ^$r;
+
+.say for @matrix;
+say '';
+
+my $rows = @matrix.elems;
+my $cols = @matrix[0].elems;
+my @output;
+
+for 0..$rows-2 -> $r {
+ for 0..$cols-2 -> $c {
+ next unless @matrix[$r;$c];
+ for 2..min($rows,$cols) -> $size {
+ last if (($c + $size > $cols) || ($r + $size > $rows));
+
+ if four_corners( $r, $c, $size, @matrix) {
+ @output.push: ($r, $c, $size);
+ }
+ }
+ }
+}
+
+say "found ", @output.elems, " squares:\n";
+
+for sort {$^a[2] leg $^b[2]}, @output -> $s {
+ printf "column %d row %d from top - square of size %d\n",
+ $s[1]+1, $s[0]+1, $s[2];
+}