Project Euler:problem Roman Numerals

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.