diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-06-24 20:07:39 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-06-24 20:07:39 +0100 |
| commit | 3f3d45004b145f2b8bc15617e0cbce80142ccf5b (patch) | |
| tree | 4bd9330e828797e60c8d2b287c9643734562e0da | |
| parent | 786579fc8f23db25a7c8de1b2d420cf357d25c7b (diff) | |
| download | perlweeklychallenge-club-3f3d45004b145f2b8bc15617e0cbce80142ccf5b.tar.gz perlweeklychallenge-club-3f3d45004b145f2b8bc15617e0cbce80142ccf5b.tar.bz2 perlweeklychallenge-club-3f3d45004b145f2b8bc15617e0cbce80142ccf5b.zip | |
Add bc solution to challenge 011
| -rw-r--r-- | challenge-011/paulo-custodio/bc/ch-1.bc | 39 | ||||
| -rw-r--r-- | challenge-011/paulo-custodio/t/test-1.yaml | 5 | ||||
| -rw-r--r-- | challenge-011/paulo-custodio/t/test-2.yaml | 37 | ||||
| -rw-r--r-- | challenge-011/paulo-custodio/test.pl | 48 |
4 files changed, 84 insertions, 45 deletions
diff --git a/challenge-011/paulo-custodio/bc/ch-1.bc b/challenge-011/paulo-custodio/bc/ch-1.bc new file mode 100644 index 0000000000..c8e8ea3a1c --- /dev/null +++ b/challenge-011/paulo-custodio/bc/ch-1.bc @@ -0,0 +1,39 @@ +#!/usr/bin/bc -ql + +/* +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. +*/ + +scale = 10 + +/* +F = (C * 9/5) + 32 +F = C = x + => f(x) = (x * 9/5) + 32 - x = 0 +<=> f(x) = (9/5 - 1) * x + 32 + f'(x) = 9/5 - 1 +*/ + +define f(x) { return (9/5 - 1) * x + 32 } +define df(x) { return 9/5 - 1 } + +define newton(x0) { + auto x1, tmp + x1 = x0+1 + while (x0 != x1) { + x1 = x0 - f(x0)/df(x0) + tmp = x1; x1 = x0; x0 = tmp + } + return x0 +} + +t = newton(0) +scale = 0 +t/1 +quit diff --git a/challenge-011/paulo-custodio/t/test-1.yaml b/challenge-011/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..5ea8bd975e --- /dev/null +++ b/challenge-011/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: + input: + output: -40 diff --git a/challenge-011/paulo-custodio/t/test-2.yaml b/challenge-011/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..3d3d7b7bb3 --- /dev/null +++ b/challenge-011/paulo-custodio/t/test-2.yaml @@ -0,0 +1,37 @@ +- setup: + cleanup: + args: 1 + input: + output: | + |[[1]] +- setup: + cleanup: + args: 2 + input: + output: | + |[[1, 0] + | [0, 1]] +- setup: + cleanup: + args: 4 + input: + output: | + |[[1, 0, 0, 0] + | [0, 1, 0, 0] + | [0, 0, 1, 0] + | [0, 0, 0, 1]] +- setup: + cleanup: + args: 10 + input: + output: | + |[[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]] diff --git a/challenge-011/paulo-custodio/test.pl b/challenge-011/paulo-custodio/test.pl index 68b96b8cf7..ba6c37260b 100644 --- a/challenge-011/paulo-custodio/test.pl +++ b/challenge-011/paulo-custodio/test.pl @@ -1,46 +1,4 @@ -#!/usr/bin/perl - -use strict; -use warnings; -use 5.030; +#!/usr/bin/env perl +use Modern::Perl; 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; -} +require '../../challenge-001/paulo-custodio/test.pl'; |
