mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 06:46:18 -04:00
reorganize dir
Signed-off-by: Mia Steinkirch <mia.steinkirch@gmail.com>
This commit is contained in:
parent
1b6f705e7c
commit
a8e71c50db
276 changed files with 23954 additions and 0 deletions
52
other_resources/Project-Euler/089-roman_numbers.py
Normal file
52
other_resources/Project-Euler/089-roman_numbers.py
Normal file
|
@ -0,0 +1,52 @@
|
|||
#!/usr/bin/python
|
||||
|
||||
__author__ = "Mari Wahl"
|
||||
__email__ = "marina.w4hl@gmail.com"
|
||||
|
||||
'''
|
||||
The rules for writing Roman numerals allow for many ways of writing each number (see About Roman Numerals...). However, there is always a "best" way of writing a particular number.
|
||||
|
||||
For example, the following represent all of the legitimate ways of writing the number sixteen:
|
||||
|
||||
IIIIIIIIIIIIIIII
|
||||
VIIIIIIIIIII
|
||||
VVIIIIII
|
||||
XIIIIII
|
||||
VVVI
|
||||
XVI
|
||||
|
||||
The last example being considered 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, but not necessarily minimal, Roman numerals; that is, they are arranged in descending units and obey the subtractive pair rule (see About Roman Numerals... for the definitive rules for this problem).
|
||||
|
||||
Find the number of characters saved by writing each of these in their minimal form.
|
||||
'''
|
||||
|
||||
|
||||
|
||||
import os
|
||||
|
||||
def subtractive(roman):
|
||||
result = roman
|
||||
replacements = [
|
||||
("VIIII", "IX"),
|
||||
("IIII", "IV"),
|
||||
("LXXXX", "XC"),
|
||||
("XXXX", "XL"),
|
||||
("DCCCC", "CM"),
|
||||
("CCCC", "CD"),
|
||||
]
|
||||
for old, new in replacements:
|
||||
result = result.replace(old, new)
|
||||
return result
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
current = 0
|
||||
improved = 0
|
||||
for line in open(os.path.join(os.path.dirname(__file__), 'roman.txt')):
|
||||
roman = line.strip()
|
||||
current += len(roman)
|
||||
improved += len(subtractive(roman))
|
||||
print current - improved
|
Loading…
Add table
Add a link
Reference in a new issue