Files
30-seconds-of-code/snippets/to-roman-numeral.md
Angelos Chalaris f6a215e9e3 Kebab file names
2023-04-27 22:00:06 +03:00

45 lines
976 B
Markdown

---
title: Integer to roman numeral
tags: math,string
cover: tram-car
firstSeen: 2020-10-07T00:04:09+03:00
lastUpdated: 2020-11-02T19:28:35+02:00
---
Converts an integer to its roman numeral representation.
Accepts value between `1` and `3999` (both inclusive).
- Create a lookup list containing tuples in the form of (roman value, integer).
- Use a `for` loop to iterate over the values in `lookup`.
- Use `divmod()` to update `num` with the remainder, adding the roman numeral representation to the result.
```py
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
```
```py
to_roman_numeral(3) # 'III'
to_roman_numeral(11) # 'XI'
to_roman_numeral(1998) # 'MCMXCVIII'
```