-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathromanToInteger.py
More file actions
56 lines (52 loc) · 1.5 KB
/
romanToInteger.py
File metadata and controls
56 lines (52 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def romanToInt(s: str) -> int:
sum = 0
# find the first element
first = s[0]
match first:
case 'I':
sum += 1
case 'V':
sum += 5
case 'X':
sum += 10
case 'L':
sum += 50
case 'C':
sum += 100
case 'D':
sum += 500
case 'M':
sum += 1000
if len(s) > 1:
# perform calculations on the rest of s
for charIndex in range(1, len(s)):
match s[charIndex]:
case 'I':
sum += 1
case 'V':
if s[charIndex-1] == 'I':
sum += 4
sum += 5
case 'X':
if s[charIndex-1] == 'I':
sum += 9
sum += 10
case 'L':
if s[charIndex-1] == 'X':
sum += 40
sum += 50
case 'C':
if s[charIndex-1] == 'X':
sum += 90
sum += 100
case 'D':
if s[charIndex-1] == 'C':
sum += 400
sum += 500
case 'M':
if s[charIndex-1] == 'C':
sum += 900
sum += 1000
return sum
romanNumeral = "XIV"
num = romanToInt(romanNumeral)