diff options
| -rw-r--r-- | challenge-084/athanasius/perl/ch-1.pl | 107 | ||||
| -rw-r--r-- | challenge-084/athanasius/perl/ch-2.pl | 194 | ||||
| -rw-r--r-- | challenge-084/athanasius/raku/ch-1.raku | 91 | ||||
| -rw-r--r-- | challenge-084/athanasius/raku/ch-2.raku | 206 |
4 files changed, 598 insertions, 0 deletions
diff --git a/challenge-084/athanasius/perl/ch-1.pl b/challenge-084/athanasius/perl/ch-1.pl new file mode 100644 index 0000000000..65220f159f --- /dev/null +++ b/challenge-084/athanasius/perl/ch-1.pl @@ -0,0 +1,107 @@ +#!perl + +############################################################################### +=comment + +Perl Weekly 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 + +=cut +############################################################################### + +#--------------------------------------# +# Copyright © 2020 PerlMonk Athanasius # +#--------------------------------------# + + # Exports: +use strict; +use warnings; +use Const::Fast; # const() +use Regexp::Common qw( number ); # %RE{num} + +const my $MAX => (2 ** 31) - 1; +const my $MIN => -(2 ** 31); +const my $USAGE => +"Usage: + perl $0 <N> + + <N> An integer to be reversed\n"; + +#------------------------------------------------------------------------------ +BEGIN +#------------------------------------------------------------------------------ +{ + $| = 1; + print "\nChallenge 084, Task #1: Reverse Integer (Perl)\n\n"; +} + +#============================================================================== +MAIN: +#============================================================================== +{ + my $N = parse_command_line(); + + print "Input: $N\n"; + + my $reverse = reverse abs $N; + + if ($N < 0) + { + $reverse *= -1; + $reverse = 0 if $reverse < $MIN; + } + else + { + $reverse = 0 if $reverse > $MAX; + } + + print "Output: $reverse\n"; +} + +#------------------------------------------------------------------------------ +sub parse_command_line +#------------------------------------------------------------------------------ +{ + my $args = scalar @ARGV; + + scalar @ARGV == 1 + or die "ERROR: Expected a single command-line argument, found " . + "$args\n$USAGE"; + + my $N = $ARGV[0]; + $N =~ / \A $RE{num}{int} \z /x + or die "ERROR: \"$N\" is not an integer\n$USAGE"; + + return int $N; +} + +############################################################################### diff --git a/challenge-084/athanasius/perl/ch-2.pl b/challenge-084/athanasius/perl/ch-2.pl new file mode 100644 index 0000000000..ae74739b71 --- /dev/null +++ b/challenge-084/athanasius/perl/ch-2.pl @@ -0,0 +1,194 @@ +#!perl + +############################################################################### +=comment + +Perl Weekly 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 + +=cut +############################################################################### + +#--------------------------------------# +# Copyright © 2020 PerlMonk Athanasius # +#--------------------------------------# + +use strict; +use warnings; +use Const::Fast; + +const my $EXPLAIN => 1; +const my $USAGE => +qq[Usage: + perl $0 [<rows> ...] + + [<rows> ...] 1+ same-width rows, each a string of 1+ "1" and "0" chars +]; + +#------------------------------------------------------------------------------ +BEGIN +#------------------------------------------------------------------------------ +{ + $| = 1; + print "\nChallenge 084, Task #2: Find Square (Perl)\n\n"; +} + +#============================================================================== +MAIN: +#============================================================================== +{ + my @A = get_matrix(); + + print_matrix(\@A); + + my $width = scalar @{ $A[0] }; + my $height = scalar @A; + my $max_side = $width <= $height ? $width : $height; + my $count = 0; + my @squares; + + for my $row_top (0 .. $height - 2) + { + for my $col_left (0 .. $width - 2) + { + next unless $A[$row_top][$col_left] eq '1'; + + for my $side (1 .. $max_side) + { + my $col_right = $col_left + $side; + + next unless $col_right < $width && + $A[$row_top][$col_right] eq '1'; + + my $row_bottom = $row_top + $side; + + next unless $row_bottom < $height && + $A[$row_bottom][$col_left ] eq '1' && + $A[$row_bottom][$col_right] eq '1'; + + ++$count; + + push @squares, [ $row_top + 1, $col_left + 1, $side + 1 ] + if $EXPLAIN; + } + } + } + + print " Output: $count\n"; + + if ($EXPLAIN && scalar @squares > 0) + { + print "\nExplanation:\n\n"; + + for (@squares) + { + printf " %dx%d square with top left corner at row %d, column %d\n", + (@$_)[2, 2, 0, 1]; + } + } +} + +#------------------------------------------------------------------------------ +sub get_matrix +#------------------------------------------------------------------------------ +{ + scalar @ARGV > 0 or error('No command-line arguments'); + + my $width = length $ARGV[0]; + + $width > 0 or error('Empty first row'); + + my @A; + + for my $i (0 .. $#ARGV) + { + $ARGV[$i] =~ /([^10])/ and error("Invalid character \"$1\""); + + length $ARGV[$i] == $width + or error(sprintf 'Inconsistent number of ' . + 'columns in row %d', $i + 1); + + push @A, [ split //, $ARGV[$i] ]; + } + + return @A; +} + +#------------------------------------------------------------------------------ +sub error +#------------------------------------------------------------------------------ +{ + my ($message) = @_; + + die "ERROR: $message\n$USAGE"; +} + +#------------------------------------------------------------------------------ +sub print_matrix +#------------------------------------------------------------------------------ +{ + my ($matrix) = @_; + + printf " Input: [ %s ]\n", join ' ', @{ $matrix->[0 ] }; + printf " [ %s ]\n", join ' ', @{ $matrix->[$_] } for 1 .. $#$matrix; + print "\n"; +} + +############################################################################### diff --git a/challenge-084/athanasius/raku/ch-1.raku b/challenge-084/athanasius/raku/ch-1.raku new file mode 100644 index 0000000000..9e9795801e --- /dev/null +++ b/challenge-084/athanasius/raku/ch-1.raku @@ -0,0 +1,91 @@ +use v6d; + +############################################################################### +=begin comment + +Perl Weekly 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 + +=end comment +############################################################################### + +#--------------------------------------# +# Copyright © 2020 PerlMonk Athanasius # +#--------------------------------------# + +my Int constant $MAX = (2 ** 31) - 1; +my Int constant $MIN = -(2 ** 31); + +#------------------------------------------------------------------------------ +BEGIN +#------------------------------------------------------------------------------ +{ + "\nChallenge 084, Task #1: Reverse Integer (Raku)\n".put; +} + +##============================================================================= +sub MAIN +( + Int:D $N #= An integer to be reversed +) +##============================================================================= +{ + my Int $n = $N.Int; + + "Input: $n".put; + + my $reverse = $n.abs.flip; + + if $n < 0 + { + $reverse *= -1; + $reverse = 0 if $reverse < $MIN; + } + else + { + $reverse = 0 if $reverse > $MAX; + } + + print "Output: $reverse\n"; +} + +#------------------------------------------------------------------------------ +sub USAGE() +#------------------------------------------------------------------------------ +{ + my Str $usage = $*USAGE; + + $usage ~~ s/ ($*PROGRAM-NAME) /raku $0/; + $usage.put; +} + +############################################################################## diff --git a/challenge-084/athanasius/raku/ch-2.raku b/challenge-084/athanasius/raku/ch-2.raku new file mode 100644 index 0000000000..bcb7b8b90e --- /dev/null +++ b/challenge-084/athanasius/raku/ch-2.raku @@ -0,0 +1,206 @@ +use v6d; + +############################################################################### +=begin comment + +Perl Weekly 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 + +=end comment +############################################################################### + +#--------------------------------------# +# Copyright © 2020 PerlMonk Athanasius # +#--------------------------------------# + +my Bool constant $EXPLAIN = True; + +subset Element where 1|0; + +#------------------------------------------------------------------------------ +BEGIN +#------------------------------------------------------------------------------ +{ + "\nChallenge 084, Task #2: Find Square (Raku)\n".put; +} + +##============================================================================= +sub MAIN +( + *@rows where { @rows\ .elems > 0 && #= 1+ same-width rows, each a + @rows[0].chars > 0 } #= string of 1+ "1" and "0" chars +) +##============================================================================= +{ + my Str @str-rows = @rows; + + my Array[Element] @A = get-matrix(@str-rows); + + print-matrix(@A); + + my UInt $width = @A[0].elems; + my UInt $height = @A\ .elems; + my UInt $max-side = $width <= $height ?? $width !! $height; + my UInt $count = 0; + + my Array[UInt] @squares; + + for 0 .. $height - 2 -> UInt $row-top + { + for 0 .. $width - 2 -> UInt $col-left + { + next unless @A[$row-top;$col-left] eq '1'; + + for 1 .. $max-side -> UInt $side + { + my UInt $col-right = $col-left + $side; + + next unless $col-right < $width && + @A[$row-top;$col-right] eq '1'; + + my $row-bottom = $row-top + $side; + + next unless $row-bottom < $height && + @A[$row-bottom;$col-left ] eq '1' && + @A[$row-bottom;$col-right] eq '1'; + + ++$count; + + if $EXPLAIN + { + @squares.push: Array[UInt].new($row-top + 1, $col-left + 1, + $side + 1); + } + } + } + } + + " Output: $count".put; + + if $EXPLAIN && @squares.elems > 0 + { + "\nExplanation:\n".put; + + for @squares + { + " %dx%d square with top left corner at row %d, column %d\n".printf: + $_[2, 2, 0, 1]; + } + } +} + +#------------------------------------------------------------------------------ +sub get-matrix( Str:D @rows --> Array:D[Element:D] ) +#------------------------------------------------------------------------------ +{ + my Array[Element] @matrix[ @rows.elems ]; + + my UInt $width = @rows[0].chars; + + for 0 .. @rows.end -> UInt $i + { + my Str $row = @rows[$i]; + + $row ~~ / ( <-[ 1 0 ]> ) / + and error("Invalid character '$0' in the input matrix"); + + $row.chars == $width + or error("Inconsistent number of columns in row { $i + 1 }"); + + my Element @chars = $row.split: '', :skip-empty; + + @matrix[$i] = @chars; + } + + return @matrix; +} + +#------------------------------------------------------------------------------ +sub print-matrix( Array:D[Element:D] @matrix ) +#------------------------------------------------------------------------------ +{ + " Input: [ %s ]\n".printf: @matrix[0 ].join: ' '; + " [ %s ]\n".printf: @matrix[$_].join: ' ' for 1 .. @matrix.end; + + ''.put; +} + +#------------------------------------------------------------------------------ +sub error( Str:D $message ) +#------------------------------------------------------------------------------ +{ + "ERROR: $message".put; + + USAGE(); + + exit; +} + +#------------------------------------------------------------------------------ +sub USAGE() +#------------------------------------------------------------------------------ +{ + my Str $usage = $*USAGE; + + $usage ~~ s/ ($*PROGRAM-NAME) /raku $0/; + $usage.put; +} + +############################################################################### |
