Created
January 20, 2019 17:41
-
-
Save radeinla/73a5edac05b4cefc0c3d2c59c2102906 to your computer and use it in GitHub Desktop.
Numerals to Roman Numerals
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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