In this article, we will teach you how to add two Roman Numbers in python. So let's get started.
Addition of Two Roman Numerals:
The addition of
Roman Numeral is quite difficult because of their different orders. For
example, IV means 4 whereas VI means 6, also XL means 40 whereas LX means 60.
Therefore, it is challenging for us to detect the positions or orders of Romans
Numerals.
NOTE:
1>
Creating Roman Numerals with their
Numeric Values:
Creating
Romans with their Numeric Values helps us to determine the exact value of the
user-entered Roman Number. Creating in global space will make them available
to all our functions.
romans = {'M': 1000, 'D': 500, 'C': 100, 'L': 50, 'X': 10, 'V': 5, 'I': 1}
Also, we add
another group of Romans which is used to determine the values of Romans
according to their orders.
pre_romans = { 'CM': 900, 'LM': 950, 'VM': 995, 'IM': 999, 'XM': 990, 'CD': 400, 'LD': 450, 'VD': 495, 'ID': 499, 'XD': 490, 'VC': 95, 'IC': 99, 'VL': 45, 'XL': 40, 'XC': 90, 'IL': 49, 'IX': 9, 'IV': 4 }
The Above
Code helps us in the following Situations:
- When users enter Pure Romans (like C, M, L, X, D, V, I), then We will simply get their values (like 100, 1000, 50, 10, 500, 5, 1) using ‘Romans’ dictionary.
- When
users enter Romans where the determination of order is necessary or where roman is
subtracted from another roman (like IV, IX, XL, XC, etc.) then we will obtain
their numeric values (like 4, 9, 40, 90, etc.) with the help of our pre_romans
dictionary.
2> Creating
function which will return our answer:
Now, we need
to create a function that returns our answer. Here, we called it ‘get_answer’ as we get our answer from it but you call it whatever you want.
def get_answer(eq): first_num, second_num = eq.split("+") num1, num2 = get_conversion(first_num), get_conversion(second_num) r = back_to_roman(num1 + num2) return r
- argument “eq” stands for ‘equation’ that the user enters in the form of ‘XIV + XL’.
- The second line, eq.split(“+”) breaks the equation into two separate Roman numbers & stored in two variables as first_num and second_num.
- In the third line, we obtain the numeric representation of “first_num” and “second_num” that is returned by get_conversion() function and store them in num1 or num2 variables.
- After, getting the Roman representation, we simply add and passed them as an argument to the back_to_roman() function, which will return the Roman Form of the sum of “num1” and “num2”
get_conversion
() function:
def get_conversion(num): n1 = 0 for i in pre_romans.keys(): if i in num: num = num[:num.find(i)] + num[num.find(i)+len(i):] n1 += pre_romans[i] for i in romans.keys(): while i in num: n1 += romans[i] num = num[:num.find(i)] + num[num.find(i)+len(i):] return n1
The
get_conversion () function accepts an argument, a Roman number, and returns its
numeric form.
·
The
First for loop finds all the pre_romans (such as ‘IV’, ‘XL’, ‘VL’, etc.) and
adds their numeric values to n1. Since, there is no built-in “remove” or “pop”
methods are available for Python String, therefore we simply remove the
pre_romans from the number using String Slicing so that, after giving the value
of pre_roman, it will remove it from the number which gives us the correct the
values of pre_romans. Hence, the First for loop helps us to determine the order
of Romans numbers.
·
After
First for loop, there is no pre_romans are left since we remove them from the
number therefore there is no need to determine the order of Romans numbers.
Now, we simply convert the rest of the roman numbers using or “roman”
dictionary.
back_to_roman () function:
In our “How to convert Decimal to Roman” article, we discuss the functionality of the back_to_roman () function in detail. To read about it, click here.
def back_to_roman(n): roman_number = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"] integer = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1] result = "" for i in range(0, 13): while n - integer[i] >= 0: result += roman_number[i] n -= integer[i] return result
After doing all this stuff, here is our code and output:
CODE
romans = {'M': 1000, 'D': 500, 'C': 100, 'L': 50, 'X': 10, 'V': 5, 'I': 1}
pre_romans = {
'CM': 900, 'LM': 950, 'VM': 995, 'IM': 999, 'XM': 990,
'CD': 400, 'LD': 450, 'VD': 495, 'ID': 499, 'XD': 490,
'VC': 95, 'IC': 99, 'VL': 45, 'XL': 40, 'XC': 90,
'IL': 49, 'IX': 9, 'IV': 4
}
def get_conversion(num):
n1 = 0
for i in pre_romans.keys():
if i in num:
num = num[:num.find(i)] + num[num.find(i)+len(i):]
n1 += pre_romans[i]
for i in romans.keys():
while i in num:
n1 += romans[i]
num = num[:num.find(i)] + num[num.find(i)+len(i):]
return n1
def back_to_roman(n):
roman_number = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
integer = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
result = ""
for i in range(0, 13):
while n - integer[i] >= 0:
result += roman_number[i]
n -= integer[i]
return result
def get_answer(eq):
first_num, second_num = eq.split("+")
num1, num2 = get_conversion(first_num), get_conversion(second_num)
r = back_to_roman(num1 + num2)
return r
user_input = input("Enter your eq: ")
print("Answer is :", get_answer(user_input))
Enter your eq: X + XC
Answer is : C
0 Comments