aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-01-14 20:18:51 +0000
committerGitHub <noreply@github.com>2021-01-14 20:18:51 +0000
commit4d434485244ff5c2b8c4fd2802b2001683c1bea0 (patch)
tree6f5cc09d0e22adfba1b237ea0dc0e0be40877267
parent5c5779bbfca6af92e529736ffe30d1c5efe0e49f (diff)
parente7504902c10014d38cb31f543a95176d2c79ba9d (diff)
downloadperlweeklychallenge-club-4d434485244ff5c2b8c4fd2802b2001683c1bea0.tar.gz
perlweeklychallenge-club-4d434485244ff5c2b8c4fd2802b2001683c1bea0.tar.bz2
perlweeklychallenge-club-4d434485244ff5c2b8c4fd2802b2001683c1bea0.zip
Merge pull request #3259 from pauloscustodio/011
011
-rw-r--r--challenge-011/paulo-custodio/README1
-rw-r--r--challenge-011/paulo-custodio/perl/ch-1.pl24
-rw-r--r--challenge-011/paulo-custodio/perl/ch-2.pl23
-rw-r--r--challenge-011/paulo-custodio/test.pl47
4 files changed, 95 insertions, 0 deletions
diff --git a/challenge-011/paulo-custodio/README b/challenge-011/paulo-custodio/README
new file mode 100644
index 0000000000..87dc0b2fbd
--- /dev/null
+++ b/challenge-011/paulo-custodio/README
@@ -0,0 +1 @@
+Solution by Paulo Custodio
diff --git a/challenge-011/paulo-custodio/perl/ch-1.pl b/challenge-011/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..39e0164ecc
--- /dev/null
+++ b/challenge-011/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,24 @@
+#!/usr/bin/env perl
+
+# Challenge 011
+#
+# Challenge #1
+# Write a script that computes the equal point in the Fahrenheit and Celsius
+# scales, knowing that the freezing point of water is 32 °F and 0 °C, and that
+# the boiling point of water is 212 °F and 100 °C. This challenge was proposed
+# by Laurent Rosenfeld.
+
+use strict;
+use warnings;
+use 5.030;
+
+use Math::Calculus::NewtonRaphson;
+
+my $exp = Math::Calculus::NewtonRaphson->new;
+
+# F = (C * 9/5) + 32; F = C = x
+$exp->addVariable('x');
+$exp->setExpression("(x * 9/5) + 32 - x");
+
+my $result = $exp->newtonRaphson('x', 0) or die $exp->getError;
+say $result;
diff --git a/challenge-011/paulo-custodio/perl/ch-2.pl b/challenge-011/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..bba012874f
--- /dev/null
+++ b/challenge-011/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,23 @@
+#!/usr/bin/env perl
+
+# Challenge 011
+#
+# Challenge #2
+# Write a script to create an Indentity Matrix for the given size. For example,
+# if the size is 4, then create Identity Matrix 4x4. For more information about
+# Indentity Matrix, please read the wiki page.
+
+use strict;
+use warnings;
+use 5.030;
+
+my $size = shift || 4;
+my @i;
+for my $r (0..$size-1) {
+ for my $c (0..$size-1) {
+ $i[$r][$c] = ($r==$c) ? 1 : 0;
+ }
+}
+
+# print matrix
+say "[", join("\n ", map {"[".join(", ", @{$i[$_]})."]"} 0..$size-1), "]";
diff --git a/challenge-011/paulo-custodio/test.pl b/challenge-011/paulo-custodio/test.pl
new file mode 100644
index 0000000000..4964296c8f
--- /dev/null
+++ b/challenge-011/paulo-custodio/test.pl
@@ -0,0 +1,47 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use 5.030;
+use Test::More;
+
+is capture("perl perl/ch-1.pl"), "-40\n";
+
+is capture("perl perl/ch-2.pl 1"), <<END;
+[[1]]
+END
+
+is capture("perl perl/ch-2.pl 2"), <<END;
+[[1, 0]
+ [0, 1]]
+END
+
+is capture("perl perl/ch-2.pl 4"), <<END;
+[[1, 0, 0, 0]
+ [0, 1, 0, 0]
+ [0, 0, 1, 0]
+ [0, 0, 0, 1]]
+END
+
+is capture("perl perl/ch-2.pl 10"), <<END;
+[[1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
+ [0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
+ [0, 0, 1, 0, 0, 0, 0, 0, 0, 0]
+ [0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
+ [0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
+ [0, 0, 0, 0, 0, 1, 0, 0, 0, 0]
+ [0, 0, 0, 0, 0, 0, 1, 0, 0, 0]
+ [0, 0, 0, 0, 0, 0, 0, 1, 0, 0]
+ [0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
+ [0, 0, 0, 0, 0, 0, 0, 0, 0, 1]]
+END
+
+done_testing;
+
+sub capture {
+ my($cmd) = @_;
+ my $out = `$cmd`;
+ $out =~ s/[ \r\t]*\n/\n/g;
+ return $out;
+}
+