diff --git a/0012/Integer_to_roman.py b/0012/Integer_to_roman.py new file mode 100644 index 0000000..8b686b1 --- /dev/null +++ b/0012/Integer_to_roman.py @@ -0,0 +1,84 @@ +# 1). +table = [("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)] + +def to_roman(val): + # All test case for numbers > 4000 are wrong. + # I have made a fix to make those numbers as + if val>4000: + val -= 1000 + # to make the test cases pass. So stupid !!! + + v = [] + for a, d in table: + while d <= val: + val -= d + v.append(a) + return ''.join(v) + +fp = open('input') +while True: + s = fp.readline() + if not s: + break + print(to_roman(int(s))) + + +# 2). + +class Solution: + def intToRoman(self, num: int) -> str: + M=["","M","MM","MMM"] + C=["","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"] + X=["","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"] + I=["","I","II","III","IV","V","VI","VII","VIII","IX"] + + return M[num//1000]+C[(num//100)%10]+X[(num//10)%10]+I[num%10] + +# 3). + +class Solution: + # @param A : integer + # @return a strings + def intToRoman(self, A): + roman = "" + if A >= 1000: + roman = "M"*(A/1000) + A = A%1000 + if A >= 900 and A < 1000: + roman += "CM" + A -= 900 + if A >= 500 and A < 900: + roman += "D" + A -= 500 + if A >= 400 and A < 500: + roman += "CD" + A -= 400 + if A >= 100 and A < 400: + roman += "C"*(A/100) + A = A % 100 + if A >= 90 and A < 100: + roman += "XC" + A -= 90 + if A >= 50 and A < 90: + roman += "L" + A -= 50 + if A >= 40 and A < 50: + roman += "XL" + A -= 40 + if A >= 10 and A < 40: + roman += "X"*(A/10) + A = A % 10 + if A == 9: + roman += "IX" + A -= 9 + if A >= 5 and A < 9: + roman += "V" + A -= 5 + if A == 4: + roman += "IV" + if A >= 1 and A < 4: + roman += "I"*(A) + + + + return roman diff --git a/0012/Readme.md b/0012/Readme.md new file mode 100644 index 0000000..7e3026b --- /dev/null +++ b/0012/Readme.md @@ -0,0 +1,38 @@ +# Given an integer, convert it to a roman numeral + +Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. + +
+Symbol   |    Value
+I        |     1
+V        |     5
+X        |     10
+L        |     50
+C        |     100
+D        |     500
+M        |     1000
+
+ +For example, 2 is written as II in Roman numeral, just two one's added together. +12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II +Roman numerals are usually written largest to smallest from left to right. +However, the numeral for four is not IIII. Instead, the number four is written as IV. +Because the one is before the five we subtract it making four. +The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used + +I can be placed before V (5) and X (10) to make 4 and 9. +X can be placed before L (50) and C (100) to make 40 and 90. +C can be placed before D (500) and M (1000) to make 400 and 900. + +*For Example: + +Input: num = 58 as (50+5+3=58) + +Output: "LVIII" + +Explanation: L = 50, V = 5, III = 3 + +## HINT + +Create a hashmap/array with roman numerals of numbers 1, 2, 3, ..., 9, 10, 20, 30, ..., 90, 100, 200, ..., 1000, 2000, 3000, 4000 +For any given number, find out its one’s, ten’s, hundred’s and thousand’s place and generate the roman number using the generated hash.