π€ Converting Roman NumeralsΒΆ
I took this picture on the island of Gozo at the citadel of the city of Vittorija. The island is a small island that is part of the country of Malta.
Malta has been conquered by just about everyone at one point or another in their history, including the Romans, who presumably gave us roman numerals, and example of which you can see above the arch in the picture. But, there is a problem with this one. Do you know what it is?
As I was standing there looking at it I said to my self βWhat kind of Roman numeral is MDCCCCIV !? You are not supposed to have four of the same letter in a row. That breaks the rules. Iβm pretty sure they were trying to represent the number 1904, The question is how would you correctly represent that if you followed the rules for roman numerals?
If you are not familiar with the rules let me tell you.
Repeating a numeral up to three times represents the addition of the number. For example the common III is 1+1+1 = 3
Only I, X, C, and M can be repeated. V, L, and D should not be as there is no reason to do so. VV would simply be X and LL would be written as C
Writing numerals that decrease in values from left to right represents addition. For example MC represents 1000 + 100 = 1100 and XVI represents 10 + 5 + 1 or 16.
To write a number that would otherwise take repeating of a numeral 4 or more times there is a negation rule. Writing a smaller numeral to the left of a larger numeral causes the sign of then number on the left to be negative. for example IV is -1 + 5 = 4. Or MCMXIV is 1000 - 100 + 1000 + 10 - 1 + 5 or 1914.
Knowing all of that you can now re-write 1904 in a proper way.
-
: Match the roman numerals to the correct arabic numbers
Make sure you follow all the rules.
- MCD
- 1400
- MCMXCVIII
- 1998
- MCMXCIX
- 1999
- MMDXLIX
- 2549
- MMCCCXLV
- 2345
Converting Roman to ArabicΒΆ
Now that you are an expert in converting roman numerals to numbers its time to save yourself the trouble and write a program that does it for you. Hereβs an outline of what you should do.
Ask the user to enter a roman numeral
Validate the number to make sure it follows all the rules. The number in the picture should cause you to print an error message.
If the roman numeral is valid then you should convert it to its Arabic equivalent.
Print out the roman numeral and its Arabic equivalent.
Hint: You will want to start by using a dictionary to map each single character roman numeral to the correct Arabic number.
Converting Arabic To RomanΒΆ
Can you write a program to go the opposite direction? Given an Arabic number can you convert it to a roman numeral? This program uses a special kind of dictionary that preserves the order so that if you iterate over the keys you know you are going to get them in the same order you created them. (This is the default behavior for dictionaries in Python 3.6 and later, but not for our browser Python)
Can you explain why the dictionary used for this part uses the keys and values it does? Why does it need to be ordered? Why does this dictionary have more entries in it than the dictionary you used for part 1?
Super Challenge:
Write a program that converts an incorrectly repeated number like CCCC into the proper form of CD
Write a program that converts a correctly negated roman numeral into a non-negated incorrect equivalent. For example IV becomes IIII
If you can do the first two parts then you are ready to do addition of roman numerals WITHOUT converting them to Arabic first! This one might take some extra research beyond what we have covered in class up to this point.
To add two roman numerals follow these steps:
Convert any negated prefixes to additive suffixes. So, for example, IX would be rewritten to VIIII.
Concatenate the two strings you want to add.
Sort the letters, large to small. (research required)
If youβve made it this far you have an answer but its not the simplest answer. Keep going.
Do internal sums (e.g., replace βIIIIIβ with βVβ)
Convert back to subtractive prefixes. (e.g. replace βXIIIIβ with βXIVβ)
Post Project Questions
-
During this project I was primarily in my...
- 1. Comfort Zone
- 2. Learning Zone
- 3. Panic Zone
-
Completing this project took...
- 1. Very little time
- 2. A reasonable amount of time
- 3. More time than is reasonable
-
Based on my own interests and needs, the things taught in this project...
- 1. Don't seem worth learning
- 2. May be worth learning
- 3. Are definitely worth learning
-
For me to master the things taught in this project feels...
- 1. Definitely within reach
- 2. Within reach if I try my hardest
- 3. Out of reach no matter how hard I try