diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-11-11 19:26:35 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-11 19:26:35 +0000 |
| commit | 8c6baf09f0fcec80f376408a97e72c592048af2f (patch) | |
| tree | b4524c0dfe4ce866872d9cffe711441d3abab793 | |
| parent | 204a188b693bfaed51420c4422736b9db2bd8723 (diff) | |
| parent | 60e9cb0fcbf3a557ffa17a2243e309e8489847c3 (diff) | |
| download | perlweeklychallenge-club-8c6baf09f0fcec80f376408a97e72c592048af2f.tar.gz perlweeklychallenge-club-8c6baf09f0fcec80f376408a97e72c592048af2f.tar.bz2 perlweeklychallenge-club-8c6baf09f0fcec80f376408a97e72c592048af2f.zip | |
Merge pull request #5196 from pauloscustodio/devel
Add Python solution to challenge 87
32 files changed, 785 insertions, 243 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/Makefile b/challenge-085/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-085/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-085/paulo-custodio/perl/ch-1.pl b/challenge-085/paulo-custodio/perl/ch-1.pl index a471b54e85..6037ae6976 100644 --- a/challenge-085/paulo-custodio/perl/ch-1.pl +++ b/challenge-085/paulo-custodio/perl/ch-1.pl @@ -1,12 +1,13 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # Challenge 085 # -# TASK #1 › Triplet Sum +# TASK #1 > Triplet Sum # Submitted by: Mohammad S Anwar # You are given an array of real numbers greater than zero. # -# Write a script to find if there exists a triplet (a,b,c) such that 1 < a+b+c < 2. Print 1 if you succeed otherwise 0. +# Write a script to find if there exists a triplet (a,b,c) such that +# 1 < a+b+c < 2. Print 1 if you succeed otherwise 0. # # Example 1: # Input: @R = (1.2, 0.4, 0.1, 2.5) diff --git a/challenge-085/paulo-custodio/perl/ch-2.pl b/challenge-085/paulo-custodio/perl/ch-2.pl index c0a16a4221..4b0f2447f4 100644 --- a/challenge-085/paulo-custodio/perl/ch-2.pl +++ b/challenge-085/paulo-custodio/perl/ch-2.pl @@ -1,12 +1,13 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # Challenge 085 # -# TASK #2 › Power of Two Integers +# TASK #2 > Power of Two Integers # Submitted by: Mohammad S Anwar # You are given a positive integer $N. # -# Write a script to find if it can be expressed as a ** b where a > 0 and b > 1. Print 1 if you succeed otherwise 0. +# Write a script to find if it can be expressed as a ** b where +# a > 0 and b > 1. Print 1 if you succeed otherwise 0. # # Example 1: # Input: 8 diff --git a/challenge-085/paulo-custodio/python/ch-1.py b/challenge-085/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..54f9e1cd1b --- /dev/null +++ b/challenge-085/paulo-custodio/python/ch-1.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 + +# Challenge 085 +# +# TASK #1 > Triplet Sum +# Submitted by: Mohammad S Anwar +# You are given an array of real numbers greater than zero. +# +# Write a script to find if there exists a triplet (a,b,c) such that +# 1 < a+b+c < 2. Print 1 if you succeed otherwise 0. +# +# Example 1: +# Input: @R = (1.2, 0.4, 0.1, 2.5) +# Output: 1 as 1 < 1.2 + 0.4 + 0.1 < 2 +# Example 2: +# Input: @R = (0.2, 1.5, 0.9, 1.1) +# Output: 0 +# Example 3: +# Input: @R = (0.5, 1.1, 0.3, 0.7) +# Output: 1 as 1 < 0.5 + 1.1 + 0.3 < 2 + +import sys +from itertools import combinations + +def check(nums): + for combin in combinations(nums, 3): + if 1.0 < sum(combin) < 2.0: + return True + return False + +print(1 if check([float(x) for x in sys.argv[1:]]) else 0) diff --git a/challenge-085/paulo-custodio/python/ch-2.py b/challenge-085/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..ef869c12f8 --- /dev/null +++ b/challenge-085/paulo-custodio/python/ch-2.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 + +# Challenge 085 +# +# TASK #2 > Power of Two Integers +# Submitted by: Mohammad S Anwar +# You are given a positive integer $N. +# +# Write a script to find if it can be expressed as a ** b where +# a > 0 and b > 1. Print 1 if you succeed otherwise 0. +# +# Example 1: +# Input: 8 +# Output: 1 as 8 = 2 ** 3 +# Example 2: +# Input: 15 +# Output: 0 +# Example 3: +# Input: 125 +# Output: 1 as 125 = 5 ** 3 + +import sys + +def get_prime_factors(n): + i = 2 + prime_factors = [] + while i*i <= n: + if n%i == 0: + prime_factors.append(i) + n //= i + else: + i += 1 + + if n>1: + prime_factors.append(n) + + return prime_factors + +def is_perfect_power(n): + factors = list(set(get_prime_factors(n))) + return len(factors)==1 + +print(1 if is_perfect_power(int(sys.argv[1])) else 0) diff --git a/challenge-085/paulo-custodio/t/test-1.yaml b/challenge-085/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..f382a0db0c --- /dev/null +++ b/challenge-085/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 1.2 0.4 0.1 2.5 + input: + output: 1 +- setup: + cleanup: + args: 0.2 1.5 0.9 1.1 + input: + output: 0 +- setup: + cleanup: + args: 0.5 1.1 0.3 0.7 + input: + output: 1 diff --git a/challenge-085/paulo-custodio/t/test-2.yaml b/challenge-085/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..1e3f9f6071 --- /dev/null +++ b/challenge-085/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 8 + input: + output: 1 +- setup: + cleanup: + args: 15 + input: + output: 0 +- setup: + cleanup: + args: 125 + input: + output: 1 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; -} diff --git a/challenge-086/paulo-custodio/Makefile b/challenge-086/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-086/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-086/paulo-custodio/perl/ch-1.pl b/challenge-086/paulo-custodio/perl/ch-1.pl index 9164d0b630..281cfd766c 100644 --- a/challenge-086/paulo-custodio/perl/ch-1.pl +++ b/challenge-086/paulo-custodio/perl/ch-1.pl @@ -1,8 +1,8 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # Challenge 086 -# TASK #1 › Pair Difference +# TASK #1 > Pair Difference # Submitted by: Mohammad S Anwar # You are given an array of integers @N and an integer $A. # diff --git a/challenge-086/paulo-custodio/perl/ch-2.pl b/challenge-086/paulo-custodio/perl/ch-2.pl index d13376b053..27f6626469 100644 --- a/challenge-086/paulo-custodio/perl/ch-2.pl +++ b/challenge-086/paulo-custodio/perl/ch-2.pl @@ -1,8 +1,8 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # Challenge 086 -# TASK #2 › Sudoku Puzzle +# TASK #2 > Sudoku Puzzle # Submitted by: Mohammad S Anwar # You are given Sudoku puzzle (9x9). # diff --git a/challenge-086/paulo-custodio/python/ch-1.py b/challenge-086/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..7f3af4aa62 --- /dev/null +++ b/challenge-086/paulo-custodio/python/ch-1.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 + +# Challenge 086 + +# TASK #1 > Pair Difference +# Submitted by: Mohammad S Anwar +# You are given an array of integers @N and an integer $A. +# +# Write a script to find find if there exists a pair of elements in the array whose difference is $A. +# +# Print 1 if exists otherwise 0. +# +# Example 1: +# Input: @N = (10, 8, 12, 15, 5) and $A = 7 +# Output: 1 as 15 - 8 = 7 +# Example 2: +# Input: @N = (1, 5, 2, 9, 7) and $A = 6 +# Output: 1 as 7 - 1 = 6 +# Example 3: +# Input: @N = (10, 30, 20, 50, 40) and $A = 15 +# Output: 0 + +import sys + +def found(dif, nums): + for i in range(len(nums)-1): + for j in range(i+1, len(nums)): + if abs(nums[i]-nums[j])==dif: + return True + return False + +nums = [int(x) for x in sys.argv[1:]] +dif = nums.pop(-1) +if found(dif, nums): + print(1) +else: + print(0) diff --git a/challenge-086/paulo-custodio/python/ch-2.py b/challenge-086/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..c0724735ca --- /dev/null +++ b/challenge-086/paulo-custodio/python/ch-2.py @@ -0,0 +1,122 @@ +#!/usr/bin/env python3 + +# Challenge 086 + +# TASK #2 > Sudoku Puzzle +# Submitted by: Mohammad S Anwar +# You are given Sudoku puzzle (9x9). +# +# Write a script to complete the puzzle and must respect the following rules: +# +# a) Each row must have the numbers 1-9 occurring just once. +# b) Each column must have the numbers 1-9 occurring just once. +# c) The numbers 1-9 must occur just once in each of the 9 sub-boxes (3x3) of the grid. +# Example: +# [ _ _ _ 2 6 _ 7 _ 1 ] +# [ 6 8 _ _ 7 _ _ 9 _ ] +# [ 1 9 _ _ _ 4 5 _ _ ] +# [ 8 2 _ 1 _ _ _ 4 _ ] +# [ _ _ 4 6 _ 2 9 _ _ ] +# [ _ 5 _ _ _ 3 _ 2 8 ] +# [ _ _ 9 3 _ _ _ 7 4 ] +# [ _ 4 _ _ 5 _ _ 3 6 ] +# [ 7 _ 3 _ 1 8 _ _ _ ] +# Output: +# [ 4 3 5 2 6 9 7 8 1 ] +# [ 6 8 2 5 7 1 4 9 3 ] +# [ 1 9 7 8 3 4 5 6 2 ] +# [ 8 2 6 1 9 5 3 4 7 ] +# [ 3 7 4 6 8 2 9 1 5 ] +# [ 9 5 1 7 4 3 6 2 8 ] +# [ 5 1 9 3 2 6 8 7 4 ] +# [ 2 4 8 9 5 7 1 3 6 ] +# [ 7 6 3 4 1 8 2 5 9 ] +# As the above puzzle respect the 3 rules including 9-sub-boxes as shown below: +# +# [ 4 3 5 ] [ 2 6 9 ] [ 7 8 1 ] +# [ 6 8 2 ] [ 5 7 1 ] [ 4 9 3 ] +# [ 1 9 7 ] [ 8 3 4 ] [ 5 6 2 ] +# +# [ 8 2 6 ] [ 1 9 5 ] [ 3 4 7 ] +# [ 3 7 4 ] [ 6 8 2 ] [ 9 1 5 ] +# [ 9 5 1 ] [ 7 4 3 ] [ 6 2 8 ] +# +# [ 5 1 9 ] [ 3 2 6 ] [ 8 7 4 ] +# [ 2 4 8 ] [ 9 5 7 ] [ 1 3 6 ] +# [ 7 6 3 ] [ 4 1 8 ] [ 2 5 9 ] + +import fileinput +import re +import copy + +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"_", "0", line) + line = re.sub(r"\D+", " ", line) + cols = [int(x) for x in line.split()] + m.append(cols) + return m + +# check no position violates the three rules +def check_constraints(m): + # a) Each row must have the numbers 1-9 occurring just once. + for c in range(len(m[0])): + found = set() + for r in range(len(m)): + v = m[r][c] + if v > 0 and v in found: + return False + else: + found.add(v) + + # b) Each column must have the numbers 1-9 occurring just once. + for r in range(len(m)): + found = set() + for c in range(len(m[0])): + v = m[r][c] + if v > 0 and v in found: + return False + else: + found.add(v) + + # c) The numbers 1-9 must occur just once in each of the 9 sub-boxes (3x3) of the grid. + for r0 in range(0, len(m), 3): + for c0 in range(0, len(m[0]), 3): + found = set() + for r in range(r0, r0+3): + for c in range(c0, c0+3): + v = m[r][c] + if v > 0 and v in found: + return False + else: + found.add(v) + + return True + +def solve(m): + if not check_constraints(m): + return + + for r in range(len(m)): + for c in range(len(m[0])): + if m[r][c]==0: + for v in range(1, 10): + mcpy = copy.deepcopy(m) + mcpy[r][c] = v + solve(mcpy) + return # trim the tree, we have tried 1..9 + + # all solved, output result + for row in m: + print("[ "+ " ".join([str(x) for x in row]) +" ]") + print("") + + +solve(read_matrix(read_input())) diff --git a/challenge-086/paulo-custodio/t/test-1.yaml b/challenge-086/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..7632a2a806 --- /dev/null +++ b/challenge-086/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 10 8 12 15 5 7 + input: + output: 1 +- setup: + cleanup: + args: 1 5 2 9 7 6 + input: + output: 1 +- setup: + cleanup: + args: 10 30 20 50 40 15 + input: + output: 0 diff --git a/challenge-086/paulo-custodio/t/test-2.yaml b/challenge-086/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..a082b670a7 --- /dev/null +++ b/challenge-086/paulo-custodio/t/test-2.yaml @@ -0,0 +1,24 @@ +- setup: + cleanup: + args: + input: | + [ _ _ _ 2 6 _ 7 _ 1 ] + [ 6 8 _ _ 7 _ _ 9 _ ] + [ 1 9 _ _ _ 4 5 _ _ ] + [ 8 2 _ 1 _ _ _ 4 _ ] + [ _ _ 4 6 _ 2 9 _ _ ] + [ _ 5 _ _ _ 3 _ 2 8 ] + [ _ _ 9 3 _ _ _ 7 4 ] + [ _ 4 _ _ 5 _ _ 3 6 ] + [ 7 _ 3 _ 1 8 _ _ _ ] + output: | + |[ 4 3 5 2 6 9 7 8 1 ] + |[ 6 8 2 5 7 1 4 9 3 ] + |[ 1 9 7 8 3 4 5 6 2 ] + |[ 8 2 6 1 9 5 3 4 7 ] + |[ 3 7 4 6 8 2 9 1 5 ] + |[ 9 5 1 7 4 3 6 2 8 ] + |[ 5 1 9 3 2 6 8 7 4 ] + |[ 2 4 8 9 5 7 1 3 6 ] + |[ 7 6 3 4 1 8 2 5 9 ] + | diff --git a/challenge-086/paulo-custodio/test.pl b/challenge-086/paulo-custodio/test.pl deleted file mode 100644 index 529f841c6c..0000000000 --- a/challenge-086/paulo-custodio/test.pl +++ /dev/null @@ -1,50 +0,0 @@ -#!/usr/bin/perl - -use Modern::Perl; -use Test::More; - -my $in = "in.txt"; - -is capture("perl perl/ch-1.pl 10 8 12 15 5 7"), "1\n"; -is capture("perl perl/ch-1.pl 1 5 2 9 7 6"), "1\n"; -is capture("perl perl/ch-1.pl 10 30 20 50 40 15"), "0\n"; - - -spew($in, <<END); -[ _ _ _ 2 6 _ 7 _ 1 ] -[ 6 8 _ _ 7 _ _ 9 _ ] -[ 1 9 _ _ _ 4 5 _ _ ] -[ 8 2 _ 1 _ _ _ 4 _ ] -[ _ _ 4 6 _ 2 9 _ _ ] -[ _ 5 _ _ _ 3 _ 2 8 ] -[ _ _ 9 3 _ _ _ 7 4 ] -[ _ 4 _ _ 5 _ _ 3 6 ] -[ 7 _ 3 _ 1 8 _ _ _ ] -END -is capture("perl perl/ch-2.pl <$in"), <<END; -[ 4 3 5 2 6 9 7 8 1 ] -[ |
