Last active
February 5, 2020 10:09
-
-
Save cal97g/dab2952e58d7a9abd30bed6a5cf330ac to your computer and use it in GitHub Desktop.
A 'broken' roman numeral converter
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
roman_to_arabic = {"I": 1,"V": 5,"X": 10,"L": 50,"C": 100,"D": 500,"M": 1000} | |
valid_subtractions = {"IV", "IX", "XL", "XC", "CD", "CM"} | |
def solution(s): | |
s = "".join(list(s.upper())) | |
for c in s: | |
if c not in roman_to_arabic.keys(): | |
return 0 | |
rta = roman_to_arabic | |
total = 0 | |
skipnext = False | |
for i in range(len(s)): | |
if skipnext: | |
skipnext = False | |
continue | |
try: | |
if rta[s[i]] < rta[s[i+1]]: | |
if str(s[i]+s[i+1]) in valid_subtractions: | |
total += rta[s[i+1]] - rta[s[i]] | |
skipnext = True | |
else: | |
total += rta[s[i]] | |
except IndexError as e: | |
total += rta[s[i]] | |
return total | |
assert(solution("MCMXCVIIII") == 1999) | |
assert(solution("MCMXCIX") == 1999) | |
assert(solution("MDCCCCLXXXXVIIII") == 1999) | |
assert(solution("XX") == 20) | |
assert(solution("XIV") == 14) | |
assert(solution("VII") == 7) | |
assert(solution("XXXIV") == 34) | |
assert(solution("XXXVII") == 37) | |
assert(solution("XLIII") == 43) | |
assert(solution("XLVIII") == 48) | |
assert(solution("LII") == 52) | |
assert(solution("CXL") == 140) | |
assert(solution("CXLV") == 145) | |
assert(solution("XIII") == 13) | |
assert(solution("XIV") == 14) | |
assert(solution("XVIII") == 18) | |
assert(solution("XXVI") == 26) | |
assert(solution("XXXIX") == 39) | |
assert(solution("XXXXII") == 42) | |
assert(solution("LXI") == 61) | |
assert(solution("DXLV") == 545) | |
assert(solution("CCXVII") == 217) | |
assert(solution("MLXVI") == 1066) | |
assert(solution("I") == 1) | |
assert(solution("II") == 2) | |
assert(solution("III") == 3) | |
assert(solution("V") == 5) | |
assert(solution("VI") == 6) | |
assert(solution("VII") == 7) | |
assert(solution("VIII") == 8) | |
assert(solution("IV") == 4) | |
assert(solution("X") == 10) | |
assert(solution("IX") == 9) | |
assert(solution("XIV") == 14) | |
assert(solution("XIX") == 19) | |
assert(solution("XI") == 11) | |
assert(solution("XIII") == 13) | |
assert(solution("XVIII") == 18) | |
assert(solution("XX") == 20) | |
assert(solution("XXVIII") == 28) | |
assert(solution("XXIX") == 29) | |
assert(solution("XXXIX") == 39) | |
assert(solution("L") == 50) | |
assert(solution("LIII") == 53) | |
assert(solution("LVII") == 57) | |
assert(solution("XL") == 40) | |
assert(solution("XLIX") == 49) | |
assert(solution("LIX") == 59) | |
assert(solution("LXXIX") == 79) | |
assert(solution("C") == 100) | |
assert(solution("XC") == 90) | |
assert(solution("XCIX") == 99) | |
assert(solution("CC") == 200) | |
assert(solution("D") == 500) | |
assert(solution("CDXCIX") == 499) | |
assert(solution("CMXCIX") == 999) | |
assert(solution("MMCMXCIX") == 2999) | |
assert(solution("MMMCMXCIX") == 3999) | |
assert(solution("CMI") == 901) | |
assert(solution("CMXXVI") == 926) | |
assert(solution("CMLI") == 951) | |
assert(solution("CMLXXVI") == 976) | |
assert(solution("CMII") == 902) | |
assert(solution("CMXXVII") == 927) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment