-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroman.py3
executable file
·64 lines (59 loc) · 1.6 KB
/
roman.py3
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
57
58
59
60
61
62
63
#!/usr/bin/python3
from sys import argv
try:
FILE = argv[1]
except NameError:
FILE = 'tests/106'
DATA = open(FILE, 'r').read().splitlines()
def intToRoman(num):
roman = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000 }
# list of tuples
SR = sorted(roman.items(), key=lambda x: x[1], reverse=True)
# replacements (we could do the replacements algorithmically
repls = {'IIII':'IV',
'VIIII':'IX',
'XIIII':'XIV',
'LIIII':'LIV',
'XXXX':'XL',
'CXXXX':'CXL',
'LXXXX':'XC',
'CIIII':'CIV',
'CCCC':'CD',
'DIIII':'DIV',
'DXXXX':'DXL',
'DCCCC':'CM',
'MIIII':'MIV',
'MXXXX':'MXL',
'MCCCC':'MCD',
}
out = ''
for numeral, amount in SR:
count = 0
while num >= amount:
num -= amount
out += numeral
count += 1
if count > 3:
#print('count > 3!!, last 5 in out: ', out[-5::])
inx = out[-5::]
out = out[:-5]
out += repls[inx]
return out
for line in DATA:
if not line:
continue
num = int(line)
out = intToRoman(num)
#out = ''
#for numeral, amount in SR:
#count = 0
#while num >= amount:
#num -= amount
#out += numeral
#count += 1
#if count > 3:
#print('count > 3!!, last 5 in out: ', out[-5::])
#inx = out[-5::]
#out = out[:-5]
#out += repls[inx]
print(out)