Skip to content

Instantly share code, notes, and snippets.

@radeinla
Created January 20, 2019 17:41
Show Gist options
  • Save radeinla/73a5edac05b4cefc0c3d2c59c2102906 to your computer and use it in GitHub Desktop.
Save radeinla/73a5edac05b4cefc0c3d2c59c2102906 to your computer and use it in GitHub Desktop.
Numerals to Roman Numerals
MAPPING = [None]
RAW = ['I', 'V', 'X', 'L', 'C', 'D', 'M']
n = 0
last = ''
places = [1, 2, 3]
for place in places:
MAPPING.append(dict())
SET = RAW[(place-1)*2: ((place-1)*2)+3]
for i in range(1, 10):
if i < 4:
MAPPING[place][str(i)] = SET[0]*i
elif i == 4:
MAPPING[place][str(i)] = SET[0]+SET[1]
elif i == 5:
MAPPING[place][str(i)] = SET[1]
elif i < 9:
MAPPING[place][str(i)] = SET[1]+(SET[0]*(i-5))
else:
MAPPING[place][str(i)] = SET[0]+SET[2]
MAPPING.append({"1": "M"})
def convert(s):
i = len(s)
ret = ""
for c in s:
ret += MAPPING[i][c]
i -= 1
return ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment