Starting from:

$20

CS3723-Assignment 7 Python Solved

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:).

Given a number, return a Roman Numeral string. (10 points)
Any number <= 0 or 3,999, or invalid number, return "Error".
Use only these standard values (wikipedia.org/wiki/Roman_numerals).
Assume the input is a number and not a string.
1 = I

2 = II

3 = III

4 = IV

5 = V

6 = VI

7 = VII

8 = VIII

9 = IX
10 = X

20 = XX

30 = XXX

40 = XL

50 = L

60 = LX

70 = LXX

80 = LXXX

90 = XC

100 = C

200 = CC

300 = CCC

400 = CD

500 = D

600 = DC

700 = DCC

800 = DCCC

900 = CM
1000 = M

2000 = MM

3000 = MMM

 

Examples:

74 = LXXIV

299 = CCXCIX

2019 = MMXIX

 

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:

Look at thousands then hundreds then tens then ones.
Create 4 Lists and put "" for the zero values.
E.g., tens = ["", "X", "XX", "XXX", "XL", "L", …, "XC"]

Use // for integer divide and % for remainder.
 

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.
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:

Use 4 dictionaries instead of Lists. Use dict.items() method.
E.g., tens = {"XC": 90, "LXXX": 80, "LXX": 70, …, "X": 10}

Very Important: Put each dictionary in reverse order
E.g., look for XC then LXXX then LXX then LX then L, etc.

Use the str.upper() and str.startswith() methods.
 

More products