Skip to content

Commit 0cae7de

Browse files
almost done with TDD
1 parent 1bc7ac7 commit 0cae7de

9 files changed

Lines changed: 1393 additions & 416 deletions

File tree

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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+

‎source/examples/test_driven_development/roman3.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ def test_to_roman_known_values():
108108

109109
def test_too_large():
110110
"""
111-
to_roman should raise an OutOfRangeError when passed
111+
to_roman should raise an ValueError when passed
112112
values over 3999
113113
"""
114-
with pytest.raises(OutOfRangeError):
114+
with pytest.raises(ValueError):
115115
to_roman(4000)
116116

117117

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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 n > 3999:
31+
raise ValueError("number out of range (must be less than 4000)")
32+
33+
result = ''
34+
for numeral, integer in roman_numeral_map:
35+
while n >= integer:
36+
result += numeral
37+
n -= integer
38+
return result
39+
40+
41+
## Tests for roman numeral conversion
42+
43+
KNOWN_VALUES = ( (1, 'I'),
44+
(2, 'II'),
45+
(3, 'III'),
46+
(4, 'IV'),
47+
(5, 'V'),
48+
(6, 'VI'),
49+
(7, 'VII'),
50+
(8, 'VIII'),
51+
(9, 'IX'),
52+
(10, 'X'),
53+
(50, 'L'),
54+
(100, 'C'),
55+
(500, 'D'),
56+
(1000, 'M'),
57+
(31, 'XXXI'),
58+
(148, 'CXLVIII'),
59+
(294, 'CCXCIV'),
60+
(312, 'CCCXII'),
61+
(421, 'CDXXI'),
62+
(528, 'DXXVIII'),
63+
(621, 'DCXXI'),
64+
(782, 'DCCLXXXII'),
65+
(870, 'DCCCLXX'),
66+
(941, 'CMXLI'),
67+
(1043, 'MXLIII'),
68+
(1110, 'MCX'),
69+
(1226, 'MCCXXVI'),
70+
(1301, 'MCCCI'),
71+
(1485, 'MCDLXXXV'),
72+
(1509, 'MDIX'),
73+
(1607, 'MDCVII'),
74+
(1754, 'MDCCLIV'),
75+
(1832, 'MDCCCXXXII'),
76+
(1993, 'MCMXCIII'),
77+
(2074, 'MMLXXIV'),
78+
(2152, 'MMCLII'),
79+
(2212, 'MMCCXII'),
80+
(2343, 'MMCCCXLIII'),
81+
(2499, 'MMCDXCIX'),
82+
(2574, 'MMDLXXIV'),
83+
(2646, 'MMDCXLVI'),
84+
(2723, 'MMDCCXXIII'),
85+
(2892, 'MMDCCCXCII'),
86+
(2975, 'MMCMLXXV'),
87+
(3051, 'MMMLI'),
88+
(3185, 'MMMCLXXXV'),
89+
(3250, 'MMMCCL'),
90+
(3313, 'MMMCCCXIII'),
91+
(3408, 'MMMCDVIII'),
92+
(3501, 'MMMDI'),
93+
(3610, 'MMMDCX'),
94+
(3743, 'MMMDCCXLIII'),
95+
(3844, 'MMMDCCCXLIV'),
96+
(3888, 'MMMDCCCLXXXVIII'),
97+
(3940, 'MMMCMXL'),
98+
(3999, 'MMMCMXCIX'),
99+
)
100+
101+
102+
def test_to_roman_known_values():
103+
"""
104+
to_roman should give known result with known input
105+
"""
106+
for integer, numeral in KNOWN_VALUES:
107+
result = to_roman(integer)
108+
assert numeral == result
109+
110+
111+
def test_too_large():
112+
"""
113+
to_roman should raise an ValueError when passed
114+
values over 3999
115+
"""
116+
with pytest.raises(ValueError):
117+
to_roman(4000)
118+
119+

0 commit comments

Comments
 (0)