Question: http://oj.leetcode.com/problems/integer-to-roman/
Question Name: Integer to Roman
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | class Solution: # @return a string def intToRoman(self, num): result = "" # In the first round, we will generate the Romen numbers without # considering the issue about four repeating characters. transTable = {1:"I", 5:"V", 10:"X", 50:"L", 100:"C", 500:"D", 1000:"M"} for trans in sorted(transTable.keys())[::-1]: while num != 0 and num >= trans: num -= trans result += transTable[trans] if num == 0: break # In the second round, we will replace the quanlified four-repeating- # character with the subtractive notation simplifyTable = {"DCCCC":"CM", "CCCC":"CD", "LXXXX":"XC", "XXXX":"XL", "VIIII":"IX", "IIII":"IV"} # Consider the largest four repeating characters first. simplifyContent = ["DCCCC", "CCCC", "LXXXX", "XXXX", "VIIII", "IIII"] for item in simplifyContent: if item in result: result = result.replace(item, simplifyTable[item]) return result |