diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-11-10 14:24:40 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-11-10 14:25:38 +0000 |
| commit | d83f91b866be692bee5eb46cf0252eeebb4634ef (patch) | |
| tree | 6e0d9f9ed0d77d53096e238c1dbed2fd58450c84 | |
| parent | a77b120e0df6002c36515ea79cb84c801a18c88a (diff) | |
| download | perlweeklychallenge-club-d83f91b866be692bee5eb46cf0252eeebb4634ef.tar.gz perlweeklychallenge-club-d83f91b866be692bee5eb46cf0252eeebb4634ef.tar.bz2 perlweeklychallenge-club-d83f91b866be692bee5eb46cf0252eeebb4634ef.zip | |
Add Python solution to challenge 89
| -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 |
9 files changed, 113 insertions, 52 deletions
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 $_; -} |
