aboutsummaryrefslogtreecommitdiff
path: root/challenge-047
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-02-10 11:52:11 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-02-10 11:52:11 +0000
commitd5741960b9b2f50c21d2efd4975281380fe69ef7 (patch)
tree49392a743d6810d74022b9b88555d66171c5f119 /challenge-047
parenteafbcb908967a51753c6578afb7694dbff764e61 (diff)
downloadperlweeklychallenge-club-d5741960b9b2f50c21d2efd4975281380fe69ef7.tar.gz
perlweeklychallenge-club-d5741960b9b2f50c21d2efd4975281380fe69ef7.tar.bz2
perlweeklychallenge-club-d5741960b9b2f50c21d2efd4975281380fe69ef7.zip
- Added solutions by Javier Luque.
Diffstat (limited to 'challenge-047')
-rw-r--r--challenge-047/javier-luque/blog.txt1
-rw-r--r--challenge-047/javier-luque/perl/ch-1.pl22
-rw-r--r--challenge-047/javier-luque/perl/ch-2.pl21
-rw-r--r--challenge-047/javier-luque/raku/ch-1.p659
-rw-r--r--challenge-047/javier-luque/raku/ch-2.p620
5 files changed, 123 insertions, 0 deletions
diff --git a/challenge-047/javier-luque/blog.txt b/challenge-047/javier-luque/blog.txt
new file mode 100644
index 0000000000..231f8ed1d6
--- /dev/null
+++ b/challenge-047/javier-luque/blog.txt
@@ -0,0 +1 @@
+https://perlchallenges.wordpress.com/2020/02/10/perl-weekly-challenge-047/
diff --git a/challenge-047/javier-luque/perl/ch-1.pl b/challenge-047/javier-luque/perl/ch-1.pl
new file mode 100644
index 0000000000..918c8bd150
--- /dev/null
+++ b/challenge-047/javier-luque/perl/ch-1.pl
@@ -0,0 +1,22 @@
+#!/usr/bin/perl
+# Test: ./ch-1.pl V + VI
+use strict;
+use warnings;
+use feature qw /say/;
+use Number::Convert::Roman;
+
+my $input_string = join ' ', @ARGV;
+my $c = Number::Convert::Roman->new;
+
+my %operations = (
+ '+' => sub {return ($c->arabic($_[0]) + $c->arabic($_[1]))},
+ '-' => sub {return ($c->arabic($_[0]) - $c->arabic($_[1]))},
+ 'x' => sub {return ($c->arabic($_[0]) * $c->arabic($_[1]))},
+ '/' => sub {return ($c->arabic($_[0]) / $c->arabic($_[1]))},
+);
+
+if ($input_string =~ /^(\w+)\s*([\+\-x\/])\s*(\w+)$/) {
+ say $c->roman(int($operations{$2}->($1, $3)));
+} else {
+ say "Invalid input";
+}
diff --git a/challenge-047/javier-luque/perl/ch-2.pl b/challenge-047/javier-luque/perl/ch-2.pl
new file mode 100644
index 0000000000..bbcf9164f4
--- /dev/null
+++ b/challenge-047/javier-luque/perl/ch-2.pl
@@ -0,0 +1,21 @@
+#!/usr/bin/perl
+# test: perl ch-2.pl
+use strict;
+use warnings;
+use feature qw /say/;
+
+my $n = 100;
+my $p = 0;
+
+while ($p < 20) {
+ $n =~ /^(\d)\d*(\d)$/;
+ my $g_divisor = $1 . $2;
+
+ if ($n % $g_divisor == 0){
+ $p++;
+ say $n . ' / ' . $g_divisor . ' = ' .
+ ($n / $g_divisor);
+ }
+
+ $n++;
+}
diff --git a/challenge-047/javier-luque/raku/ch-1.p6 b/challenge-047/javier-luque/raku/ch-1.p6
new file mode 100644
index 0000000000..196779ad68
--- /dev/null
+++ b/challenge-047/javier-luque/raku/ch-1.p6
@@ -0,0 +1,59 @@
+# Test: perl6 ch1.p6 "V + VI"
+use v6.d;
+
+sub MAIN($equation) {
+ my %operators = (
+ '+' => -> $a , $b { rtoa($a) + rtoa($b) },
+ '-' => -> $a , $b { rtoa($a) - rtoa($b) },
+ 'x' => -> $a , $b { rtoa($a) * rtoa($b) },
+ '/' => -> $a , $b { rtoa($a) / rtoa($b) },
+ );
+
+ if ($equation ~~ /^(\w+)\s*(\+|\-|x|\/)\s*(\w+)$/) {
+ say ator(%operators{$1}($0.Str, $2.Str).Int);
+ } else {
+ say "Invalid input";
+ }
+}
+
+
+# Inspired by:
+# https://examples.p6c.dev/categories/euler/prob089-andreoss.html
+
+multi rtoa() { 0 }
+multi rtoa(Str $r where $r.chars > 1 ) {
+ rtoa(| $r.comb)
+}
+
+multi rtoa('I', |a) { 1 + rtoa(|a) }
+multi rtoa('I', 'V', |a) { 4 + rtoa(|a) }
+multi rtoa('V', |a) { 5 + rtoa(|a) }
+multi rtoa('I', 'X', |a) { 9 + rtoa(|a) }
+multi rtoa('X', |a) { 10 + rtoa(|a) }
+multi rtoa('X', 'L', |a) { 40 + rtoa(|a) }
+multi rtoa('L' , |a) { 50 + rtoa(|a) }
+multi rtoa('X', 'C', |a) { 90 + rtoa(|a) }
+multi rtoa('C', |a) { 100 + rtoa(|a) }
+multi rtoa('C', 'D', |a) { 400 + rtoa(|a) }
+multi rtoa('D', |a) { 500 + rtoa(|a) }
+multi rtoa('C', 'M', |a) { 900 + rtoa(|a) }
+multi rtoa('M', |a) { 1000 + rtoa(|a) }
+
+sub ator(Int $n) returns Str {
+ given $n {
+ when $n >= 1000 { 'M' ~ ator($n- 1000) }
+ when $n >= 900 { 'CM' ~ ator($n - 900) }
+ when $n >= 500 { 'D' ~ ator($n - 500) }
+ when $n >= 400 { 'CD' ~ ator($n- 400) }
+ when $n >= 100 { 'C' ~ ator($n - 100) }
+ when $n >= 90 { 'XC' ~ ator($n - 90) }
+ when $n >= 50 { 'L' ~ ator($n - 50) }
+ when $n >= 40 { 'XL' ~ ator($n - 40) }
+ when $n >= 10 { 'X' ~ ator($n - 10) }
+ when $n >= 9 { 'IX' ~ ator($n- 9) }
+ when $n >= 5 { 'V' ~ ator($n - 5) }
+ when $n >= 4 { 'IV' ~ ator($n - 4) }
+ when $n >= 1 { 'I' ~ ator($n - 1) }
+ default { '' }
+ }
+}
diff --git a/challenge-047/javier-luque/raku/ch-2.p6 b/challenge-047/javier-luque/raku/ch-2.p6
new file mode 100644
index 0000000000..fe39bb2554
--- /dev/null
+++ b/challenge-047/javier-luque/raku/ch-2.p6
@@ -0,0 +1,20 @@
+# Test: perl6 ch-2.p6
+use v6.d;
+
+sub MAIN () {
+ my $n = 100;
+ my $p = 0;
+
+ while ($p < 20) {
+ $n ~~ /^(\d)\d*(\d)$/;
+ my $g_divisor = $0 ~ $1;
+
+ if ($n %% $g_divisor) {
+ $p++;
+ say $n ~ ' / ' ~ $g_divisor ~
+ ' = ' ~ ($n / $g_divisor);
+ }
+
+ $n++;
+ }
+}