Euler Solution 89

From ProgSoc Wiki

Jump to: navigation, search

Solutions to Problem 89

Calculate the number of characters saved by reducing a list of roman numerals to their shortest variants following certain rules. (eg XVIIII = XIX)

Python by Althalus

Runtime: 1ms

I did write functions to convert roman numerals to numbers, and numbers to roman numerals, but realised there was a better way before I got it working right. The information given in the problem is enough to identify exactly what we can change to minimise these numerals.

file = open('roman.txt')
numerals = file.read()
count = len(numerals)

numerals = numerals.replace('VIIII', 'IX')
numerals = numerals.replace('IIII', 'IV')
numerals = numerals.replace('LXXXX', 'XC')
numerals = numerals.replace('XXXX', 'XL')
numerals = numerals.replace('DCCCC', 'CM')
numerals = numerals.replace('CCCC', 'CD')

count -= len(numerals)
print count
Personal tools