From ba2bf9c57b9b4806436f9650ce7adedfe62bfbec Mon Sep 17 00:00:00 2001 From: Isabelle Viktoria Maciohsek Date: Wed, 7 Oct 2020 00:04:09 +0300 Subject: [PATCH] Add to_roman_numeral --- snippets/to_roman_numeral.md | 40 ++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 snippets/to_roman_numeral.md diff --git a/snippets/to_roman_numeral.md b/snippets/to_roman_numeral.md new file mode 100644 index 000000000..84bc7762f --- /dev/null +++ b/snippets/to_roman_numeral.md @@ -0,0 +1,40 @@ +--- +title: to_roman_numeral +tags: math,string,intermediate +--- + +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`, `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' +```