diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-06-20 19:04:25 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-06-20 19:04:25 +0100 |
| commit | 1769cc8300770514d3bb92317d7f3368711e45fa (patch) | |
| tree | 09b9efc8c7a45825bcd3d94b1ee88bee4402eacc | |
| parent | 35abb3405622b757e8218ec2ab4421de253cb1c1 (diff) | |
| parent | abff22fe18e53d4292497b8e84bbeab0ddd54e78 (diff) | |
| download | perlweeklychallenge-club-1769cc8300770514d3bb92317d7f3368711e45fa.tar.gz perlweeklychallenge-club-1769cc8300770514d3bb92317d7f3368711e45fa.tar.bz2 perlweeklychallenge-club-1769cc8300770514d3bb92317d7f3368711e45fa.zip | |
Merge pull request #4295 from pauloscustodio/paulo-custodio
Paulo custodio
40 files changed, 262 insertions, 190 deletions
diff --git a/challenge-001/paulo-custodio/test.pl b/challenge-001/paulo-custodio/test.pl index bc1f0a9a13..a8a46745f3 100644 --- a/challenge-001/paulo-custodio/test.pl +++ b/challenge-001/paulo-custodio/test.pl @@ -141,11 +141,11 @@ sub build { return "bc -lq $prog"; } if (/^c$/) { - run("gcc $prog -o $prog_wo_ext") if (!-f $exe || -M $exe > -M $prog); + run("gcc $prog -o $prog_wo_ext -lmpfr -lgmp") if (!-f $exe || -M $exe > -M $prog); return $exe; } if (/^cpp$/) { - run("g++ $prog -o $prog_wo_ext") if (!-f $exe || -M $exe > -M $prog); + run("g++ $prog -o $prog_wo_ext -lmpfr -lgmpxx -lgmp") if (!-f $exe || -M $exe > -M $prog); return $exe; } if (/^d$/) { diff --git a/challenge-002/paulo-custodio/test.pl b/challenge-002/paulo-custodio/test.pl index a61c28ebb7..ba6c37260b 100644 --- a/challenge-002/paulo-custodio/test.pl +++ b/challenge-002/paulo-custodio/test.pl @@ -1,8 +1,4 @@ -#!/usr/bin/perl - -use strict; -use warnings; +#!/usr/bin/env perl +use Modern::Perl; use Test::More; -use 5.030; - require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-003/paulo-custodio/test.pl b/challenge-003/paulo-custodio/test.pl index a61c28ebb7..ba6c37260b 100644 --- a/challenge-003/paulo-custodio/test.pl +++ b/challenge-003/paulo-custodio/test.pl @@ -1,8 +1,4 @@ -#!/usr/bin/perl - -use strict; -use warnings; +#!/usr/bin/env perl +use Modern::Perl; use Test::More; -use 5.030; - require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-004/paulo-custodio/test.pl b/challenge-004/paulo-custodio/test.pl index 921572d853..ba6c37260b 100755 --- a/challenge-004/paulo-custodio/test.pl +++ b/challenge-004/paulo-custodio/test.pl @@ -1,6 +1,4 @@ #!/usr/bin/env perl - use Modern::Perl; use Test::More; - require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-005/paulo-custodio/test.pl b/challenge-005/paulo-custodio/test.pl index a61c28ebb7..ba6c37260b 100644 --- a/challenge-005/paulo-custodio/test.pl +++ b/challenge-005/paulo-custodio/test.pl @@ -1,8 +1,4 @@ -#!/usr/bin/perl - -use strict; -use warnings; +#!/usr/bin/env perl +use Modern::Perl; use Test::More; -use 5.030; - require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-006/paulo-custodio/c/ch-1.c b/challenge-006/paulo-custodio/c/ch-1.c new file mode 100644 index 0000000000..06d7e14737 --- /dev/null +++ b/challenge-006/paulo-custodio/c/ch-1.c @@ -0,0 +1,54 @@ +/* +Challenge 006 + +Challenge #1 +Create a script which takes a list of numbers from command line and print the +same in the compact form. For example, if you pass "1,2,3,4,9,10,14,15,16" +then it should print the compact form like "1-4,9,10,14-16". +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +int* nums, nr_nums = 0; + +void* check_mem(void* p) { + if (!p) { + fputs("Out of memory", stderr); + exit(EXIT_FAILURE); + } + return p; +} + +void parse_nums(char* str) { + char* num_str = strtok(str, ","); + while (num_str != NULL) { + int num = atoi(num_str); + nums = check_mem(realloc(nums, ++nr_nums * sizeof(int))); + nums[nr_nums - 1] = num; + + num_str = strtok(NULL, ","); + } +} + +int main(int argc, char* argv[]) { + if (argc != 2) return EXIT_FAILURE; + parse_nums(argv[1]); + for (int i = 0; i < nr_nums; i++) { + printf("%d", nums[i]); + if (i + 2 < nr_nums && + nums[i + 1] == nums[i] + 1 && + nums[i + 2] == nums[i] + 2) { + int j = 0; + while (i + j < nr_nums && nums[i + j] == nums[i] + j) + j++; + i += j - 1; + printf("-%d", nums[i]); + } + if (i + 1 < nr_nums) + printf(","); + } + printf("\n"); + free(nums); +} diff --git a/challenge-006/paulo-custodio/c/ch-2.c b/challenge-006/paulo-custodio/c/ch-2.c new file mode 100644 index 0000000000..3b83f12cc5 --- /dev/null +++ b/challenge-006/paulo-custodio/c/ch-2.c @@ -0,0 +1,33 @@ +/* +Challenge 006 + +Challenge #2 +Create a script to calculate Ramanujan's constant with at least 32 digits of +precision. Find out more about it here. + +The standard IEEE 754 double-precision binary floating-point format: binary64 +gives only 15 to 17 significant decimal digits +*/ + +#include <stdio.h> +#include <gmp.h> +#include <mpfr.h> + +int main() { + mpfr_set_default_prec(256); // 107 bits needed to represent 32 decimal digits + + mpfr_t pi, e, k; + mpfr_inits(pi, e, k, NULL); + + mpfr_const_pi(pi, MPFR_RNDN); + + mpfr_set_str(e, "163", 10, MPFR_RNDN); // e = 163 + mpfr_sqrt(e, e, MPFR_RNDN); // e = sqr(163) + mpfr_mul(e, pi, e, MPFR_RNDN); // e = pi*sqr(163) + + mpfr_exp(k, e, MPFR_RNDN); // k = e^(pi*sqr(163)) + + mpfr_printf("%32.12Rf\n", k); // 18.12 = 32 precision + + mpfr_clears(pi, e, k, NULL); +} diff --git a/challenge-006/paulo-custodio/cpp/ch-1.cpp b/challenge-006/paulo-custodio/cpp/ch-1.cpp new file mode 100644 index 0000000000..314584f1f9 --- /dev/null +++ b/challenge-006/paulo-custodio/cpp/ch-1.cpp @@ -0,0 +1,45 @@ +/* +Challenge 006 + +Challenge #1 +Create a script which takes a list of numbers from command line and print the +same in the compact form. For example, if you pass "1,2,3,4,9,10,14,15,16" +then it should print the compact form like "1-4,9,10,14-16". +*/ + +#include <iostream> +#include <sstream> +#include <string> +#include <vector> +using namespace std; + +vector<int> nums; + +void parse_nums(const string& str) { + istringstream iss(str); + string num_str; + while (getline(iss, num_str, ',')) { + int num = atoi(num_str.c_str()); + nums.push_back(num); + } +} + +int main(int argc, char* argv[]) { + if (argc != 2) return EXIT_FAILURE; + parse_nums(argv[1]); + for (size_t i = 0; i < nums.size(); i++) { + cout << nums[i]; + if (i + 2 < nums.size() && + nums[i + 1] == nums[i] + 1 && + nums[i + 2] == nums[i] + 2) { + size_t j = 0; + while (i + j < nums.size() && nums[i + j] == nums[i] + j) + j++; + i += j - 1; + cout << "-" << nums[i]; + } + if (i + 1 < nums.size()) + cout << ","; + } + cout << endl; +} diff --git a/challenge-006/paulo-custodio/cpp/ch-2.cpp b/challenge-006/paulo-custodio/cpp/ch-2.cpp new file mode 100644 index 0000000000..b408b41945 --- /dev/null +++ b/challenge-006/paulo-custodio/cpp/ch-2.cpp @@ -0,0 +1,33 @@ +/* +Challenge 006 + +Challenge #2 +Create a script to calculate Ramanujan's constant with at least 32 digits of +precision. Find out more about it here. + +The standard IEEE 754 double-precision binary floating-point format: binary64 +gives only 15 to 17 significant decimal digits +*/ + +#include <iostream> +#include <gmp.h> +#include <mpfr.h> + +int main() { + mpfr_set_default_prec(256); // 107 bits needed to represent 32 decimal digits + + mpfr_t pi, e, k; + mpfr_inits(pi, e, k, NULL); + + mpfr_const_pi(pi, MPFR_RNDN); + + mpfr_set_str(e, "163", 10, MPFR_RNDN); // e = 163 + mpfr_sqrt(e, e, MPFR_RNDN); // e = sqr(163) + mpfr_mul(e, pi, e, MPFR_RNDN); // e = pi*sqr(163) + + mpfr_exp(k, e, MPFR_RNDN); // k = e^(pi*sqr(163)) + + mpfr_printf("%32.12Rf\n", k); // 18.12 = 32 precision + + mpfr_clears(pi, e, k, NULL); +} diff --git a/challenge-006/paulo-custodio/perl/ch-1.pl b/challenge-006/paulo-custodio/perl/ch-1.pl index 4b39e6774e..94fc7119bb 100644 --- a/challenge-006/paulo-custodio/perl/ch-1.pl +++ b/challenge-006/paulo-custodio/perl/ch-1.pl @@ -3,7 +3,9 @@ # Challenge 006 # # Challenge #1 -# Create a script which takes a list of numbers from command line and print the same in the compact form. For example, if you pass “1,2,3,4,9,10,14,15,16” then it should print the compact form like “1-4,9,10,14-16”. +# Create a script which takes a list of numbers from command line and print the +# same in the compact form. For example, if you pass "1,2,3,4,9,10,14,15,16" +# then it should print the compact form like "1-4,9,10,14-16". use strict; use warnings; diff --git a/challenge-006/paulo-custodio/perl/ch-2.pl b/challenge-006/paulo-custodio/perl/ch-2.pl index 6159d42a0a..053b9e377e 100644 --- a/challenge-006/paulo-custodio/perl/ch-2.pl +++ b/challenge-006/paulo-custodio/perl/ch-2.pl @@ -3,9 +3,11 @@ # Challenge 006 # # Challenge #2 -# Create a script to calculate Ramanujan’s constant with at least 32 digits of precision. Find out more about it here. +# Create a script to calculate Ramanujan's constant with at least 32 digits of +# precision. Find out more about it here. # -# The standard IEEE 754 double-precision binary floating-point format: binary64 gives only 15 to 17 significant decimal digits +# The standard IEEE 754 double-precision binary floating-point format: binary64 +# gives only 15 to 17 significant decimal digits # Therefore must use the bignum library use strict; @@ -13,8 +15,8 @@ use warnings; use 5.030; use Math::BigFloat; -my $accuracy = 64; +my $accuracy = 128; my $e = Math::BigFloat->bpi($accuracy) * Math::BigFloat->bsqrt(163, $accuracy); my $k = Math::BigFloat->bexp($e, $accuracy); -say $k; +say $k->bround(30); diff --git a/challenge-006/paulo-custodio/t/test-1.yaml b/challenge-006/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..61dba0b8d3 --- /dev/null +++ b/challenge-006/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: 1,2,3,4,9,10,14,15,16 + input: + output: 1-4,9,10,14-16 diff --git a/challenge-006/paulo-custodio/t/test-2.yaml b/challenge-006/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..2e2db55591 --- /dev/null +++ b/challenge-006/paulo-custodio/t/test-2.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: + input: + output: 262537412640768743.999999999999 diff --git a/challenge-006/paulo-custodio/test.pl b/challenge-006/paulo-custodio/test.pl index 9cb60254fd..ba6c37260b 100644 --- a/challenge-006/paulo-custodio/test.pl +++ b/challenge-006/paulo-custodio/test.pl @@ -1,19 +1,4 @@ -#!/usr/bin/perl - -use strict; -use warnings; +#!/usr/bin/env perl +use Modern::Perl; use Test::More; -use 5.030; - -is capture("perl perl/ch-1.pl 1,2,3,4,9,10,14,15,16"), "1-4,9,10,14-16\n"; - -is capture("perl perl/ch-2.pl"), "262537412640768743.9999999999992500725971981856888793538563373316\n"; - -done_testing; - -sub capture { - my($cmd) = @_; - my $out = `$cmd`; - $out =~ s/[ \t\v\f\r]*\n/\n/g; - return $out; -} +require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-090/paulo-custodio/test.pl b/challenge-090/paulo-custodio/test.pl index 01ed2b83cd..ba6c37260b 100644 --- a/challenge-090/paulo-custodio/test.pl +++ b/challenge-090/paulo-custodio/test.pl @@ -1,7 +1,4 @@ -#!/usr/bin/perl - -use strict; -use warnings; -use 5.030; - +#!/usr/bin/env perl +use Modern::Perl; +use Test::More; require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-091/paulo-custodio/test.pl b/challenge-091/paulo-custodio/test.pl index 01ed2b83cd..ba6c37260b 100644 --- a/challenge-091/paulo-custodio/test.pl +++ b/challenge-091/paulo-custodio/test.pl @@ -1,7 +1,4 @@ -#!/usr/bin/perl - -use strict; -use warnings; -use 5.030; - +#!/usr/bin/env perl +use Modern::Perl; +use Test::More; require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-092/paulo-custodio/test.pl b/challenge-092/paulo-custodio/test.pl index 01ed2b83cd..ba6c37260b 100644 --- a/challenge-092/paulo-custodio/test.pl +++ b/challenge-092/paulo-custodio/test.pl @@ -1,7 +1,4 @@ -#!/usr/bin/perl - -use strict; -use warnings; -use 5.030; - +#!/usr/bin/env perl +use Modern::Perl; +use Test::More; require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-093/paulo-custodio/test.pl b/challenge-093/paulo-custodio/test.pl index 01ed2b83cd..ba6c37260b 100644 --- a/challenge-093/paulo-custodio/test.pl +++ b/challenge-093/paulo-custodio/test.pl @@ -1,7 +1,4 @@ -#!/usr/bin/perl - -use strict; -use warnings; -use 5.030; - +#!/usr/bin/env perl +use Modern::Perl; +use Test::More; require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-096/paulo-custodio/test.pl b/challenge-096/paulo-custodio/test.pl index 01ed2b83cd..ba6c37260b 100644 --- a/challenge-096/paulo-custodio/test.pl +++ b/challenge-096/paulo-custodio/test.pl @@ -1,7 +1,4 @@ -#!/usr/bin/perl - -use strict; -use warnings; -use 5.030; - +#!/usr/bin/env perl +use Modern::Perl; +use Test::More; require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-097/paulo-custodio/test.pl b/challenge-097/paulo-custodio/test.pl index 01ed2b83cd..ba6c37260b 100644 --- a/challenge-097/paulo-custodio/test.pl +++ b/challenge-097/paulo-custodio/test.pl @@ -1,7 +1,4 @@ -#!/usr/bin/perl - -use strict; -use warnings; -use 5.030; - +#!/usr/bin/env perl +use Modern::Perl; +use Test::More; require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-098/paulo-custodio/test.pl b/challenge-098/paulo-custodio/test.pl index 01ed2b83cd..ba6c37260b 100755 --- a/challenge-098/paulo-custodio/test.pl +++ b/challenge-098/paulo-custodio/test.pl @@ -1,7 +1,4 @@ -#!/usr/bin/perl - -use strict; -use warnings; -use 5.030; - +#!/usr/bin/env perl +use Modern::Perl; +use Test::More; require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-099/paulo-custodio/test.pl b/challenge-099/paulo-custodio/test.pl index 01ed2b83cd..ba6c37260b 100644 --- a/challenge-099/paulo-custodio/test.pl +++ b/challenge-099/paulo-custodio/test.pl @@ -1,7 +1,4 @@ -#!/usr/bin/perl - -use strict; -use warnings; -use 5.030; - +#!/usr/bin/env perl +use Modern::Perl; +use Test::More; require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-100/paulo-custodio/test.pl b/challenge-100/paulo-custodio/test.pl index 01ed2b83cd..ba6c37260b 100644 --- a/challenge-100/paulo-custodio/test.pl +++ b/challenge-100/paulo-custodio/test.pl @@ -1,7 +1,4 @@ -#!/usr/bin/perl - -use strict; -use warnings; -use 5.030; - +#!/usr/bin/env perl +use Modern::Perl; +use Test::More; require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-101/paulo-custodio/test.pl b/challenge-101/paulo-custodio/test.pl index 01ed2b83cd..ba6c37260b 100644 --- a/challenge-101/paulo-custodio/test.pl +++ b/challenge-101/paulo-custodio/test.pl @@ -1,7 +1,4 @@ -#!/usr/bin/perl - -use strict; -use warnings; -use 5.030; - +#!/usr/bin/env perl +use Modern::Perl; +use Test::More; require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-102/paulo-custodio/test.pl b/challenge-102/paulo-custodio/test.pl index 01ed2b83cd..ba6c37260b 100644 --- a/challenge-102/paulo-custodio/test.pl +++ b/challenge-102/paulo-custodio/test.pl @@ -1,7 +1,4 @@ -#!/usr/bin/perl - -use strict; -use warnings; -use 5.030; - +#!/usr/bin/env perl +use Modern::Perl; +use Test::More; require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-103/paulo-custodio/test.pl b/challenge-103/paulo-custodio/test.pl index 01ed2b83cd..ba6c37260b 100644 --- a/challenge-103/paulo-custodio/test.pl +++ b/challenge-103/paulo-custodio/test.pl @@ -1,7 +1,4 @@ -#!/usr/bin/perl - -use strict; -use warnings; -use 5.030; - +#!/usr/bin/env perl +use Modern::Perl; +use Test::More; require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-104/paulo-custodio/test.pl b/challenge-104/paulo-custodio/test.pl index 01ed2b83cd..ba6c37260b 100644 --- a/challenge-104/paulo-custodio/test.pl +++ b/challenge-104/paulo-custodio/test.pl @@ -1,7 +1,4 @@ -#!/usr/bin/perl - -use strict; -use warnings; -use 5.030; - +#!/usr/bin/env perl +use Modern::Perl; +use Test::More; require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-105/paulo-custodio/test.pl b/challenge-105/paulo-custodio/test.pl index 01ed2b83cd..ba6c37260b 100644 --- a/challenge-105/paulo-custodio/test.pl +++ b/challenge-105/paulo-custodio/test.pl @@ -1,7 +1,4 @@ -#!/usr/bin/perl - -use strict; -use warnings; -use 5.030; - +#!/usr/bin/env perl +use Modern::Perl; +use Test::More; require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-106/paulo-custodio/test.pl b/challenge-106/paulo-custodio/test.pl index 01ed2b83cd..ba6c37260b 100644 --- a/challenge-106/paulo-custodio/test.pl +++ b/challenge-106/paulo-custodio/test.pl @@ -1,7 +1,4 @@ -#!/usr/bin/perl - -use strict; -use warnings; -use 5.030; - +#!/usr/bin/env perl +use Modern::Perl; +use Test::More; require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-107/paulo-custodio/test.pl b/challenge-107/paulo-custodio/test.pl index cf1ced98e0..ba6c37260b 100755 --- a/challenge-107/paulo-custodio/test.pl +++ b/challenge-107/paulo-custodio/test.pl @@ -1,7 +1,4 @@ #!/usr/bin/env perl - -use strict; -use warnings; -use 5.030; - +use Modern::Perl; +use Test::More; require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-108/paulo-custodio/test.pl b/challenge-108/paulo-custodio/test.pl index cf1ced98e0..ba6c37260b 100755 --- a/challenge-108/paulo-custodio/test.pl +++ b/challenge-108/paulo-custodio/test.pl @@ -1,7 +1,4 @@ #!/usr/bin/env perl - -use strict; -use warnings; -use 5.030; - +use Modern::Perl; +use Test::More; require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-109/paulo-custodio/test.pl b/challenge-109/paulo-custodio/test.pl index cf1ced98e0..ba6c37260b 100755 --- a/challenge-109/paulo-custodio/test.pl +++ b/challenge-109/paulo-custodio/test.pl @@ -1,7 +1,4 @@ #!/usr/bin/env perl - -use strict; -use warnings; -use 5.030; - +use Modern::Perl; +use Test::More; require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-110/paulo-custodio/test.pl b/challenge-110/paulo-custodio/test.pl index cf1ced98e0..ba6c37260b 100755 --- a/challenge-110/paulo-custodio/test.pl +++ b/challenge-110/paulo-custodio/test.pl @@ -1,7 +1,4 @@ #!/usr/bin/env perl - -use strict; -use warnings; -use 5.030; - +use Modern::Perl; +use Test::More; require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-111/paulo-custodio/test.pl b/challenge-111/paulo-custodio/test.pl index cf1ced98e0..ba6c37260b 100755 --- a/challenge-111/paulo-custodio/test.pl +++ b/challenge-111/paulo-custodio/test.pl @@ -1,7 +1,4 @@ #!/usr/bin/env perl - -use strict; -use warnings; -use 5.030; - +use Modern::Perl; +use Test::More; require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-112/paulo-custodio/test.pl b/challenge-112/paulo-custodio/test.pl index cf1ced98e0..ba6c37260b 100755 --- a/challenge-112/paulo-custodio/test.pl +++ b/ |
