aboutsummaryrefslogtreecommitdiff
path: root/challenge-023
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2019-08-31 22:14:59 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2019-08-31 22:14:59 +0100
commite200d09264f1c7218af9e22b007efd77da3e834e (patch)
treefe7368065325a1ff060e14a5d222c5612f2d27bb /challenge-023
parent8df6baba003c25211b0f97b2817826bd61e6f69d (diff)
downloadperlweeklychallenge-club-e200d09264f1c7218af9e22b007efd77da3e834e.tar.gz
perlweeklychallenge-club-e200d09264f1c7218af9e22b007efd77da3e834e.tar.bz2
perlweeklychallenge-club-e200d09264f1c7218af9e22b007efd77da3e834e.zip
- Added solutions by Guillermo Ramos.
Diffstat (limited to 'challenge-023')
-rw-r--r--challenge-023/guillermo-ramos/perl5/ch-1.pl30
-rw-r--r--challenge-023/guillermo-ramos/perl5/ch-2.pl33
-rw-r--r--challenge-023/guillermo-ramos/perl5/ch-3.pl52
3 files changed, 115 insertions, 0 deletions
diff --git a/challenge-023/guillermo-ramos/perl5/ch-1.pl b/challenge-023/guillermo-ramos/perl5/ch-1.pl
new file mode 100644
index 0000000000..5c34400b72
--- /dev/null
+++ b/challenge-023/guillermo-ramos/perl5/ch-1.pl
@@ -0,0 +1,30 @@
+#!/usr/bin/env perl
+#
+# Create a script that prints nth order forward difference series. You should be
+# a able to pass the list of numbers and order number as command line
+# parameters. Let me show you with an example.
+#
+# Suppose we have list (X) of numbers: 5, 9, 2, 8, 1, 6 and we would like to
+# create 1st order forward difference series (Y). So using the formula Y(i) =
+# X(i+1) - X(i), we get the following numbers: (9-5), (2-9), (8-2), (1-8),
+# (6-1). In short, the final series would be: 4, -7, 6, -7, 5. If you noticed,
+# it has one less number than the original series. Similary you can carry on 2nd
+# order forward difference series like: (-7-4), (6+7), (-7-6), (5+7) => -11, 13,
+# -13, 12.
+################################################################################
+
+use strict;
+use warnings;
+
+@ARGV == 2 or die "Usage: $0 <order> <x1,x2,...xn>";
+my $order = shift;
+my @xs = split ',', shift;
+
+foreach (1 .. $order) {
+ foreach my $i (0 .. $#xs-1) {
+ $xs[$i] = $xs[$i+1] - $xs[$i];
+ }
+ pop @xs;
+}
+
+print "@xs\n";
diff --git a/challenge-023/guillermo-ramos/perl5/ch-2.pl b/challenge-023/guillermo-ramos/perl5/ch-2.pl
new file mode 100644
index 0000000000..fb3c699997
--- /dev/null
+++ b/challenge-023/guillermo-ramos/perl5/ch-2.pl
@@ -0,0 +1,33 @@
+#!/usr/bin/env perl
+#
+# Create a script that prints Prime Decomposition of a given number. The prime
+# decomposition of a number is defined as a list of prime numbers which when all
+# multiplied together, are equal to that number. For example, the Prime
+# decomposition of 228 is 2,2,3,19 as 228 = 2 * 2 * 3 * 19.
+################################################################################
+
+use strict;
+use warnings;
+
+use List::Util qw<any>;
+
+my $n = my $number = shift or die "Usage: $0 <number>";
+
+my @primes = (2);
+my @factors;
+while ($n != 1) {
+ my $lastp = $primes[-1];
+ if ($n % $lastp == 0) {
+ push @factors, $lastp;
+ $n /= $lastp;
+ } else {
+ $lastp++ while any { $lastp % $_ == 0 } @primes;
+ push @primes, $lastp;
+ }
+}
+
+print "$number = ", join("*", @factors), "\n";
+
+# Quick test
+$n *= $_ foreach (@factors);
+$n == $number or die "Oops, something went wrong!";
diff --git a/challenge-023/guillermo-ramos/perl5/ch-3.pl b/challenge-023/guillermo-ramos/perl5/ch-3.pl
new file mode 100644
index 0000000000..2dd84376b2
--- /dev/null
+++ b/challenge-023/guillermo-ramos/perl5/ch-3.pl
@@ -0,0 +1,52 @@
+#!/usr/bin/env perl
+#
+# Write a script to use Random Poems API. This is the easiset API, I have come
+# across so far. You don’t need API key for this. They have only route to work
+# with (GET).
+#
+# (https://www.poemist.com/api/v1/randompoems)
+################################################################################
+
+use strict;
+use warnings;
+use open ':std', ':encoding(UTF-8)';
+use autodie;
+
+use HTTP::Request;
+use LWP::UserAgent;
+use JSON qw<decode_json>;
+use Time::HiRes qw<sleep>;
+
+my $ua = LWP::UserAgent->new();
+$ua->agent("gramos's script for perlweeklychallenge.org");
+
+my $uri = 'https://www.poemist.com/api/v1/randompoems';
+my $resp = $ua->request(HTTP::Request->new('GET', $uri));
+my $content = decode_json($resp->content);
+my $poem = $content->[0];
+my $poet = $poem->{'poet'};
+
+$|++;
+sub typewrite {
+ my $text = shift;
+ foreach my $i (0 .. length($text) - 1) {
+ my $char = substr($text, $i, 1);
+ print $char;
+
+ my $sleep =
+ $char eq ',' ? 0.2 :
+ (grep { $_ eq $char } qw<- : ( )>) ? 0.3 :
+ $char eq ';' ? 0.4 :
+ $char eq '.' ? 0.5 :
+ (grep { $_ eq $char } qw<! ?>) ? 0.6 :
+ $char eq "\n" ? 0.7 :
+ 0.06;
+ sleep($sleep);
+ }
+}
+
+typewrite sprintf "%s", $poem->{'title'};
+sleep(0.2);
+typewrite sprintf "\nby %s\n\n", $poet->{'name'};
+printf "(%s)", $poem->{'url'};
+typewrite(sprintf "\n\n%s\n", $poem->{'content'});