Description
Write two Python programs. One that converts a number to a Roman Numeral (roman.py), and the other converts it back to a number(namor.py). Each one should accept one or more values using sys.argv(1:).
For both programs, you MUST use a function to do the conversion. For roman.py, define a function that takes a number and returns the string. For namor.py, the function takes a string and returns a number.
You MUST submit code and some sample output for each (submit 2 .py files and 2 output files). Some comments should be present in your code (no fixed rules) and a little bit of discussion is encouraged.
I. Given a number, return a Roman Numeral string. (10 points)
1. Any number <= 0 or > 3,999, or invalid number, return “Error”.
2. Use only these standard values (en.wikipedia.org/wiki/Roman_numerals).
3. Assume the input is a number and not a string.
1 = I 10 = X 100 = C 1000 = M
2 = II 20 = XX 200 = CC 2000 = MM
3 = III 30 = XXX 300 = CCC 3000 = MMM
4 = IV 40 = XL 400 = CD 5 = V 50 = L 500 = D
6 = VI 60 = LX 600 = DC Examples:
7 = VII 70 = LXX 700 = DCC 74 = LXXIV
8 = VIII 80 = LXXX 800 = DCCC 299 = CCXCIX
Examples:
roman.py 812 43 prints “812 is DCCCXII” and “43 is XLIII”
roman.py 49 5000 prints “49 is XLIX” and “5000 is Error”
Hints for Part I:
1. Look at thousands then hundreds then tens then ones.
2. Create 4 Lists and put “” for the zero values.
E.g., tens = [“”, “X”, “XX”, “XXX”, “XL”, “L”, …, “XC”] 3. Use // for integer divide and % for remainder.
II. Given a Roman Numeral String, parse it and return the number. If it does not match a legal number from above (in the range 1 to 3,999), return -1. (20 points)
Examples:
namor.py CCCXIV prints “CCCXIV is 314” namor.py MCMXCIX prints “MCMXCIX is 1999” namor.py CXXXXI prints “CXXXXI is -1”
namor.py 123 MMMM II prints “123 is -1” and “MMMM is -1” and “II is 2” Hints for Part II:
1. Use 4 dictionaries instead of Lists. Use dict.items() method. E.g., tens = {“XC”: 90, “LXXX”: 80, “LXX”: 70, …, “X”: 10}
2. Very Important: Put each dictionary in reverse order E.g., look for XC then LXXX then LXX then LX then L, etc.
3. Use the str.upper() and str.startswith() methods.
Pulling a solution off the internet or from other people or sources is a severe violation of our Code of Conduct. Please do not do this. You learn nothing, and I will turn you in. It also won’t help you on the Final exam.
Reviews
There are no reviews yet.