7 examples of 'roman to integer python' in Python

Every line of 'roman to integer python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
14def int_from_roman2(string):
15 acc = prev = 0
16 for c in reversed(string):
17 v = values[c]
18 if prev <= v: acc += v
19 else: acc -= v
20 prev = v
21 return acc
553def roman_to_int(n):
554 n = unicode(n).upper()
555
556 i = result = 0
557 for integer, numeral in numeral_map:
558 while n[i:i + len(numeral)] == numeral:
559 result += integer
560 i += len(numeral)
561 return result
2def romanToInt(self, s):
3 """
4 :type s: str
5 :rtype: int
6 """
7 ans = 0
8
9 if not s:
10 return ans
11
12 symbs = {
13 'I': 1,
14 'V': 5,
15 'X': 10,
16 'L': 50,
17 'C': 100,
18 'D': 500,
19 'M': 1000,
20 }
21
22 ans += symbs[s[-1]]
23
24 for i in range(len(s) - 2, -1, -1):
25 if symbs[s[i]] >= symbs[s[i + 1]]:
26 ans += symbs[s[i]]
27 else:
28 ans -= symbs[s[i]]
29
30 return ans
91def number_to_int(s):
92 """
93 Converts a numeric string to int. `s` can also be a Roman numeral.
94 """
95 try:
96 return int(s)
97 except ValueError:
98 try:
99 return fromRoman(s)
100 except RomanError as e:
101 raise ValueError(e)
64def romanToInt(self, s):
65 """
66 :type s: str
67 :rtype: int
68 """
69 roman = {"I":1, "V":5, "X":10, "L":50, "C":100, "D":500, "M":1000}
70 total = 0
71 for index in range(len(s)-1):
72 type = 1 if roman[s[index]]>=roman[s[index+1]] else -1
73 total += type*roman[s[index]]
74 return total + roman[s[len(s)-1]]
17def int_to_roman(num):
18
19 roman = OrderedDict()
20 roman[1000] = "M"
21 roman[900] = "CM"
22 roman[500] = "D"
23 roman[400] = "CD"
24 roman[100] = "C"
25 roman[90] = "XC"
26 roman[50] = "L"
27 roman[40] = "XL"
28 roman[10] = "X"
29 roman[9] = "IX"
30 roman[5] = "V"
31 roman[4] = "IV"
32 roman[1] = "I"
33
34 def roman_num(num):
35 for r in roman.keys():
36 x, y = divmod(num, r)
37 yield roman[r] * x
38 num -= (r * x)
39 if num > 0:
40 roman_num(num)
41 else:
42 break
43
44 return "".join([a for a in roman_num(num)])
22def to_roman(n):
23 '''convert integer to Roman numeral'''
24 result = ''
25 for numeral, integer in roman_numeral_map:
26 while n >= integer:
27 result += numeral
28 n -= integer
29 return result

Related snippets