diff options
| -rw-r--r-- | challenge-086/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-086/paulo-custodio/perl/ch-1.pl | 4 | ||||
| -rw-r--r-- | challenge-086/paulo-custodio/perl/ch-2.pl | 4 | ||||
| -rw-r--r-- | challenge-086/paulo-custodio/python/ch-1.py | 37 | ||||
| -rw-r--r-- | challenge-086/paulo-custodio/python/ch-2.py | 122 | ||||
| -rw-r--r-- | challenge-086/paulo-custodio/t/test-1.yaml | 15 | ||||
| -rw-r--r-- | challenge-086/paulo-custodio/t/test-2.yaml | 24 | ||||
| -rw-r--r-- | challenge-086/paulo-custodio/test.pl | 50 |
8 files changed, 204 insertions, 54 deletions
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 ] -[ 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 ] -END - -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; -} |
