HiveBrain v1.2.0
Get Started
← Back to all entries
snippetpythonTip

Convert an integer to a roman numeral

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
romannumeralconvertintegerpython

Problem

Roman numerals can only represent numbers between 1 and 3999. However, they're often used in various contexts, such as movie titles, book chapters, and outlines.
To convert an integer to its roman numeral representation, you can use a lookup list containing tuples of roman values and integers. Then, you can iterate over the values in the lookup list and use divmod() to update the number with the remainder, adding the roman numeral representation to the result.

Solution

def to_roman_numeral(num):
  lookup = [
    (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'),
  ]
  res = ''
  for (n, roman) in lookup:
    (d, num) = divmod(num, n)
    res += roman * d
  return res

to_roman_numeral(3) # 'III'
to_roman_numeral(11) # 'XI'
to_roman_numeral(1998) # 'MCMXCVIII'

Code Snippets

def to_roman_numeral(num):
  lookup = [
    (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'),
  ]
  res = ''
  for (n, roman) in lookup:
    (d, num) = divmod(num, n)
    res += roman * d
  return res

to_roman_numeral(3) # 'III'
to_roman_numeral(11) # 'XI'
to_roman_numeral(1998) # 'MCMXCVIII'

Context

From 30-seconds-of-code: to-roman-numeral

Revisions (0)

No revisions yet.