Skip to content

Instantly share code, notes, and snippets.

@pbassut
Created March 26, 2016 21:10
Show Gist options
  • Save pbassut/ed4f0e981e05c09c9cc7 to your computer and use it in GitHub Desktop.
Save pbassut/ed4f0e981e05c09c9cc7 to your computer and use it in GitHub Desktop.
UNFINISHED
import sys
import math
from collections import deque
# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.
table = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000
}
atable = {
1: 'I',
5: 'V',
10: 'X',
50: 'L',
100: 'C',
500: 'D',
1000: 'M'
}
convert = lambda x: table[x]
aconvert = lambda x: atable[x]
def find_max_index(roman):
current = 0
for index, letter in enumerate(list(roman)[1:]):
if convert(letter) > convert(roman[current]):
current = index + 1
return current
def process(roman):
pivot = find_max_index(roman)
return sum(map(convert, roman[pivot:])) - sum(map(convert, roman[:pivot]))
def encode(i):
digits = deque()
for v in sorted(atable.keys(), reverse=True):
while i >= v:
i -= v
digits.append(aconvert(v))
return ''.join(digits)
rom_1 = raw_input()
rom_2 = raw_input()
print encode(process(rom_1) + process(rom_2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment