diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-07-28 15:31:57 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-07-28 15:31:57 +0100 |
| commit | 6bd49251057dd09c43d92a0789a9e6fd59c17e14 (patch) | |
| tree | 4d39a1026d7a3ac45ea09aaadd36103d19bf00b3 /challenge-227 | |
| parent | 36a94039dd759fc567e9807f5b7fe5fd4689712d (diff) | |
| download | perlweeklychallenge-club-6bd49251057dd09c43d92a0789a9e6fd59c17e14.tar.gz perlweeklychallenge-club-6bd49251057dd09c43d92a0789a9e6fd59c17e14.tar.bz2 perlweeklychallenge-club-6bd49251057dd09c43d92a0789a9e6fd59c17e14.zip | |
- Added solutions by Andreas Voegele.
- Added solutions by Lubos Kolouch.
- Added solutions by Jorg Sommrey.
Diffstat (limited to 'challenge-227')
| -rw-r--r-- | challenge-227/robert-dicicco/julia/ch-2.jl | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/challenge-227/robert-dicicco/julia/ch-2.jl b/challenge-227/robert-dicicco/julia/ch-2.jl new file mode 100644 index 0000000000..af1be45fe6 --- /dev/null +++ b/challenge-227/robert-dicicco/julia/ch-2.jl @@ -0,0 +1,130 @@ +#!/usr/bin/env julia +#= +------------------------------------------ +AUTHOR: Robert DiCicco +DATE : 2023-07-28 +Challenge 227 Task 2 Roman Maths ( Julia ) +------------------------------------------ +=# +using Printf +using RomanNumerals + +numerals = Dict( + "I" => 1, + "V" => 5, + "X" => 10, + "L" => 50, + "C" => 100, + "D" => 500, + "M" => 1000 + ) + +special_numerals = Dict( + "IV" => 4, + "IX" => 9, + "XL" => 40, + "XC" => 90, + "CD" => 400, + "CM" => 900 + ) + +special_sum = 0 +normal_sum = 0 + +function roman_to_int(s) + global special_sum, normal_sum, numerals + special_sum = 0 + normal_sum = 0 + while(length(s) > 0) + if (length(s) > 1) && occursin(r"(IV|IX|XL|XC|CD|CM)",s[1:2]) + special_sum += special_numerals[string(s[1:2])] + s=chop(s,head=2,tail=0) + else + normal_sum += numerals[string(s[1])] + s=chop(s,head=1,tail=0) + end + end + return special_sum + normal_sum +end + +n = length(ARGS) +if ( n != 1 ) + print("Please enter a math problem using Roman numerals") + exit(1) +end + +problem = uppercase(ARGS[1]) +parts = split(problem) +left = parts[1] + +op = parts[2] +right = parts[3] + +lft = roman_to_int(left) +rt = roman_to_int(right) + +if cmp(op,"-") == 0 && (lft - rt == 0) + @printf("%s est nulla\n",problem) + exit(1) + elseif cmp(op,"/") == 0 && (lft % rt) > 0 + @printf("%s non potest",problem) + exit(1) + elseif cmp(op,"+") == 0 && (lft + rt) > 3999 + @printf("%s non potest",problem) + exit(1) + elseif cmp(op,"-") == 0 && (lft - rt) < 0 + @printf("%s non potest",problem) + exit(1) + elseif cmp(op,"/") == 0 && (lft % rt) != 0 + @printf("%s non potest",problem) + exit(1) +end + +if cmp(op,"+") == 0 + val = RomanNumeral(lft + rt) + elseif cmp(op,"-") == 0 + val = RomanNumeral(lft - rt) + elseif cmp(op,"/") == 0 + val = RomanNumeral(trunc(Int,lft/rt)) + elseif cmp(op,"*") == 0 + val = RomanNumeral(lft * rt) + elseif cmp(op,"**") == 0 + val = RomanNumeral(trunc(Int,lft ^ rt)) +end + + +@printf("%s => %s\n",problem,val) + +#= +------------------------------------------ +SAMPLE OUTPUT +julia .\RomanMath.jl "IV + V" +IV + V => IX + +julia .\RomanMath.jl "M - I" +M - I => CMXCIX + +julia .\RomanMath.jl "X / II" +X / II => V + +julia .\RomanMath.jl "XI * VI" +XI * VI => LXVI + +julia .\RomanMath.jl "VII ** III" +VII ** III => CCCXLIII + +julia .\RomanMath.jl "V - V" +V - V est nulla + +julia .\RomanMath.jl "V / II" +V / II non potest + +julia .\RomanMath.jl "MMM + M" +MMM + M non potest + +julia .\RomanMath.jl "V - X" +V - X non potest +------------------------------------------ +=# + + |
