aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-08-18 16:40:41 +0100
committerGitHub <noreply@github.com>2019-08-18 16:40:41 +0100
commitbe8cfb33d0543a05c975eb3d41f5d422420603ef (patch)
tree0d589828243486ecf8fab4def3d5862cc57ca3e0
parenta4acaa3471a33be669b6c6f45aaf9938eab37cd4 (diff)
parentab6aecbbe767d193b05ca04c083d2a3b3b268cf0 (diff)
downloadperlweeklychallenge-club-be8cfb33d0543a05c975eb3d41f5d422420603ef.tar.gz
perlweeklychallenge-club-be8cfb33d0543a05c975eb3d41f5d422420603ef.tar.bz2
perlweeklychallenge-club-be8cfb33d0543a05c975eb3d41f5d422420603ef.zip
Merge pull request #520 from randyl/week21
Some solutions to Challenge #21
-rw-r--r--challenge-021/randy-lauen/perl5/ch-1.pl76
-rw-r--r--challenge-021/randy-lauen/perl5/ch-3.pl43
-rw-r--r--challenge-021/randy-lauen/perl6/ch-1.p624
-rw-r--r--challenge-021/randy-lauen/perl6/ch-3.p661
4 files changed, 204 insertions, 0 deletions
diff --git a/challenge-021/randy-lauen/perl5/ch-1.pl b/challenge-021/randy-lauen/perl5/ch-1.pl
new file mode 100644
index 0000000000..3f727cb405
--- /dev/null
+++ b/challenge-021/randy-lauen/perl5/ch-1.pl
@@ -0,0 +1,76 @@
+#!/usr/bin/env perl
+
+use v5.28;
+use strict;
+use warnings;
+
+=head1 SYNOPSIS
+
+Task: Write a script to calculate the value of e, also known as Euler’s number and Napier’s constant.
+
+In addition to calculating it myself, I took a tour through some of the CPAN modules that
+do it for you. Some modules, like Math::Pari, have a function called Euler() that returns
+Euler's constant, which is not the same as Euler's number.
+
+Example output:
+ $ perl ch-1.pl
+ +------------------------------+-----------------------------------------------------+
+ | Module | Euler's Number |
+ +------------------------------+-----------------------------------------------------+
+ | Math::Big::euler | 2.7182818284590452353602874713526624977572470937 |
+ | Math::NumberCruncher::ECONST | 2.7182818284590452353602874713526624977572470937 |
+ | Math::NumberCruncher::_e_ | 2.7182818284590452353602874713526624977572470936... |
+ | Math::Symbolic::EULER | 2.71828182845905 |
+ | Math::AnyNum::euler | 2.7182818284590452353602874713526624977572470937 |
+ | Sidef Number.e | 2.7182818284590452353602874713526624977572470937 |
+ | bigrat::e | 2.718281828459045235360287471352662497757 |
+ | Calculated e | 2.7182818284590452353602874713526625112110646491... |
+ +------------------------------+-----------------------------------------------------+
+
+=cut
+
+use Math::Big ();
+use Math::BigFloat ();
+use Math::NumberCruncher ();
+use Math::Symbolic ();
+use Math::AnyNum ();
+use Sidef ();
+use Text::Table::Tiny ();
+
+my @eulers = (
+ [ 'Math::Big::euler', sub { Math::Big::euler(1, 47) } ],
+ [ 'Math::NumberCruncher::ECONST', sub { Math::NumberCruncher::ECONST( 46 ) } ],
+ [ 'Math::NumberCruncher::_e_', sub { substr( $Math::NumberCruncher::_e_, 0, 48 ) . '...' } ],
+ [ 'Math::Symbolic::EULER', sub { Math::Symbolic::EULER } ],
+ [ 'Math::AnyNum::euler', sub { Math::AnyNum->e } ],
+ [ 'Sidef Number.e', sub { Sidef->new()->execute_code("Number.e()") } ],
+ [ 'bigrat::e', sub { require bigrat; bigrat::e() } ],
+ # My calculated version of e starts to differ around the 35th decimal.
+ [ 'Calculated e', sub { substr( e(), 0, 48 ) . '...' } ],
+);
+
+my @table = [ 'Module', q{Euler's Number} ];
+foreach my $euler ( @eulers ) {
+ push @table, [ $euler->[0], $euler->[1]->() ];
+}
+
+say Text::Table::Tiny::generate_table( header_row => 1, rows => \@table );
+
+exit 0;
+
+
+sub e {
+ my $e = Math::BigFloat->bzero;
+ for my $n ( 0 .. 50 ) {
+ $e += Math::BigFloat->bone / Math::BigFloat->new( factorial( $n ) );
+ }
+ return $e;
+}
+
+
+sub factorial {
+ my $n = shift;
+ return 1 if $n == 0;
+ return $n * factorial( $n - 1 );
+}
+
diff --git a/challenge-021/randy-lauen/perl5/ch-3.pl b/challenge-021/randy-lauen/perl5/ch-3.pl
new file mode 100644
index 0000000000..1cdb31766d
--- /dev/null
+++ b/challenge-021/randy-lauen/perl5/ch-3.pl
@@ -0,0 +1,43 @@
+#!/usr/bin/env perl
+
+use v5.28;
+use strict;
+use warnings;
+
+=head1 SYNOPSIS
+
+Task: Write a script to use BC Gov News API.
+
+This script prints out the top and feature post headlines.
+
+Example output:
+ $ perl ch-3.pl
+ topPostKey
+ * News Release: Third urgent and primary care centre opens in Greater Vancouver
+ * Backgrounder: North Vancouver UPCC
+
+ featurePostKey
+ * News Release: Province seeks feedback to ban, reduce, recycle more plastics
+
+=cut
+
+use HTTP::API::Client;
+
+my $agent = HTTP::API::Client->new(
+ base_url => URI->new('https://news.api.gov.bc.ca'),
+ pre_defined_data => { 'api-version' => '1.0' },
+);
+
+$agent->get('/api/Home');
+my $homeInfo = $agent->json_response();
+
+foreach my $key (qw( topPostKey featurePostKey )) {
+ $agent->get("/api/Posts/$homeInfo->{$key}");
+ my $postInfo = $agent->json_response();
+
+ say $key;
+ foreach my $document ( $postInfo->{documents}->@* ) {
+ say "* $document->{pageTitle}: $document->{headline}";
+ }
+ say '';
+}
diff --git a/challenge-021/randy-lauen/perl6/ch-1.p6 b/challenge-021/randy-lauen/perl6/ch-1.p6
new file mode 100644
index 0000000000..334c847ec4
--- /dev/null
+++ b/challenge-021/randy-lauen/perl6/ch-1.p6
@@ -0,0 +1,24 @@
+#!/usr/bin/env perl6
+
+=begin SYNOPSIS
+
+Task: Write a script to calculate the value of e, also known as Euler’s number and Napier’s constant.
+
+Example output:
+ $ perl6 ch-1.p6
+ Calculated e = 2.718281828459045
+ Perl6 e = 2.718281828459045
+
+=end SYNOPSIS
+
+my $e = 0;
+for ^20 -> $n {
+ $e += 1/factorial($n);
+}
+say "Calculated e = $e";
+say "Perl6 e = {e}";
+
+sub factorial( Int $n where * >= 0 ) {
+ return 1 if $n == 0;
+ return $n * factorial( $n - 1 );
+}
diff --git a/challenge-021/randy-lauen/perl6/ch-3.p6 b/challenge-021/randy-lauen/perl6/ch-3.p6
new file mode 100644
index 0000000000..ca51f8a140
--- /dev/null
+++ b/challenge-021/randy-lauen/perl6/ch-3.p6
@@ -0,0 +1,61 @@
+#!/usr/bin/env perl6
+
+=begin SYNOPSIS
+
+Task: Write a script to use BC Gov News API.
+
+This script prints out the top and feature post headlines.
+
+Example output:
+
+ $ perl6 ch-3.p6
+ topPostKey
+ * News Release: Third urgent and primary care centre opens in Greater Vancouver
+ * Backgrounder: North Vancouver UPCC
+
+ featurePostKey
+ * News Release: Province seeks feedback to ban, reduce, recycle more plastics
+
+=end SYNOPSIS
+
+use Cro::HTTP::Client;
+use URI::Encode;
+
+class ApiClient {
+ has %.params;
+ has $.base-uri;
+ has $!client = Cro::HTTP::Client.new(
+ :http('1.1'),
+ :base-uri( $!base-uri )
+ :headers( [ Accept => 'application/json' ] ),
+ );
+
+ method get( $path ) {
+ return self!get-json( "$path?" ~ %!params.pairs.map( { .key ~ '=' ~ .value } ).join('&') );
+ }
+
+ method !get-json( $uri ) {
+ my $encoded = uri_encode( $uri );
+ my $response = await $!client.get( $encoded );
+ return await $response.body;
+ }
+}
+
+
+sub MAIN() {
+ my $client = ApiClient.new(
+ base-uri => 'https://news.api.gov.bc.ca',
+ params => %( :api-version('1.0') ),
+ );
+ my %homeInfo = $client.get( '/api/Home' );
+
+ for <topPostKey featurePostKey> -> $key {
+ my %postInfo = $client.get( "/api/Posts/%homeInfo{$key}" );
+ say $key;
+
+ for %postInfo<documents>.flat -> $doc {
+ say "* $doc<pageTitle>: $doc<headline>";
+ }
+ say '';
+ }
+}