aboutsummaryrefslogtreecommitdiff
path: root/challenge-227
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2023-07-27 15:53:24 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2023-07-27 15:53:24 +0100
commite72ccc8528077ed10b882d93be08dbc521c6ed29 (patch)
tree6afbf7552816012d60efe45b924b352a7fcc7f4e /challenge-227
parentf84f38be0e67c61c44fa0769b00e53124d1bdde0 (diff)
downloadperlweeklychallenge-club-e72ccc8528077ed10b882d93be08dbc521c6ed29.tar.gz
perlweeklychallenge-club-e72ccc8528077ed10b882d93be08dbc521c6ed29.tar.bz2
perlweeklychallenge-club-e72ccc8528077ed10b882d93be08dbc521c6ed29.zip
- Added guest contributions by Robert DiCicco.
Diffstat (limited to 'challenge-227')
-rw-r--r--challenge-227/robert-dicicco/ruby/ch-2.rb159
1 files changed, 159 insertions, 0 deletions
diff --git a/challenge-227/robert-dicicco/ruby/ch-2.rb b/challenge-227/robert-dicicco/ruby/ch-2.rb
new file mode 100644
index 0000000000..92ded9db7f
--- /dev/null
+++ b/challenge-227/robert-dicicco/ruby/ch-2.rb
@@ -0,0 +1,159 @@
+#!/usr/bin/env ruby
+
+=begin
+I found these routines in an article written by Joe McCann
+ joe-mccann.dev
+ and also Module: Roman Numerals
+ Paul M. Winkler
+----------------------------------------------
+AUTHOR: Robert DiCicco
+DATE : 2023-07-26
+Challenge 227 Task 2 Roman Maths ( Ruby )
+----------------------------------------------
+=end
+def numerals
+ {
+ 'I' => 1,
+ 'V' => 5,
+ 'X' => 10,
+ 'L' => 50,
+ 'C' => 100,
+ 'D' => 500,
+ 'M' => 1000
+ }
+end
+
+def special_numerals
+ {
+ 'IV' => 4,
+ 'IX' => 9,
+ 'XL' => 40,
+ 'XC' => 90,
+ 'CD' => 400,
+ 'CM' => 900
+ }
+end
+
+ROMAN_NUMBERS = {
+ 1000 => "M",
+ 900 => "CM",
+ 500 => "D",
+ 400 => "CD",
+ 100 => "C",
+ 90 => "XC",
+ 50 => "L",
+ 40 => "XL",
+ 10 => "X",
+ 9 => "IX",
+ 5 => "V",
+ 4 => "IV",
+ 1 => "I",
+}
+
+def roman_to_int(s)
+ special_matches = s.scan(/IV|IX|XL|XC|CD|CM/)
+ if special_matches
+ special_sum = special_matches.map { |m| special_numerals[m]}.sum
+ special_matches.each { |m| s.gsub!(m, '') }
+ end
+ normal_sum = s.each_char.map { |c| numerals[c] }.sum
+ return (special_sum + normal_sum) if special_sum
+
+ normal_sum
+end
+
+def int_to_roman(n)
+ #n = self
+ roman = ""
+ ROMAN_NUMBERS.each do |value, letter|
+ roman << letter*(n / value)
+ n = n % value
+ end
+ return roman
+end
+
+def main()
+ input_array = ARGV
+ if input_array.length != 1
+ puts("Please enter a math problem using Roman numerals")
+ exit
+
+ end
+
+ problem = ARGV[0]
+ parts = problem.split
+ left = parts[0]
+ lft = roman_to_int(left)
+#333 puts("#{left} = #{lft}")
+ op = parts[1]
+ right = parts[2]
+ rt = roman_to_int(right)
+
+ if ( op == '-' and lft - rt == 0)
+ puts("#{problem} est nulla")
+ exit(1)
+ elsif (op == '/' and (lft % rt) > 0 )
+ puts("#{problem} non potest")
+ exit(1)
+ elsif (op == '+' and lft + rt > 3999)
+ puts("#{problem} non potest")
+ exit(1)
+ elsif (op == '-' and (lft - rt < 0))
+ puts("#{problem} non potest")
+ exit(1)
+ elsif (op == '/' and (lft % rt != 0))
+ puts("#{problem} non potest")
+ exit(1)
+ end
+
+ if ( op == '+')
+ val = int_to_roman(lft + rt)
+ elsif ( op == '-')
+ val = int_to_roman(lft - rt)
+ elsif ( op == '/')
+ val = int_to_roman((lft / rt))
+ elsif ( op == '*')
+ val = int_to_roman((lft * rt))
+ elsif ( op == '**')
+ val = int_to_roman(lft ** rt)
+ end
+
+ puts("#{problem} => #{val}")
+end
+
+main()
+
+=begin
+----------------------------------------------
+ruby .\RomanMath.rb "IV + V"
+IV + V => IX
+
+ruby .\RomanMath.rb "M - I"
+M - I => CMXCIX
+
+ruby .\RomanMath.rb "X / II"
+X / II => V
+
+ruby .\RomanMath.rb "XI * VI"
+XI * VI => LXVI
+
+ruby .\RomanMath.rb "XII ** III"
+XII ** III => MDCCXXVIII
+
+ruby .\RomanMath.rb "V - V"
+V - V est nulla
+
+ruby .\RomanMath.rb "V / II"
+V / II non potest
+
+ruby .\RomanMath.rb "MMM + M"
+MMM + M non potest
+
+ruby .\RomanMath.rb "V - X"
+V - X non potest
+----------------------------------------------
+=end
+
+
+
+