aboutsummaryrefslogtreecommitdiff
path: root/challenge-084
diff options
context:
space:
mode:
authorjuliodcs <julio.dcs@gmail.com>2020-10-26 22:40:08 +0100
committerjuliodcs <julio.dcs@gmail.com>2020-10-26 22:40:08 +0100
commitcbf73c5afb3183f2b6c35746368aa1372e5c6c88 (patch)
tree67cba16806a5132cc0076da380c59ef9bc540a8c /challenge-084
parentdadcf251061d31b7073bd05005420d46fec6a8b3 (diff)
downloadperlweeklychallenge-club-cbf73c5afb3183f2b6c35746368aa1372e5c6c88.tar.gz
perlweeklychallenge-club-cbf73c5afb3183f2b6c35746368aa1372e5c6c88.tar.bz2
perlweeklychallenge-club-cbf73c5afb3183f2b6c35746368aa1372e5c6c88.zip
juliodcs-week78
Diffstat (limited to 'challenge-084')
-rw-r--r--challenge-084/juliodcs/perl/ch-1.pl31
-rw-r--r--challenge-084/juliodcs/perl/ch-2.pl41
-rw-r--r--challenge-084/juliodcs/raku/ch-1.raku22
-rw-r--r--challenge-084/juliodcs/raku/ch-2.raku37
4 files changed, 131 insertions, 0 deletions
diff --git a/challenge-084/juliodcs/perl/ch-1.pl b/challenge-084/juliodcs/perl/ch-1.pl
new file mode 100644
index 0000000000..8bc7933ffe
--- /dev/null
+++ b/challenge-084/juliodcs/perl/ch-1.pl
@@ -0,0 +1,31 @@
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+use bigint;
+
+use constant INT_LAST => 2_147_483_647;
+
+sub flip($number) {
+ my $sign = $number < 0 ? q(-) : q();
+ my $numb = join q(), reverse split m//, abs $number;
+ return $numb > INT_LAST ? 0 : $sign . $numb;
+}
+
+if (@ARGV == 1) {
+ say flip(shift);
+ exit 0;
+}
+
+use Test::More;
+
+is flip(2147483648), 0, 'Integer last + 1';
+is flip(99999999999999999999999999), 0, 'Big number';
+is flip(1), 1, 'Small number';
+is flip(0), 0, 'Zero, no sign';
+is flip(-0), 0, 'Zero, with sign is still zero';
+is flip(123), 321, '123=321';
+is flip(2147483641), 1463847412, '2147483641=1463847412';
+is flip(-2147483641), -1463847412, '-2147483641=-1463847412';
+
+done_testing;
diff --git a/challenge-084/juliodcs/perl/ch-2.pl b/challenge-084/juliodcs/perl/ch-2.pl
new file mode 100644
index 0000000000..0ae90bd8ed
--- /dev/null
+++ b/challenge-084/juliodcs/perl/ch-2.pl
@@ -0,0 +1,41 @@
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub all_ones($matrix, $x, $y, $size) {
+ return $matrix->[$x ][$y ]
+ && $matrix->[$x+$size-1][$y ]
+ && $matrix->[$x ][$y+$size-1]
+ && $matrix->[$x+$size-1][$y+$size-1];
+}
+
+sub count_squares($matrix) {
+ my $count = 0;
+
+ my $h = @{ $matrix };
+ my $w = @{ $matrix->[0] };
+ my $max = $h > $w ? $h : $w;
+
+ for my $s (2 .. $max) {
+ for my $y (0 .. $w - $s) {
+ for my $x (0 .. $h - $s) {
+ $count++ if all_ones($matrix, $x, $y, $s);
+ }
+ }
+ }
+
+ return $count;
+}
+
+use Test::More;
+
+is count_squares([[1, 1], [1, 1]]), 1, 'Simple matrix';
+is count_squares([[1, 1], [1, 0]]), 0, 'Simple matrix, incomplete';
+is count_squares([[1, 1], [1, 0], [1, 1], [1, 1]]), 1, 'Simple (w != h) matrix';
+is count_squares([[1, 1], [1, 1], [1, 1], [1, 1]]), 3, 'Simple (w != h) matrix, all complete';
+is count_squares([[0, 1, 0, 1], [0, 0, 1, 0], [1, 1, 0, 1], [1, 0, 0, 1]]), 1, 'Example test 1';
+is count_squares([[1, 1, 0, 1], [1, 1, 0, 0], [0, 1, 1, 1], [1, 0, 1, 1]]), 4, 'Example test 2';
+is count_squares([[0, 1, 0, 1], [1, 0, 1, 0], [0, 1, 0, 0], [1, 0, 0, 1]]), 0, 'Example test 3';
+
+done_testing;
diff --git a/challenge-084/juliodcs/raku/ch-1.raku b/challenge-084/juliodcs/raku/ch-1.raku
new file mode 100644
index 0000000000..eaac490491
--- /dev/null
+++ b/challenge-084/juliodcs/raku/ch-1.raku
@@ -0,0 +1,22 @@
+#! /usr/bin/raku
+
+constant INT_MAX = 2_147_483_647;
+
+sub flip(Int \number) {
+ number > INT_MAX ?? 0 !! number.abs.flip * number.sign;
+}
+
+#############################################################################
+
+use Test;
+
+is flip(2147483648), 0, 'Integer last + 1';
+is flip(99999999999999999999999999), 0, 'Big number';
+is flip(1), 1, 'Small number';
+is flip(0), 0, 'Zero, no sign';
+is flip(-0), 0, 'Zero, with sign is still zero';
+is flip(123), 321, '123=321';
+is flip(2147483641), 1463847412, '2147483641=1463847412';
+is flip(-2147483641), -1463847412, '-2147483641=-1463847412';
+
+done-testing;
diff --git a/challenge-084/juliodcs/raku/ch-2.raku b/challenge-084/juliodcs/raku/ch-2.raku
new file mode 100644
index 0000000000..62752c57a9
--- /dev/null
+++ b/challenge-084/juliodcs/raku/ch-2.raku
@@ -0,0 +1,37 @@
+#! /usr/bin/raku
+
+sub count-squares(\matrix) {
+ sub all-ones(\x, \y, \size) {
+ matrix[ x ; y ]
+ && matrix[ x + size.pred ; y ]
+ && matrix[ x ; y + size.pred ]
+ && matrix[ x + size.pred ; y + size.pred ]
+ }
+
+ my \h = matrix.elems;
+ my \w = matrix.head.elems;
+
+ my $count = 0;
+ for 2 .. max h, w -> \s {
+ for 0 .. w - s -> \y {
+ for 0 .. h - s -> \x {
+ $count++ if all-ones x, y, s;
+ }
+ }
+ }
+ $count
+}
+
+#############################################################################
+
+use Test;
+
+is count-squares([[1, 1], [1, 1]]), 1, 'Simple matrix';
+is count-squares([[1, 1], [1, 0]]), 0, 'Simple matrix, incomplete';
+is count-squares([[1, 1], [1, 0], [1, 1], [1, 1]]), 1, 'Simple (w != h) matrix';
+is count-squares([[1, 1], [1, 1], [1, 1], [1, 1]]), 3, 'Simple (w != h) matrix, all complete';
+is count-squares([[0, 1, 0, 1], [0, 0, 1, 0], [1, 1, 0, 1], [1, 0, 0, 1]]), 1, 'Example test 1';
+is count-squares([[1, 1, 0, 1], [1, 1, 0, 0], [0, 1, 1, 1], [1, 0, 1, 1]]), 4, 'Example test 2';
+is count-squares([[0, 1, 0, 1], [1, 0, 1, 0], [0, 1, 0, 0], [1, 0, 0, 1]]), 0, 'Example test 3';
+
+done-testing;