/
ch-2.rb
159 lines (133 loc) · 3.02 KB
/
ch-2.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/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