For a number written in Roman numerals to be considered valid there is basic rules which must be followed. Even though the rules allow some numbers to being expressed in more than one-to-one-to-a-one-to-one-to-a-"best-of-a-type" Rticular number.
For example, it would appear that there is at least six ways of writing the number sixteen:
Iiiiiiiiiiiiiiii
Viiiiiiiiiii
Vviiiiii
Xiiiiii
Vvvi
XVI
However, according to the rules of XIIIIII and XVI are valid, and the last example was considered to be The most efficient, as it uses the least number of numerals.
The 11K text file, Roman.txt (right click and ' Save link/target as ... '), contains one thousand numbers written in valid, B UT not necessarily minimal, Roman numerals; See About ... Roman numerals for the definitive rules for this problem.
Find the number of characters saved by writing each of the these in their minimal form.
Note:you can assume that all the Roman numerals in the file contain no more than four consecutive identical units.
The simplest form of simplification is simply to encounter DCCCC into cm,lxxxx into XC,VIIII into IX,IIII into iv,xxxx into a CD
Anyway, these characters will eventually become 2 characters.
Import Reans=0for Line on open ("Roman.txt"): A=len (line) line=re.sub (' dcccc| Lxxxx| viiii| cccc| Xxxx| IIII ', ' AA ', line) B=len (line) ans=ans+a-bprint (ans)
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Project Euler:problem Roman Numerals