Using Python to parse roman numerals using Regex -
this question has answer here:
- converting roman numerals integers in python 12 answers
i need convert roman numeral string integer. have no clue how start, need use regex.
import re def romannumeraltoint(romannum): romannum = romannum.upper() totalvalue = 0
i have series of tests should past:
def test(): print("tests started.") x = "iii" "" if romannumeraltoint(x) == 3 else print(x + " - " + str(romannumeraltoint(x))) x = "iv" "" if romannumeraltoint(x) == 4 else print(x + " - " + str(romannumeraltoint(x))) x = "ix" "" if romannumeraltoint(x) == 9 else print(x + " - " + str(romannumeraltoint(x))) x = "c" "" if romannumeraltoint(x) == 100 else print(x + " - " + str(romannumeraltoint(x))) x = "cc" "" if romannumeraltoint(x) == 200 else print(x + " - " + str(romannumeraltoint(x))) x = "ccc" "" if romannumeraltoint(x) == 300 else print(x + " - " + str(romannumeraltoint(x))) x = "cd" "" if romannumeraltoint(x) == 400 else print(x + " - " + str(romannumeraltoint(x))) x = "d" "" if romannumeraltoint(x) == 500 else print(x + " - " + str(romannumeraltoint(x))) x = "dc" "" if romannumeraltoint(x) == 600 else print(x + " - " + str(romannumeraltoint(x))) x = "dcc" "" if romannumeraltoint(x) == 700 else print(x + " - " + str(romannumeraltoint(x))) x = "dccc" "" if romannumeraltoint(x) == 800 else print(x + " - " + str(romannumeraltoint(x))) x = "m" "" if romannumeraltoint(x) == 1000 else print(x + " - " + str(romannumeraltoint(x))) x = "lxi" "" if romannumeraltoint(x) == 61 else print(x + " - " + str(romannumeraltoint(x))) x = "ic" "" if romannumeraltoint(x) == 99 else print(x + " - " + str(romannumeraltoint(x))) x = "mmci" "" if romannumeraltoint(x) == 2101 else print(x + " - " + str(romannumeraltoint(x))) print("tests ended.")
you can use regex split string ccmmi
3 parts: string of largest value ("mm"), whatever's left of subtracted, , whatever's right of added.
you have trivial problem of finding value of string of single value (mm
), , 2 smaller problems of finding value of 2 roman numerals don't include "m" (cc
, i
). can divide , conquer until base case of empty string, 0.
here's suboptimal implementation of this, lot more matching necessary , has no input validation (wouldn't want ruin fun):
import re values = [ ("m", 1000), ("d", 500), ("c", 100), ("l", 50), ("x", 10), ("v", 5), ("i", 1) ] def romannumeraltoint(romannum): (c, v) in values: match = re.match("(.*?)(" + c + "+)(.*)", romannum) if match: return len(match.group(2)) * v \ - romannumeraltoint(match.group(1)) \ + romannumeraltoint(match.group(3)) return 0
Comments
Post a Comment