-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path89.py
More file actions
48 lines (42 loc) · 1001 Bytes
/
Copy path89.py
File metadata and controls
48 lines (42 loc) · 1001 Bytes
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
from collections import OrderedDict
with open('p089_roman.txt') as f:
romans = f.read().split("\n")
roman_values = OrderedDict((
('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 generate_roman(num: int) -> str:
out = ""
while num > 0:
for roman, value in roman_values.items():
if num >= value:
out += roman
num -= value
break
return out
def roman_value(s: str) -> int:
out = 0
i = 0
while i < len(s):
cur = roman_values[s[i]]
if i < len(s)-1:
nxt = roman_values[s[i+1]]
if nxt > cur:
out += nxt - cur
i += 2
continue
out += cur
i += 1
return out
print(sum(len(roman) - len(generate_roman(roman_value(roman))) for roman in romans))