How to build App that will be converting Roman Numeral to Decimal number.
what is Roman Numeral??
Roman numeral is any of the symbols used in a system of numerical notation based on the ancient Roman system. The symbols are I, V, X, L, C, D, and M
, standing respectively for 1, 5, 10, 50, 100, 500, and 1,000
in the Hindu-Arabic.
However one of the major aspect of mathematics that we all learn in basic school is roman numeral. But won't it be more interesting if we can have a app that convert or interprete roman to our own counting number??
Yeah! who doesn't want to do that?? Now let's see how it work.
How to Convert Roman Numeral to Decimal or hindu-arabic.
As a matter of fact you can not write a codes or build an app for certain purpose if you don't understand the nature of the problem. so let's see how to convert some roman numeral and discover the logic behind it.
Example
Roman Numeral | Logical Explanation | Decimal/hindu-arabic |
XCV | -10+100+5 | 95 |
MMCCV | 1000+1000+100+100+5 | 2205 |
CDXCIV | -100+500-10+100-1+5 | 494 |
MMCVII | 1000+1000+100+5+1+1 | 2107 |
MMDLXVIII | 1000+1000+500+50+10+5+1+1+1 | 2568 |
Study the example above and the logical Explanation then you will realized that the symbols are just interpreted just like I mentioned before and added together. However, there is important Notice on the summation what was that??
Yeah, we put negative sign infront of some of them. the rule is that greater number should comes before the less number when ever less number come before the great number the less number takes negative. Thus, in example 1, X is interpreted as -10 because it comes before C which is 100. In example 3, C is interpreted as -100 because it comes before D which is 500 while X and I is interpreted as -10 and -1 because they come before C and V which are 100 and 5 respectively.
How do we write Code for Roman Numeral Converter?? Since we know each of the symbol denote a specific number and we only need to add them up and follow the logic pattern then we take the following step.
STEP 1: create a Dictionary to store each of the symbols and their value like the codes below:
codes>>
romanSymbols = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000,
}
STEP 2: ask the user to input the roman numeral and also decalare variable that will be keeping the sum
romanNumeral=input('Enter your roman Numeral to be converted')
summation = 0
STEP 3: loop through the roman numeral and convert the each of the symbol to to Decimal then add.
for symbol in romanNumeral:
summation += romanSymbols[symbol] # to convert the value and add it to sum.
STEP 4: print the sum to see the summation
print(summation)
The whole codes I just explained look like this
romanSymbols = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000,
# specify more numerals if you wish
}
romanNumeral=str(input('Enter your roman Numeral: '))
summation=0
for symbol in romanNumeral:
summation += romanSymbols[symbol] # to convert the value and add it to sum.
print(sum)
copy the codes and run using different roman Numeral what did you discover???? Nothing ??? check again?? Yeah the answer is incorrect sometimes. why?? well that is because we are actually doing addition of the symbols without even considering the condition when we have the previous symbol less than the next. So to do the little adjustment we add conditional codes to monitoring that.
Adjustment Codes>>
romanSymbols = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000,
}
romanNumeral=str(input('Enter your roman Numeral: '))
summation=0
print(romanNumeral)
for index in range(len(romanNumeral) - 1): #we subtract 1 so that we don't exceed the string limit
previousSymbol = romanNumeral[index] #to know the first symbol
nextSymbol = romanNumeral[index + 1] # to know the symbol that followed it.
previousNumber=romanSymbols[previousSymbol] #to know the numeric value when converted
nextNumber=romanSymbols[nextSymbol] #to know the numeric value when converted
if previousNumber< nextNumber : # to check if the first value is less than the second one
summation -= previousNumber # add negative to the number if true
else:
summation += previousNumber # add the number positively.
lastSymbol=romanNumeral[-1] # to get the last symbols.
lastNumber=romanSymbols[lastSymbol] # to get the converted value of the last symbol
summation+=lastNumber # add the last number to the summation
print(summation)
Run the codes and input different value and see the result. the following show the result after running the codes 4 times
Result>>>
Enter your roman Numeral: DXCVII
DXCVII
597
Enter your roman Numeral: DXCVII
MMCDCCXCIV
2694
Enter your roman Numeral: DXCVII
CCXXXIX
239
Enter your roman Numeral: DXCVII
MDXII
1512
Note: we need to add last Symbol to the summation because inside our loop we always add or subtract the previousNumber meaning there will always be one symbol which we never added to the summation and that is what we later do at the end of the loop. Also, if you check our for loop you will realize that we subtract 1 from the length which we won't have done if we need to loop through all the Symbols in the romanNumeral. Another way of doing it using List instead of Dictionary is below
romanSymbol=['I','V','X','L','C','D','M']
decimal=[1,5,10,50,100,500,1000]
romanNumeral='MMCDXCVI'
summation=0
for index in range(len(romanNumeral) - 1): #we subtract 1 so that we don't exceed the string limit
previousSymbol = romanNumeral[index] #to know the first symbol
nextSymbol = romanNumeral[index+1] # to know the symbol that followed it.
previousNumber=decimal[romanSymbol.index(previousSymbol)] #to know the numeric value when converted
nextNumber=decimal[romanSymbol.index(nextSymbol)] #to know the numeric value when convert
if previousNumber< nextNumber : # to check if the first value is less than the second one
summation -= previousNumber # add negative to the number if true
else:
summation += previousNumber # add the number positively.
lastSymbol=romanNumeral[-1] # to get the last symbols.
lastNumber=decimal[romanSymbol.index(lastSymbol)] # to get the converted value of the last symbol
summation+=lastNumber # add the last number to the summation
print(summation)
that is simple way to create Roman Numeral converter. I hope you find this article interesting?? it'a still your guy Maxwizard! keep on coding and learning have a nice moment.
You can chat me up on 09153036869
if you have any question or correction.