Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converter from roman to decimal numbers is added #903

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions algorithms/maths/romans_converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This code converts roman_number into arabic_number

# There are a few rules for writing Roman numerals:
# Roman numerals are always read from left to right, not a problem for us since our writing and reading systems read in the same order.
# 1) The Roman numerals I, X, C and M may be repeated up to three times when writing a compound Roman numeral.
# 2) The Roman numerals V, L and D may never be repeated.
# 3) If a compound Roman numeral has a number on the right that is smaller than the number on the left, both numbers are added.
# Example: XI: the number on the right (I = 1) is smaller than the number on the left (X = 10), then they are added, so XI = 11
# 4) If a composite Roman numeral has a number on the right greater than the number on the left, and it is I, X or C,
# then the number on the left is subtracted from the number on the right.
# Example: IX: The number on the right (X = 10) is greater than the number on the left (I = 1), and it is also I, then the number on the left is subtracted from the number on the right, so IX = 9

def roman_to_arabic(roman_num):
romans = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000}
result = 0
for i in range(len(roman_num) - 1):
if romans[roman_num[i]] >= romans[roman_num[i + 1]]:
result += int(romans[roman_num[i]])
else:
result -= int((romans[roman_num[i]]))
result += romans[roman_num[-1]]


roman_to_arabic("MM") # 2000
roman_to_arabic("XXI") # 21
roman_to_arabic("MCMXC") # 1990
roman_to_arabic("MDCLXVI") # 1666
roman_to_arabic("MMVIII") # 2008