aboutsummaryrefslogtreecommitdiff
path: root/challenge-047
diff options
context:
space:
mode:
authoruser-person <60802990+user-person@users.noreply.github.com>2020-02-14 20:22:36 -0500
committerGitHub <noreply@github.com>2020-02-14 20:22:36 -0500
commit0d3ddf429f7399382871890bf9d314ac6b7293a9 (patch)
tree3f0ed4d3e0d181b8662cb86378f1757d3fcb2144 /challenge-047
parent0323c8193223144fc99960c51b1640bc4203b8d9 (diff)
downloadperlweeklychallenge-club-0d3ddf429f7399382871890bf9d314ac6b7293a9.tar.gz
perlweeklychallenge-club-0d3ddf429f7399382871890bf9d314ac6b7293a9.tar.bz2
perlweeklychallenge-club-0d3ddf429f7399382871890bf9d314ac6b7293a9.zip
Create ch-1.pl
Diffstat (limited to 'challenge-047')
-rw-r--r--challenge-047/user-person/perl/ch-1.pl50
1 files changed, 50 insertions, 0 deletions
diff --git a/challenge-047/user-person/perl/ch-1.pl b/challenge-047/user-person/perl/ch-1.pl
new file mode 100644
index 0000000000..6dc09d1bfb
--- /dev/null
+++ b/challenge-047/user-person/perl/ch-1.pl
@@ -0,0 +1,50 @@
+#!/usr/bin/env perl
+
+###########################################################################
+# script name: ch-1.pl #
+# #
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-047/ #
+# #
+# 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. #
+# #
+# e.g. #
+# ch-1.pl V + VI #
+# #
+# output: #
+# XI #
+# #
+###########################################################################
+
+use strict;
+use warnings;
+
+use FindBin;
+use Roman;
+
+my $argString = uc "@ARGV";
+my $script = $FindBin::Script;
+
+die "$FindBin::Script requires arguments.\n" if scalar @ARGV == 0;
+
+print STDERR "[Arabic number detected in input]\n" if $argString =~ m{\d+};
+
+$argString =~ s{(\s*[-+*/%]\s*)}{ $1 }g;
+$argString =~ s{([MDCLXVI]+)}{arabic $1}ge;
+
+my $result = eval $argString;
+
+my $oldResult = $result;
+$result = int $result;
+my $decimal = $oldResult - $result;
+print STDERR "Calculation result had a decimal $decimal that was truncated.\n" if $decimal;
+
+if ( $result == 0) {
+ print "N (no formal zero)\n"; # https://en.wikipedia.org/wiki/Roman_numerals#Zero
+} elsif ($result > 3_999) {
+ die "Calculation result $result , exceeds MMMCMXCIX (3,999) the maximum value of the Roman number format.\n";
+} else {
+ $result = Roman(int $result);
+ print "$result\n";
+}