aboutsummaryrefslogtreecommitdiff
path: root/challenge-013
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2019-06-19 15:11:14 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2019-06-19 15:11:14 +0100
commit0129b707f95082868e7e0d8d722d16a15d3cb28f (patch)
treefb717fa061a388e37381082078f252236e42b78d /challenge-013
parent2a21edcafe629dbcd8894bc525fe62433d1f189a (diff)
downloadperlweeklychallenge-club-0129b707f95082868e7e0d8d722d16a15d3cb28f.tar.gz
perlweeklychallenge-club-0129b707f95082868e7e0d8d722d16a15d3cb28f.tar.bz2
perlweeklychallenge-club-0129b707f95082868e7e0d8d722d16a15d3cb28f.zip
- Added solutions by Guillermo Ramos.
Diffstat (limited to 'challenge-013')
-rw-r--r--challenge-013/guillermo-ramos/perl5/ch-1.pl24
-rw-r--r--challenge-013/guillermo-ramos/perl5/ch-2.pl38
-rw-r--r--challenge-013/guillermo-ramos/perl5/ch-3.pl88
3 files changed, 150 insertions, 0 deletions
diff --git a/challenge-013/guillermo-ramos/perl5/ch-1.pl b/challenge-013/guillermo-ramos/perl5/ch-1.pl
new file mode 100644
index 0000000000..4a436cb6f8
--- /dev/null
+++ b/challenge-013/guillermo-ramos/perl5/ch-1.pl
@@ -0,0 +1,24 @@
+#!/usr/bin/env perl
+#
+# Write a script to print the date of last Friday of every month of a given year
+################################################################################
+
+use strict;
+use warnings;
+
+use Time::Local qw<timelocal>;
+
+die "Usage: $0 <year>\n" unless @ARGV == 1;
+my $year = shift;
+
+my %last_dom = (
+ 0 => 31, 1 => 28, 2 => 31, 3 => 30, 4 => 31, 5 => 30,
+ 6 => 31, 7 => 31, 8 => 30, 9 => 31, 10 => 30, 11 => 31
+ );
+
+foreach my $month (0..11) {
+ my $last_day = $last_dom{$month};
+ my $last_wday = (localtime timelocal(0, 0, 0, $last_day, $month, $year))[6];
+ my $last_friday = $last_day - $last_wday + ($last_wday >= 5 ? 5 : -2);
+ printf "%d/%02d/%d\n", $year, $month+1, $last_friday;
+}
diff --git a/challenge-013/guillermo-ramos/perl5/ch-2.pl b/challenge-013/guillermo-ramos/perl5/ch-2.pl
new file mode 100644
index 0000000000..89091aeb32
--- /dev/null
+++ b/challenge-013/guillermo-ramos/perl5/ch-2.pl
@@ -0,0 +1,38 @@
+#!/usr/bin/env perl
+#
+# Write a script to demonstrate Mutually Recursive methods. Two methods are
+# mutually recursive if the first method calls the second and the second calls
+# first in turn. Using the mutually recursive methods, generate Hofstadter
+# Female and Male sequences.
+# (https://en.wikipedia.org/wiki/Hofstadter_sequence#Hofstadter_Female_and_Male_sequences)
+################################################################################
+
+use strict;
+use warnings;
+
+use Memoize qw<memoize>;
+
+# Cache the results of previous computations
+memoize('M');
+memoize('F');
+
+sub M {
+ my $n = shift;
+ return $n == 0
+ ? 0
+ : $n - F(M($n-1));
+}
+
+sub F {
+ my $n = shift;
+ return $n == 0
+ ? 1
+ : $n - M(F($n-1));
+}
+
+die "Usage: $0 <iterations>\n" unless @ARGV == 1;
+
+print "M\tF\n-\t-\n";
+foreach my $n (0 .. shift) {
+ printf "%d\t%d\n", M($n), F($n);
+}
diff --git a/challenge-013/guillermo-ramos/perl5/ch-3.pl b/challenge-013/guillermo-ramos/perl5/ch-3.pl
new file mode 100644
index 0000000000..5c9be21021
--- /dev/null
+++ b/challenge-013/guillermo-ramos/perl5/ch-3.pl
@@ -0,0 +1,88 @@
+#!/usr/bin/env perl
+#
+# Find the details of a given word using the Words API
+# (https://www.wordsapi.com/docs/)
+#
+# Dependencies: HTTP-Message, LWP, JSON
+################################################################################
+
+use strict;
+use warnings;
+use open ':std', ':encoding(UTF-8)'; # To correctly display pronunciations
+
+use HTTP::Request;
+use LWP::UserAgent;
+use JSON qw<decode_json>;
+
+# API key read from environment
+my $WORDSAPI_KEY = $ENV{'WORDSAPI_KEY'} or die "WORDSAPI_KEY not defined";
+my $WORD = shift or die "Usage: $0 <word>\n";
+
+my $ua = LWP::UserAgent->new;
+$ua->agent("gramos's script for perlweeklychallenge.org");
+my $uri = "https://wordsapiv1.p.mashape.com/words/$WORD";
+my @headers = ("X-Mashape-Key", $WORDSAPI_KEY);
+my $response = $ua->request(HTTP::Request->new("GET", $uri, \@headers));
+
+# Check for errors in API request
+if ($response->is_error()) {
+ if ($response->code == 404) {
+ die "Word '$WORD' not found\n";
+ } else {
+ die "Unknown error: " . $response->message;
+ }
+}
+
+my $json_resp = decode_json $response->content;
+
+my $syllables = $json_resp->{'syllables'};
+if ($syllables && $syllables->{'count'} > 1) {
+ printf "Word: '$WORD' (%s)\n", join("-", @{$syllables->{'list'}});
+} else {
+ print "Word: '$WORD'\n";
+}
+
+my $pronunciation = $json_resp->{'pronunciation'};
+if (ref($pronunciation) eq "HASH") {
+ my %pronunciations = %{$pronunciation};
+ if ($pronunciations{'all'}) {
+ print "Pronunciation: /$pronunciations{'all'}/\n";
+ } else {
+ print "Pronunciation:\n";
+ foreach my $k (keys %pronunciations) {
+ print " as $k: /$pronunciations{$k}/\n";
+ }
+ }
+} elsif ($pronunciation) {
+ # This case is not documented, thanks WordsAPI.
+ print "Pronunciation: /$pronunciation/\n";
+}
+
+my $frequency = $json_resp->{'frequency'};
+if ($frequency) {
+ print "Frequency: ";
+ print $frequency > 5 ? "frequently used"
+ : $frequency > 3 ? "occasionally used"
+ : $frequency > 1 ? "rarely used"
+ : "never used";
+ print "\n";
+}
+
+my $results = $json_resp->{'results'};
+if ($results) {
+ print "Definitions:\n";
+ foreach my $res (@$results) {
+ printf " - (%s) %s\n", $res->{'partOfSpeech'}, $res->{'definition'};
+ my $synonyms = $res->{'synonyms'};
+ if ($synonyms) {
+ printf " synonyms: %s\n", join(", ", @$synonyms);
+ }
+ my $examples = $res->{'examples'};
+ if ($examples) {
+ foreach my $example (@$examples) {
+ print qq' "$example"\n';
+ }
+ }
+ print "\n";
+ }
+}