From e2e04aa9d09eb46d6432b09f39120686dacf91b4 Mon Sep 17 00:00:00 2001 From: Matthew Volpe <69683445+mVolpe94@users.noreply.github.com> Date: Thu, 22 Jun 2023 20:20:55 -0400 Subject: [PATCH] Added algorithm to convert any integer to roman numerals --- algorithms/Python/strings/int-to-roman.py | 24 +++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/algorithms/Python/strings/int-to-roman.py b/algorithms/Python/strings/int-to-roman.py index 244b3fa7..7d1dfce8 100644 --- a/algorithms/Python/strings/int-to-roman.py +++ b/algorithms/Python/strings/int-to-roman.py @@ -1,23 +1,31 @@ ''' Output the correct Roman numerals for a given number. +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. + Input: 1994 Output: MCMXCIV - MDCD -Time Complexity: ??? -Space Complexity: ??? +Time Complexity: O(1) +Space Complexity: O(1) ''' 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)] + 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 - for numeral in range(len(reference)): - if numeral[1]: - ... if __name__ == "__main__": - print(int_to_roman(1994)) - # MCMXCIV \ No newline at end of file + print(int_to_roman(3999)) + # MMMDXLIX \ No newline at end of file