MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / int_to_roman

Function int_to_roman

conversions/roman_numerals.py:41–55  ·  view source on GitHub ↗

Given a integer, convert it to an roman numeral. https://en.wikipedia.org/wiki/Roman_numerals >>> tests = {"III": 3, "CLIV": 154, "MIX": 1009, "MMD": 2500, "MMMCMXCIX": 3999} >>> all(int_to_roman(value) == key for key, value in tests.items()) True

(number: int)

Source from the content-addressed store, hash-verified

39
40
41def int_to_roman(number: int) -> str:
42 """
43 Given a integer, convert it to an roman numeral.
44 https://en.wikipedia.org/wiki/Roman_numerals
45 >>> tests = {"III": 3, "CLIV": 154, "MIX": 1009, "MMD": 2500, "MMMCMXCIX": 3999}
46 >>> all(int_to_roman(value) == key for key, value in tests.items())
47 True
48 """
49 result = []
50 for arabic, roman in ROMAN:
51 (factor, number) = divmod(number, arabic)
52 result.append(roman * factor)
53 if number == 0:
54 break
55 return "".join(result)
56
57
58if __name__ == "__main__":

Callers

nothing calls this directly

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected