2023-06-21 21:50:10 +00:00
|
|
|
'''
|
|
|
|
Output the correct Roman numerals for a given number.
|
|
|
|
|
2023-06-23 00:20:55 +00:00
|
|
|
This will only give accurate roman numerals up to 3,999. Anything higher starts to combine the addition method along with the traditional method.
|
|
|
|
Numbers higher than 3,999 need to show a line above a roman numeral to say that it is 1,000 times that number. ie: V(with the line above) would be 5,000.
|
|
|
|
|
2023-06-21 21:50:10 +00:00
|
|
|
Input: 1994
|
|
|
|
Output: MCMXCIV
|
|
|
|
|
2023-06-23 00:20:55 +00:00
|
|
|
Time Complexity: O(1)
|
|
|
|
Space Complexity: O(1)
|
2023-06-21 21:50:10 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
def int_to_roman(num):
|
|
|
|
reference = [("M", 1000), ("CM", 900), ("D", 500), ("CD", 400), ("C", 100), ("XC", 90), ("L", 50), ("XL", 40), ("X", 10), ("IX", 9), ("V", 5), ("IV", 4), ("I", 1)]
|
2023-06-23 00:20:55 +00:00
|
|
|
remainder = num
|
|
|
|
romans = ""
|
|
|
|
|
|
|
|
for i in range(len(reference)):
|
|
|
|
quotient = int(remainder / reference[i][1])
|
|
|
|
remainder = remainder % reference[i][1]
|
|
|
|
romans += reference[i][0] * quotient
|
|
|
|
|
|
|
|
return romans
|
2023-06-21 21:50:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
2023-06-23 00:20:55 +00:00
|
|
|
print(int_to_roman(3999))
|
|
|
|
# MMMDXLIX
|