Convert Integer to Roman Numerals

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

Roman numerals are a numeral system that originated in ancient Rome. They are still used today in various contexts, such as in the numbering of book chapters and movie sequels. In this challenge, you will be tasked with creating a function that converts an integer between 1 and 3999 (inclusive) to its Roman numeral representation.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-13734{{"`Convert Integer to Roman Numerals`"}} python/for_loops -.-> lab-13734{{"`Convert Integer to Roman Numerals`"}} python/lists -.-> lab-13734{{"`Convert Integer to Roman Numerals`"}} python/tuples -.-> lab-13734{{"`Convert Integer to Roman Numerals`"}} python/function_definition -.-> lab-13734{{"`Convert Integer to Roman Numerals`"}} python/build_in_functions -.-> lab-13734{{"`Convert Integer to Roman Numerals`"}} end

Integer to Roman Numeral

Write a function to_roman_numeral(num) that takes an integer num between 1 and 3999 (inclusive) and returns its Roman numeral representation as a string.

To convert an integer to its Roman numeral representation, you can use a lookup list containing tuples in the form of (roman value, integer). You can then use a for loop to iterate over the values in the lookup list and use divmod() to update num with the remainder, adding the Roman numeral representation to the result.

Your function should return the Roman numeral representation of the input integer.

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'

Summary

In this challenge, you learned how to convert an integer to its Roman numeral representation. You used a lookup list containing tuples in the form of (roman value, integer) and a for loop to iterate over the values in the lookup list. You also used divmod() to update num with the remainder, adding the Roman numeral representation to the result.

Other Python Tutorials you may like