|
| 1 | +""" |
| 2 | +roman.py |
| 3 | +
|
| 4 | +A Roman numeral to arabic numeral (and back!) converter |
| 5 | +
|
| 6 | +complete with tests |
| 7 | +
|
| 8 | +tests are expected to be able to be run with the pytest system |
| 9 | +""" |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | +roman_numeral_map = (('M', 1000), |
| 14 | + ('CM', 900), |
| 15 | + ('D', 500), |
| 16 | + ('CD', 400), |
| 17 | + ('C', 100), |
| 18 | + ('XC', 90), |
| 19 | + ('L', 50), |
| 20 | + ('XL', 40), |
| 21 | + ('X', 10), |
| 22 | + ('IX', 9), |
| 23 | + ('V', 5), |
| 24 | + ('IV', 4), |
| 25 | + ('I', 1)) |
| 26 | + |
| 27 | + |
| 28 | +def to_roman(n): |
| 29 | + """convert integer to Roman numeral""" |
| 30 | + if not (0 < n < 4000): |
| 31 | + raise ValueError("number out of range (must be 1..3999)") |
| 32 | + |
| 33 | + if int(n) != n: |
| 34 | + raise ValueError("Only integers can be converted to Roman numerals") |
| 35 | + |
| 36 | + result = '' |
| 37 | + for numeral, integer in roman_numeral_map: |
| 38 | + while n >= integer: |
| 39 | + result += numeral |
| 40 | + n -= integer |
| 41 | + return result |
| 42 | + |
| 43 | + |
| 44 | +def from_roman(s): |
| 45 | + """convert Roman numeral to integer""" |
| 46 | + result = 0 |
| 47 | + index = 0 |
| 48 | + for numeral, integer in roman_numeral_map: |
| 49 | + while s[index:index + len(numeral)] == numeral: |
| 50 | + result += integer |
| 51 | + index += len(numeral) |
| 52 | + # print(f'found, {numeral} of length, {len(numeral)} adding {integer}') |
| 53 | + return result |
| 54 | + |
| 55 | + |
| 56 | +##################################### |
| 57 | +# Tests for roman numeral conversion |
| 58 | +##################################### |
| 59 | + |
| 60 | +KNOWN_VALUES = ( (1, 'I'), |
| 61 | + (2, 'II'), |
| 62 | + (3, 'III'), |
| 63 | + (4, 'IV'), |
| 64 | + (5, 'V'), |
| 65 | + (6, 'VI'), |
| 66 | + (7, 'VII'), |
| 67 | + (8, 'VIII'), |
| 68 | + (9, 'IX'), |
| 69 | + (10, 'X'), |
| 70 | + (50, 'L'), |
| 71 | + (100, 'C'), |
| 72 | + (500, 'D'), |
| 73 | + (1000, 'M'), |
| 74 | + (31, 'XXXI'), |
| 75 | + (148, 'CXLVIII'), |
| 76 | + (294, 'CCXCIV'), |
| 77 | + (312, 'CCCXII'), |
| 78 | + (421, 'CDXXI'), |
| 79 | + (528, 'DXXVIII'), |
| 80 | + (621, 'DCXXI'), |
| 81 | + (782, 'DCCLXXXII'), |
| 82 | + (870, 'DCCCLXX'), |
| 83 | + (941, 'CMXLI'), |
| 84 | + (1043, 'MXLIII'), |
| 85 | + (1110, 'MCX'), |
| 86 | + (1226, 'MCCXXVI'), |
| 87 | + (1301, 'MCCCI'), |
| 88 | + (1485, 'MCDLXXXV'), |
| 89 | + (1509, 'MDIX'), |
| 90 | + (1607, 'MDCVII'), |
| 91 | + (1754, 'MDCCLIV'), |
| 92 | + (1832, 'MDCCCXXXII'), |
| 93 | + (1993, 'MCMXCIII'), |
| 94 | + (2074, 'MMLXXIV'), |
| 95 | + (2152, 'MMCLII'), |
| 96 | + (2212, 'MMCCXII'), |
| 97 | + (2343, 'MMCCCXLIII'), |
| 98 | + (2499, 'MMCDXCIX'), |
| 99 | + (2574, 'MMDLXXIV'), |
| 100 | + (2646, 'MMDCXLVI'), |
| 101 | + (2723, 'MMDCCXXIII'), |
| 102 | + (2892, 'MMDCCCXCII'), |
| 103 | + (2975, 'MMCMLXXV'), |
| 104 | + (3051, 'MMMLI'), |
| 105 | + (3185, 'MMMCLXXXV'), |
| 106 | + (3250, 'MMMCCL'), |
| 107 | + (3313, 'MMMCCCXIII'), |
| 108 | + (3408, 'MMMCDVIII'), |
| 109 | + (3501, 'MMMDI'), |
| 110 | + (3610, 'MMMDCX'), |
| 111 | + (3743, 'MMMDCCXLIII'), |
| 112 | + (3844, 'MMMDCCCXLIV'), |
| 113 | + (3888, 'MMMDCCCLXXXVIII'), |
| 114 | + (3940, 'MMMCMXL'), |
| 115 | + (3999, 'MMMCMXCIX'), |
| 116 | + ) |
| 117 | + |
| 118 | + |
| 119 | +def test_to_roman_known_values(): |
| 120 | + """ |
| 121 | + to_roman should give known result with known input |
| 122 | + """ |
| 123 | + for integer, numeral in KNOWN_VALUES: |
| 124 | + result = to_roman(integer) |
| 125 | + assert numeral == result |
| 126 | + |
| 127 | + |
| 128 | +def test_too_large(): |
| 129 | + """ |
| 130 | + to_roman should raise an ValueError when passed |
| 131 | + values over 3999 |
| 132 | + """ |
| 133 | + with pytest.raises(ValueError): |
| 134 | + to_roman(4000) |
| 135 | + |
| 136 | + |
| 137 | +def test_zero(): |
| 138 | + """to_roman should raise an ValueError with 0 input""" |
| 139 | + with pytest.raises(ValueError): |
| 140 | + to_roman(0) |
| 141 | + |
| 142 | + |
| 143 | +def test_negative(): |
| 144 | + """to_roman should raise an ValueError with negative input""" |
| 145 | + with pytest.raises(ValueError): |
| 146 | + to_roman(-1) |
| 147 | + |
| 148 | + |
| 149 | +def test_non_integer(): |
| 150 | + """to_roman should raise an ValueError with non-integer input""" |
| 151 | + with pytest.raises(ValueError): |
| 152 | + to_roman(0.5) |
| 153 | + |
| 154 | + |
| 155 | +def test_float_with_integer_value(): |
| 156 | + """to_roman should work for floats with integer values""" |
| 157 | + assert to_roman(3.0) == "III" |
| 158 | + |
| 159 | +# #################### |
| 160 | +# Tests for from_roman |
| 161 | + |
| 162 | + |
| 163 | +def test_from_roman_known_values(): |
| 164 | + """from_roman should give known result with known input""" |
| 165 | + for integer, numeral in KNOWN_VALUES: |
| 166 | + result = from_roman(numeral) |
| 167 | + assert integer == result |
| 168 | + |
| 169 | + |
| 170 | +def test_roundtrip(): |
| 171 | + """from_roman(to_roman(n))==n for all n""" |
| 172 | + for integer in range(1, 4000): |
| 173 | + numeral = to_roman(integer) |
| 174 | + result = from_roman(numeral) |
| 175 | + assert integer == result |
| 176 | + |
| 177 | + |
| 178 | +# ##################### |
| 179 | +# The following to test for valid roman numerals |
| 180 | + |
| 181 | +def test_invalid_character(): |
| 182 | + """ |
| 183 | + Roman numerals can only use these characters: |
| 184 | +
|
| 185 | + M, D, C, L, X, V, I |
| 186 | +
|
| 187 | + This tests that other characters will cause a failure |
| 188 | + """ |
| 189 | + for s in ['Z', 'XXIIIQ', 'QXXIII', 'XXYIII']: |
| 190 | + with pytest.raises(ValueError): |
| 191 | + from_roman(s) |
| 192 | + |
| 193 | + |
| 194 | +def test_too_many_repeated_numerals(): |
| 195 | + '''from_roman should fail with too many repeated numerals''' |
| 196 | + for s in ('MMMM', 'DD', 'CCCC', 'LL', 'XXXX', 'VV', 'IIII'): |
| 197 | + with pytest.raises(ValueError): |
| 198 | + from_roman(s) |
| 199 | + |
| 200 | + |
| 201 | +def test_repeated_pairs(): |
| 202 | + '''from_roman should fail with repeated pairs of numerals''' |
| 203 | + for s in ('CMCM', 'CDCD', 'XCXC', 'XLXL', 'IXIX', 'IVIV'): |
| 204 | + with pytest.raises(ValueError): |
| 205 | + from_roman(s) |
| 206 | + |
| 207 | + |
| 208 | +def test_malformed_antecedents(): |
| 209 | + '''from_roman should fail with malformed antecedents''' |
| 210 | + for s in ('IIMXCC', 'VX', 'DCM', 'CMM', 'IXIV', |
| 211 | + 'MCMC', 'XCX', 'IVI', 'LM', 'LD', 'LC'): |
| 212 | + with pytest.raises(ValueError): |
| 213 | + from_roman(s) |
| 214 | + |
| 215 | + |
0 commit comments