aboutsummaryrefslogtreecommitdiff
path: root/challenge-047/noud
diff options
context:
space:
mode:
authorNoud Aldenhoven <noud.aldenhoven@gmail.com>2020-02-15 17:27:22 +0100
committerNoud Aldenhoven <noud.aldenhoven@gmail.com>2020-02-15 17:27:22 +0100
commitfcc5c2cfe1b02f66a2fa5daf7b8254eeb0045a42 (patch)
tree9574c11e41ff376fc2f08c1f68b96ee56de26816 /challenge-047/noud
parent5e0a1807a04e0f144da845c5c9fefed244cbc074 (diff)
downloadperlweeklychallenge-club-fcc5c2cfe1b02f66a2fa5daf7b8254eeb0045a42.tar.gz
perlweeklychallenge-club-fcc5c2cfe1b02f66a2fa5daf7b8254eeb0045a42.tar.bz2
perlweeklychallenge-club-fcc5c2cfe1b02f66a2fa5daf7b8254eeb0045a42.zip
Solutions to challenge 47 task 1 and 2 in Raku by Noud
Diffstat (limited to 'challenge-047/noud')
-rw-r--r--challenge-047/noud/raku/ch-1.p662
-rw-r--r--challenge-047/noud/raku/ch-2.p614
2 files changed, 76 insertions, 0 deletions
diff --git a/challenge-047/noud/raku/ch-1.p6 b/challenge-047/noud/raku/ch-1.p6
new file mode 100644
index 0000000000..45105fe235
--- /dev/null
+++ b/challenge-047/noud/raku/ch-1.p6
@@ -0,0 +1,62 @@
+# Roman Calculator
+#
+# Write a script that accepts two roman numbers and operation. It should then
+# perform the operation on the give roman numbers and print the result.
+#
+# For example,
+#
+# perl ch-1.pl V + VI
+#
+# It should print
+#
+# XI
+
+
+my @roman-symbols = [
+ 1_000, "M",
+ 900, "CM",
+ 500, "D",
+ 400, "CD",
+ 100, "C",
+ 90, "XC",
+ 50, "L",
+ 40, "CL",
+ 10, "X",
+ 9, "IX",
+ 5, "V",
+ 4, "IV",
+ 1, "I"
+];
+
+sub to-roman($i) {
+ for @roman-symbols -> $k, $v {
+ if ($i >= $k) {
+ return $v ~ to-roman($i - $k);
+ }
+ }
+ return '';
+}
+
+# It's assumed that the input is always a ROMAN number. No validation checks
+# are done.
+sub from-roman($s) {
+ for @roman-symbols -> $k, $v {
+ my $r = $s ~~ / ^$v(\w*) /;
+ if ($r) {
+ return $k + from-roman(Str($r[0]));
+ }
+ }
+ return 0;
+}
+
+my %operators =
+ '+' => { $_[0] + $_[1] },
+ '*' => { $_[0] * $_[1] },
+ '-' => { $_[0] - $_[1] },
+ '/' => { Int($_[0] / $_[1]) },
+ '%' => { $_[0] % $_[1] },
+;
+
+sub MAIN($a, $op, $b) {
+ say to-roman(%operators{$op}((from-roman($a), from-roman($b))));
+}
diff --git a/challenge-047/noud/raku/ch-2.p6 b/challenge-047/noud/raku/ch-2.p6
new file mode 100644
index 0000000000..23d514564a
--- /dev/null
+++ b/challenge-047/noud/raku/ch-2.p6
@@ -0,0 +1,14 @@
+# Gapful Number
+#
+# Write a script to print first 20 Gapful Numbers greater than or equal to 100.
+# Please check out the page for more information about Gapful Numbers.
+#
+# Gapful numbers >= 100: numbers that are divisible by the number formed by
+# their first and last digit. Numbers up to 100 trivially have this property
+# and are excluded.
+
+my @gapful = (100..Inf).grep({
+ $_ % Int(Str($_).comb[0] ~ Str($_).comb[*-1]) == 0
+});
+
+@gapful[^20].say;