diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-11-11 07:35:15 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-11 07:35:15 +0000 |
| commit | 63c89ec61dbd7965ab5403340dae7fe36441592c (patch) | |
| tree | 6ca3595039738085990df748983d788becf2cb8a | |
| parent | e5bbd306db034e234914b04c593073363c6c4198 (diff) | |
| parent | e6984ea881fc8dbe31017c60ba4f8af9ddab8329 (diff) | |
| download | perlweeklychallenge-club-63c89ec61dbd7965ab5403340dae7fe36441592c.tar.gz perlweeklychallenge-club-63c89ec61dbd7965ab5403340dae7fe36441592c.tar.bz2 perlweeklychallenge-club-63c89ec61dbd7965ab5403340dae7fe36441592c.zip | |
Merge pull request #5195 from pauloscustodio/devel
Add Python solution to challenge 89
| -rw-r--r-- | challenge-088/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-088/paulo-custodio/perl/ch-1.pl | 2 | ||||
| -rw-r--r-- | challenge-088/paulo-custodio/perl/ch-2.pl | 2 | ||||
| -rw-r--r-- | challenge-088/paulo-custodio/python/ch-1.py | 45 | ||||
| -rw-r--r-- | challenge-088/paulo-custodio/python/ch-2.py | 70 | ||||
| -rw-r--r-- | challenge-088/paulo-custodio/t/test-1.yaml | 20 | ||||
| -rw-r--r-- | challenge-088/paulo-custodio/t/test-2.yaml | 50 | ||||
| -rw-r--r-- | challenge-088/paulo-custodio/test.pl | 66 | ||||
| -rw-r--r-- | challenge-089/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-089/paulo-custodio/forth/ch-1.fs | 4 | ||||
| -rw-r--r-- | challenge-089/paulo-custodio/perl/ch-1.pl | 2 | ||||
| -rw-r--r-- | challenge-089/paulo-custodio/perl/ch-2.pl | 8 | ||||
| -rw-r--r-- | challenge-089/paulo-custodio/python/ch-1.py | 28 | ||||
| -rw-r--r-- | challenge-089/paulo-custodio/python/ch-2.py | 59 | ||||
| -rw-r--r-- | challenge-089/paulo-custodio/t/test-1.yaml | 10 | ||||
| -rw-r--r-- | challenge-089/paulo-custodio/t/test-2.yaml | 8 | ||||
| -rw-r--r-- | challenge-089/paulo-custodio/test.pl | 44 |
17 files changed, 302 insertions, 120 deletions
diff --git a/challenge-088/paulo-custodio/Makefile b/challenge-088/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-088/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-088/paulo-custodio/perl/ch-1.pl b/challenge-088/paulo-custodio/perl/ch-1.pl index d6dfe94e73..bcbfc0a151 100644 --- a/challenge-088/paulo-custodio/perl/ch-1.pl +++ b/challenge-088/paulo-custodio/perl/ch-1.pl @@ -1,4 +1,4 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # Challenge 088 # diff --git a/challenge-088/paulo-custodio/perl/ch-2.pl b/challenge-088/paulo-custodio/perl/ch-2.pl index af4b0c2c28..deb0502b22 100644 --- a/challenge-088/paulo-custodio/perl/ch-2.pl +++ b/challenge-088/paulo-custodio/perl/ch-2.pl @@ -1,4 +1,4 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # Challenge 088 # diff --git a/challenge-088/paulo-custodio/python/ch-1.py b/challenge-088/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..c63ae8f4be --- /dev/null +++ b/challenge-088/paulo-custodio/python/ch-1.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 + +# Challenge 088 +# +# TASK #1 > Array of Product +# Submitted by: Mohammad S Anwar +# You are given an array of positive integers @N. +# +# Write a script to return an array @M where $M[i] is the product of all elements +# of @N except the index $N[i]. +# +# Example 1: +# Input: +# @N = (5, 2, 1, 4, 3) +# Output: +# @M = (24, 60, 120, 30, 40) +# +# $M[0] = 2 x 1 x 4 x 3 = 24 +# $M[1] = 5 x 1 x 4 x 3 = 60 +# $M[2] = 5 x 2 x 4 x 3 = 120 +# $M[3] = 5 x 2 x 1 x 3 = 30 +# $M[4] = 5 x 2 x 1 x 4 = 40 +# Example 2: +# Input: +# @N = (2, 1, 4, 3) +# Output: +# @M = (12, 24, 6, 8) +# +# $M[0] = 1 x 4 x 3 = 12 +# $M[1] = 2 x 4 x 3 = 24 +# $M[2] = 2 x 1 x 3 = 6 +# $M[3] = 2 x 1 x 4 = 8 + +import sys + +def array_product(n): + m = [1 for x in n] # initialize the products to 1 + for i in range(len(n)): + for j in range(len(m)): + if i!=j: + m[j] *= n[i] + return m + +m = array_product([int(x) for x in sys.argv[1:]]) +print("("+ ", ".join([str(x) for x in m]) +")") diff --git a/challenge-088/paulo-custodio/python/ch-2.py b/challenge-088/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..c2a06ba585 --- /dev/null +++ b/challenge-088/paulo-custodio/python/ch-2.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 + +# Challenge 088 +# +# TASK #2 > Spiral Matrix +# Submitted by: Mohammad S Anwar +# You are given m x n matrix of positive integers. +# +# Write a script to print spiral matrix as list. +# +# Example 1: +# Input: +# [ 1, 2, 3 ] +# [ 4, 5, 6 ] +# [ 7, 8, 9 ] +# Ouput: +# [ 1, 2, 3, 6, 9, 8, 7, 4, 5 ] +# Example 2: +# Input: +# [ 1, 2, 3, 4 ] +# [ 5, 6, 7, 8 ] +# [ 9, 10, 11, 12 ] +# [ 13, 14, 15, 16 ] +# Output: +# [ 1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10 ] + +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 spiral(m): + s = [] + while len(m) > 0: + # put top row left-right + for n in m[0]: + s.append(n) + m.pop(0) + + # put right column top-bottom + if len(m) > 0 and len(m[0]) > 0: + for r in range(len(m)): + s.append(m[r].pop(-1)) + + # put bottom row right-left + if len(m) > 0: + for n in m[-1][::-1]: + s.append(n) + m.pop(-1) + + # put left column top-bottom + if len(m) > 0 and len(m[-1]) > 0: + for r in range(len(m))[::-1]: + s.append(m[r].pop(0)) + return s + +s = spiral(read_matrix(read_input())) +print("[ "+ ", ".join([str(x) for x in s]) +" ]") diff --git a/challenge-088/paulo-custodio/t/test-1.yaml b/challenge-088/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..c22655bdeb --- /dev/null +++ b/challenge-088/paulo-custodio/t/test-1.yaml @@ -0,0 +1,20 @@ +- setup: + cleanup: + args: 5 2 1 4 3 + input: + output: (24, 60, 120, 30, 40) +- setup: + cleanup: + args: 2 1 4 3 + input: + output: (12, 24, 6, 8) +- setup: + cleanup: + args: 0 1 4 3 + input: + output: (12, 0, 0, 0) +- setup: + cleanup: + args: 1 1 1 1 + input: + output: (1, 1, 1, 1) diff --git a/challenge-088/paulo-custodio/t/test-2.yaml b/challenge-088/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..f9083e28a7 --- /dev/null +++ b/challenge-088/paulo-custodio/t/test-2.yaml @@ -0,0 +1,50 @@ +- setup: + cleanup: + args: + input: | + [ 5 ] + output: | + [ 5 ] +- setup: + cleanup: + args: + input: | + [ 1, 2, 3 ] + output: | + [ 1, 2, 3 ] +- setup: + cleanup: + args: + input: | + [ 1, 2, 3 ] + [ 7, 8, 9 ] + output: | + [ 1, 2, 3, 9, 8, 7 ] +- setup: + cleanup: + args: + input: | + [ 1 ] + [ 4 ] + [ 7 ] + output: | + [ 1, 4, 7 ] +- setup: + cleanup: + args: + input: | + [ 1, 2, 3 ] + [ 4, 5, 6 ] + [ 7, 8, 9 ] + output: | + [ 1, 2, 3, 6, 9, 8, 7, 4, 5 ] +- setup: + cleanup: + args: + input: | + [ 1, 2, 3, 4 ] + [ 5, 6, 7, 8 ] + [ 9, 10, 11, 12 ] + [ 13, 14, 15, 16 ] + output: | + [ 1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10 ] diff --git a/challenge-088/paulo-custodio/test.pl b/challenge-088/paulo-custodio/test.pl deleted file mode 100644 index e2a07e0555..0000000000 --- a/challenge-088/paulo-custodio/test.pl +++ /dev/null @@ -1,66 +0,0 @@ -#!/usr/bin/perl - -use Modern::Perl; -use Test::More; - -my $in = "in.txt"; - -is capture("perl perl/ch-1.pl 5 2 1 4 3"), "(24, 60, 120, 30, 40)\n"; -is capture("perl perl/ch-1.pl 2 1 4 3 "), "(12, 24, 6, 8)\n"; -is capture("perl perl/ch-1.pl 0 1 4 3 "), "(12, 0, 0, 0)\n"; -is capture("perl perl/ch-1.pl 1 1 1 1 "), "(1, 1, 1, 1)\n"; - -spew($in, <<END); -[ 5 ] -END -is capture("perl perl/ch-2.pl <$in"), "[ 5 ]\n"; - -spew($in, <<END); -[ 1, 2, 3 ] -END -is capture("perl perl/ch-2.pl <$in"), "[ 1, 2, 3 ]\n"; - -spew($in, <<END); -[ 1, 2, 3 ] -[ 7, 8, 9 ] -END -is capture("perl perl/ch-2.pl <$in"), "[ 1, 2, 3, 9, 8, 7 ]\n"; - -spew($in, <<END); -[ 1 ] -[ 4 ] -[ 7 ] -END -is capture("perl perl/ch-2.pl <$in"), "[ 1, 4, 7 ]\n"; - -spew($in, <<END); -[ 1, 2, 3 ] -[ 4, 5, 6 ] -[ 7, 8, 9 ] -END -is capture("perl perl/ch-2.pl <$in"), "[ 1, 2, 3, 6, 9, 8, 7, 4, 5 ]\n"; - -spew($in, <<END); -[ 1, 2, 3, 4 ] -[ 5, 6, 7, 8 ] -[ 9, 10, 11, 12 ] -[ 13, 14, 15, 16 ] -END -is capture("perl perl/ch-2.pl <$in"), - "[ 1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10 ]\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-089/paulo-custodio/Makefile b/challenge-089/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-089/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-089/paulo-custodio/forth/ch-1.fs b/challenge-089/paulo-custodio/forth/ch-1.fs index 4814b6d1e9..b695281395 100644 --- a/challenge-089/paulo-custodio/forth/ch-1.fs +++ b/challenge-089/paulo-custodio/forth/ch-1.fs @@ -7,9 +7,6 @@ \ Write a script to sum GCD of all possible unique pairs between 1 and $N. \ \ This solution uses a recursive algorithm to compute the GCD. -\ -\ Start the script with N in the stack, i.e. -\ gforth -e 12345 ch-1.pl : gcd { a b -- gcd } a 0= IF @@ -28,4 +25,5 @@ LOOP ; +NEXT-ARG S>NUMBER? 0= THROW DROP sum_gcd . CR BYE diff --git a/challenge-089/paulo-custodio/perl/ch-1.pl b/challenge-089/paulo-custodio/perl/ch-1.pl index 7a7a1d490d..0e5fe68e05 100644 --- a/challenge-089/paulo-custodio/perl/ch-1.pl +++ b/challenge-089/paulo-custodio/perl/ch-1.pl @@ -1,4 +1,4 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # Challenge 089 diff --git a/challenge-089/paulo-custodio/perl/ch-2.pl b/challenge-089/paulo-custodio/perl/ch-2.pl index 13f0fb5c8f..03adbe1366 100644 --- a/challenge-089/paulo-custodio/perl/ch-2.pl +++ b/challenge-089/paulo-custodio/perl/ch-2.pl @@ -1,4 +1,4 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # Challenge 089 @@ -46,9 +46,9 @@ use warnings; sub square { my($a, $b, $c) = @_; my @sq; - push @sq, [ $c - $b, $c + ($a + $b), $c - $a ]; - push @sq, [ $c - ($a - $b), $c, $c + ($a - $b) ]; - push @sq, [ $c + $a, $c - ($a + $b), $c + $b ]; + push @sq, [ $c - $b, $c - ($a - $b), $c + $a ]; + push @sq, [ $c + ($a + $b), $c, $c - ($a + $b) ]; + push @sq, [ $c - $a, $c + ($a - $b), $c + $b ]; return @sq; } diff --git a/challenge-089/paulo-custodio/python/ch-1.py b/challenge-089/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..7b5cc6d4f9 --- /dev/null +++ b/challenge-089/paulo-custodio/python/ch-1.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 + +# Challenge 089 + +# TASK #1 > GCD Sum +# Submitted by: Mohammad S Anwar +# You are given a positive integer $N. +# +# Write a script to sum GCD of all possible unique pairs between 1 and $N. + +# This solution uses a recursive algorithm to compute the GCD. + +import sys + +def gcd(a, b): + if a==0: + return b + else: + return gcd(b%a, a) + +def sum_gcd(n): + sum = 0 + for a in range(1, n): + for b in range(a+1, n+1): + sum += gcd(a, b) + return sum + +print(sum_gcd(int(sys.argv[1]))) diff --git a/challenge-089/paulo-custodio/python/ch-2.py b/challenge-089/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..95dadc9d10 --- /dev/null +++ b/challenge-089/paulo-custodio/python/ch-2.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python3 + +# Challenge 089 + +# TASK #2 > Magical Matrix +# Submitted by: Mohammad S Anwar +# Write a script to display matrix as below with numbers 1 - 9. +# Please make sure numbers are used once. +# +# [ a b c ] +# [ d e f ] +# [ g h i ] +# So that it satisfies the following: +# +# a + b + c = 15 +# d + e + f = 15 +# g + h + i = 15 +# a + d + g = 15 +# b + e + h = 15 +# c + f + i = 15 +# a + e + i = 15 +# c + e + g = 15 + +# Solution based on Edouard Lucas (https://en.wikipedia.org/wiki/Magic_square#A_method_for_constructing_a_magic_square_of_order_3) +# +# Solution is: +# +# [ c - b | c + (a + b) | c - a ] +# [ c - (a - b) | c | c + (a - b) ] +# [ c + a | c - (a + b) | c + b ] +# +# The magic constant is 3c, where 0 < a < b < (c - a) and b != 2a. +# Moreover, every 3x3 magic square of distinct positive integers is of this form. +# +# In the problem statement it is said that the sum of each row, column and +# diagonal is 15, this is the square magic constant, therefore we know that: +# +# c = 5 +# +# We need to find the pairs (a,b) that satisfy the above conditions. + +# find a and b for a given c +def find_ab(c): + for a in range(1, 3*c-1+1): + for b in range(a+1, 3*c+1): + if b < c-a and b != 2*a: + return a, b + +def square(a, b, c): + return [[ c - b, c - (a - b), c + a ], + [ c + (a + b), c, c - (a + b) ], + [ c - a, c + (a - b), c + b ]] + +magic_constant = 15 +c = int(magic_constant/3) +a, b = find_ab(c) +sq = square(a,b,c) +for row in (sq): + print("[ "+ " ".join([str(x) for x in row]) +" ]") diff --git a/challenge-089/paulo-custodio/t/test-1.yaml b/challenge-089/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..ba9c8cbcb3 --- /dev/null +++ b/challenge-089/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 3 + input: + output: 3 +- setup: + cleanup: + args: 4 + input: + output: 7 diff --git a/challenge-089/paulo-custodio/t/test-2.yaml b/challenge-089/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..3c1154ac88 --- /dev/null +++ b/challenge-089/paulo-custodio/t/test-2.yaml @@ -0,0 +1,8 @@ +- setup: + cleanup: + args: + input: + output: | + [ 2 7 6 ] + [ 9 5 1 ] + [ 4 3 8 ] diff --git a/challenge-089/paulo-custodio/test.pl b/challenge-089/paulo-custodio/test.pl deleted file mode 100644 index 043bbc070d..0000000000 --- a/challenge-089/paulo-custodio/test.pl +++ /dev/null @@ -1,44 +0,0 @@ -#!/usr/bin/perl - -use strict; -use warnings; -use Test::More; - -# task 1 - -ok system("fbc basic/ch-1.bas") == 0; -for ([3,3], [4,7]) { - my($in, $out) = @$_; - is capture("basic/ch-1 $in"), " $out\n"; - is capture("perl perl/ch-1.pl $in"), "$out\n"; - is capture("gforth -e $in forth/ch-1.fs"), "$out\n"; -} - -# task 2 - -ok system("fbc basic/ch-2.bas") == 0; - -is capture("perl perl/ch-2.pl"), <<END; -[ 2 9 4 ] -[ 7 5 3 ] -[ 6 1 8 ] -END - -for ("basic/ch-2", - "gforth forth/ch-2.fs") { - is capture($_), <<END; -[ 2 7 6 ] -[ 9 5 1 ] -[ 4 3 8 ] -END -} - -done_testing; - -# capture output, normalize eol -sub capture { - my($cmd) = @_; - local $_ = `$cmd`; - s/[ \t\r]*\n/\n/g; - return $_; -} |
