Roman Numerals to Arabic Numbers

PUBLISHED ON 7 JUNE 2020 — TAGS:  ARCHITECT, AWS, DEVOPS, PYTHON, SCRIPTING

I am working towards my AWS System Architect - Associate certification, training online with A Cloud Guru. I started this last year but the course has been updated for the latest version of the exam. A few new modules have been added to the Architect Learning Path, so I’m doing their ‘Python for Beginners’ course.

In the first part of the course, each lesson concludes with the tutor setting a couple of exercises. I’m quite pleased that my solution to the task of converting Roman numerals to Arabic numbers almost exactly matched his!

def numerus(roman):
	values = { "i": 1, "v": 5, "x": 10, "l": 50, "c": 100, "d": 500, "m": 1000 }

	arabic = 0
	previous = 0

	for letter in roman.lower():
		current = values.get(letter, 0)

		if previous < current:
			arabic -= previous
			current -= previous

		arabic += current
		previous = current

	return arabic