aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-12-22 22:48:01 +0000
committerGitHub <noreply@github.com>2020-12-22 22:48:01 +0000
commita38d86fd7aa92bccda8be9d209b413f515fd052e (patch)
treefad06710408e205a04978173b0b7eff2157d03b1
parentab403fff28ef9b8fa8d878e8fc8260157a8a0f32 (diff)
parentfa3f9df97e5e46ef32a5d7207e7b578cfebd2158 (diff)
downloadperlweeklychallenge-club-a38d86fd7aa92bccda8be9d209b413f515fd052e.tar.gz
perlweeklychallenge-club-a38d86fd7aa92bccda8be9d209b413f515fd052e.tar.bz2
perlweeklychallenge-club-a38d86fd7aa92bccda8be9d209b413f515fd052e.zip
Merge pull request #3045 from pauloscustodio/basic
Add FreeBASIC solution to challenge-089
-rw-r--r--challenge-089/paulo-custodio/basic/ch-1.bas29
-rw-r--r--challenge-089/paulo-custodio/basic/ch-2.bas80
-rw-r--r--challenge-089/paulo-custodio/test.pl31
3 files changed, 132 insertions, 8 deletions
diff --git a/challenge-089/paulo-custodio/basic/ch-1.bas b/challenge-089/paulo-custodio/basic/ch-1.bas
new file mode 100644
index 0000000000..43b426771a
--- /dev/null
+++ b/challenge-089/paulo-custodio/basic/ch-1.bas
@@ -0,0 +1,29 @@
+' 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.
+
+Function gcd(a as Long, b as Long) as Long
+ If a=0 Then
+ gcd = b
+ Else
+ gcd = gcd(b Mod a, a)
+ EndIf
+End Function
+
+Function sum_gcd(n as Long) as Long
+ Dim sum as Long, a as Long, b as Long
+ For a=1 to n-1
+ For b=a+1 to n
+ sum = sum + gcd(a,b)
+ Next b
+ Next a
+ sum_gcd = sum
+End Function
+
+dim n as Long
+n = Val(Command(1))
+Print sum_gcd(n)
diff --git a/challenge-089/paulo-custodio/basic/ch-2.bas b/challenge-089/paulo-custodio/basic/ch-2.bas
new file mode 100644
index 0000000000..982caaa463
--- /dev/null
+++ b/challenge-089/paulo-custodio/basic/ch-2.bas
@@ -0,0 +1,80 @@
+' 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
+
+' Brute force solution in Basic
+
+Sub solve()
+ Dim a as Integer, b as Integer, c as Integer, d as Integer
+ Dim e as Integer, f as Integer, g as Integer, h as Integer, i as Integer
+ For a=1 To 9
+ For b=1 To 9
+ If b<>a Then
+ For c=1 To 9
+ If c<>a AndAlso c<>b Then
+ For d=1 To 9
+ If d<>a AndAlso d<>b AndAlso d<>c Then
+ For e=1 To 9
+ If e<>a AndAlso e<>b AndAlso e<>c AndAlso e<>d Then
+ For f=1 To 9
+ If f<>a AndAlso f<>b AndAlso f<>c AndAlso f<>d AndAlso f<>e Then
+ For g=1 To 9
+ If g<>a AndAlso g<>b AndAlso g<>c AndAlso g<>d AndAlso _
+ g<>e AndAlso g<>f Then
+ For h=1 To 9
+ If h<>a AndAlso h<>b AndAlso h<>c AndAlso h<>d AndAlso _
+ h<>e AndAlso h<>f AndAlso h<>g Then
+ For i=1 To 9
+ If i<>a AndAlso i<>b AndAlso i<>c AndAlso _
+ i<>d AndAlso i<>e AndAlso i<>f AndAlso _
+ i<>g AndAlso i<>h Then
+ If a + b + c = 15 AndAlso _
+ d + e + f = 15 AndAlso _
+ g + h + i = 15 AndAlso _
+ a + d + g = 15 AndAlso _
+ b + e + h = 15 AndAlso _
+ c + f + i = 15 AndAlso _
+ a + e + i = 15 AndAlso _
+ c + e + g = 15 Then
+ Print "[";a;b;c;" ]"
+ Print "[";d;e;f;" ]"
+ Print "[";g;h;i;" ]"
+ Exit Sub
+ EndIf
+ EndIf
+ Next i
+ EndIf
+ Next h
+ EndIf
+ Next g
+ EndIf
+ Next f
+ EndIf
+ Next e
+ EndIf
+ Next d
+ EndIf
+ Next c
+ EndIf
+ Next b
+ Next a
+End Sub
+
+solve
diff --git a/challenge-089/paulo-custodio/test.pl b/challenge-089/paulo-custodio/test.pl
index b51643c027..71a46a5364 100644
--- a/challenge-089/paulo-custodio/test.pl
+++ b/challenge-089/paulo-custodio/test.pl
@@ -6,24 +6,39 @@ use Test::More;
# task 1
-is `perl perl/ch-1.pl 3`, "3\n";
-is `perl perl/ch-1.pl 4`, "7\n";
-
-is `gforth -e 3 forth/ch-1.fs`, "3 \n";
-is `gforth -e 4 forth/ch-1.fs`, "7 \n";
+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
-is `perl perl/ch-2.pl`, <<END;
+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
-is `gforth forth/ch-2.fs`, <<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 $_;
+}