diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-11-11 17:22:18 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-11-11 17:22:18 +0000 |
| commit | 60e9cb0fcbf3a557ffa17a2243e309e8489847c3 (patch) | |
| tree | 75b8f035e28b754ac23799e095cb9c5aed79d451 | |
| parent | c0d9658341be76934b2a8881ccaf7aeb5acbf9fa (diff) | |
| download | perlweeklychallenge-club-60e9cb0fcbf3a557ffa17a2243e309e8489847c3.tar.gz perlweeklychallenge-club-60e9cb0fcbf3a557ffa17a2243e309e8489847c3.tar.bz2 perlweeklychallenge-club-60e9cb0fcbf3a557ffa17a2243e309e8489847c3.zip | |
Add Python solution to challenge 84
| -rw-r--r-- | challenge-084/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-084/paulo-custodio/perl/ch-1.pl | 6 | ||||
| -rw-r--r-- | challenge-084/paulo-custodio/perl/ch-2.pl | 15 | ||||
| -rw-r--r-- | challenge-084/paulo-custodio/python/ch-1.py | 42 | ||||
| -rw-r--r-- | challenge-084/paulo-custodio/python/ch-2.py | 81 | ||||
| -rw-r--r-- | challenge-084/paulo-custodio/t/test-1.yaml | 45 | ||||
| -rw-r--r-- | challenge-084/paulo-custodio/t/test-2.yaml | 27 | ||||
| -rw-r--r-- | challenge-084/paulo-custodio/test.pl | 57 | ||||
| -rw-r--r-- | challenge-085/paulo-custodio/test.pl | 21 |
9 files changed, 209 insertions, 87 deletions
diff --git a/challenge-084/paulo-custodio/Makefile b/challenge-084/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-084/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-084/paulo-custodio/perl/ch-1.pl b/challenge-084/paulo-custodio/perl/ch-1.pl index c81e86db18..6082b77511 100644 --- a/challenge-084/paulo-custodio/perl/ch-1.pl +++ b/challenge-084/paulo-custodio/perl/ch-1.pl @@ -1,13 +1,13 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # Challenge 084 # -# TASK #1 › Reverse Integer +# 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 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. diff --git a/challenge-084/paulo-custodio/perl/ch-2.pl b/challenge-084/paulo-custodio/perl/ch-2.pl index 000dfe2589..242cc569a1 100644 --- a/challenge-084/paulo-custodio/perl/ch-2.pl +++ b/challenge-084/paulo-custodio/perl/ch-2.pl @@ -2,7 +2,7 @@ # Challenge 084 # -# TASK #2 › Find Square +# TASK #2 > Find Square # Submitted by: Mohammad S Anwar # You are given matrix of size m x n with only 1 and 0. # @@ -16,7 +16,8 @@ # # Output: 1 # Explanation: -# There is one square (3x3) in the given matrix with four corners as 1 starts at r=1;c=2. +# 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 ] @@ -28,10 +29,12 @@ # # 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. +# 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 ] diff --git a/challenge-084/paulo-custodio/python/ch-1.py b/challenge-084/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..6fc8d909f9 --- /dev/null +++ b/challenge-084/paulo-custodio/python/ch-1.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 + +# 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 + +import sys + +def reverse_int(n): + if n < -0x80000000 or n > 0x7fffffff: + return 0 + rev = 0 + if n < 0: + sign = -1 + n = -n + else: + sign = 1 + while n > 0: + rev = 10 * rev + n%10 + n //= 10 + rev *= sign + return rev + +print(reverse_int(int(sys.argv[1]))) diff --git a/challenge-084/paulo-custodio/python/ch-2.py b/challenge-084/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..760041916e --- /dev/null +++ b/challenge-084/paulo-custodio/python/ch-2.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python3 + +# 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 + +import fileinput +import re + +def read_input(): + lines = [] + for line in fileinput.input(): + lines.append(line) + return lines + +def read_matrix(lines): + m = [] + for line in lines: + line = re.sub(r"\D+", " ", line) + cols = [int(x) for x in line.split()] + m.append(cols) + return m + +def count_squares(m): + nrows = len(m) + ncols = len(m[0]) + if nrows < 2 or ncols < 2: + return 0 + + count = 0 + for r in range(nrows): + for c in range(ncols): + if m[r][c]: + d = 1 + while r+d < nrows and c+d < ncols: + if m[r+d][c] and m[r][c+d] and m[r+d][c+d]: + count += 1 + d += 1 + + return count + +print(count_squares(read_matrix(read_input()))) diff --git a/challenge-084/paulo-custodio/t/test-1.yaml b/challenge-084/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..3b31c13a7f --- /dev/null +++ b/challenge-084/paulo-custodio/t/test-1.yaml @@ -0,0 +1,45 @@ +- setup: + cleanup: + args: 1 + input: + output: 1 +- setup: + cleanup: + args: 0 + input: + output: 0 +- setup: + cleanup: + args: -1 + input: + output: -1 +- setup: + cleanup: + args: 1234 + input: + output: 4321 +- setup: + cleanup: + args: -1234 + input: + output: -4321 +- setup: + cleanup: + args: -2147483649 + input: + output: 0 +- setup: + cleanup: + args: -2147483648 + input: + output: -8463847412 +- setup: + cleanup: + args: 2147483647 + input: + output: 7463847412 +- setup: + cleanup: + args: 2147483648 + input: + output: 0 diff --git a/challenge-084/paulo-custodio/t/test-2.yaml b/challenge-084/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..f6fd98235b --- /dev/null +++ b/challenge-084/paulo-custodio/t/test-2.yaml @@ -0,0 +1,27 @@ +- setup: + cleanup: + args: + input: | + [ 0 1 0 1 ] + [ 0 0 1 0 ] + [ 1 1 0 1 ] + [ 1 0 0 1 ] + output: 1 +- setup: + cleanup: + args: + input: | + [ 1 1 0 1 ] + [ 1 1 0 0 ] + [ 0 1 1 1 ] + [ 1 0 1 1 ] + output: 4 +- setup: + cleanup: + args: + input: | + [ 0 1 0 1 ] + [ 1 0 1 0 ] + [ 0 1 0 0 ] + [ 1 0 0 1 ] + output: 0 diff --git a/challenge-084/paulo-custodio/test.pl b/challenge-084/paulo-custodio/test.pl deleted file mode 100644 index 91fde37cc1..0000000000 --- a/challenge-084/paulo-custodio/test.pl +++ /dev/null @@ -1,57 +0,0 @@ -#!/usr/bin/perl - -use Modern::Perl; -use Test::More; - -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; -} diff --git a/challenge-085/paulo-custodio/test.pl b/challenge-085/paulo-custodio/test.pl deleted file mode 100644 index b32170a073..0000000000 --- a/challenge-085/paulo-custodio/test.pl +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/perl - -use Modern::Perl; -use Test::More; - -is capture("perl/ch-1.pl 1.2 0.4 0.1 2.5"), "1\n"; -is capture("perl/ch-1.pl 0.2 1.5 0.9 1.1"), "0\n"; -is capture("perl/ch-1.pl 0.5 1.1 0.3 0.7"), "1\n"; - -is capture("perl/ch-2.pl 8"), "1\n"; -is capture("perl/ch-2.pl 15"), "0\n"; -is capture("perl/ch-2.pl 125"), "1\n"; - -done_testing; - -sub capture { - my($cmd) = @_; - my $out = `$cmd`; - $out =~ s/[ \t\v\f\r]*\n/\n/g; - return $out; -} |
