1010
1111import pytest
1212
13- roman_numeral_map = (('M' , 1000 ),
13+ roman_numeral_map = (('M' , 1000 ),
1414 ('CM' , 900 ),
15- ('D' , 500 ),
15+ ('D' , 500 ),
1616 ('CD' , 400 ),
17- ('C' , 100 ),
17+ ('C' , 100 ),
1818 ('XC' , 90 ),
19- ('L' , 50 ),
19+ ('L' , 50 ),
2020 ('XL' , 40 ),
21- ('X' , 10 ),
21+ ('X' , 10 ),
2222 ('IX' , 9 ),
23- ('V' , 5 ),
23+ ('V' , 5 ),
2424 ('IV' , 4 ),
25- ('I' , 1 ))
25+ ('I' , 1 ))
2626
2727
2828def to_roman (n ):
@@ -47,25 +47,15 @@ def is_valid_roman_numeral(s):
4747 looking for valid characters and removing them to determine
4848 if this is, indeed, a valid Roman numeral
4949 """
50- # first check if uses only valid characters
51- for c in s :
52- if c not in "MDCLXVI" :
53- return False
54-
55- print ("starting to parse" )
56- print ("the thousands" )
57- print (f"{ s = } " )
5850 # first look for the thousands -- up to three Ms
5951 for _ in range (3 ):
6052 if s [:1 ] == "M" :
6153 s = s [1 :]
6254 # then look for the hundreds:
63- print ("the hundreds" )
64- print (f"{ s = } " )
6555 # there can be only one of CM, CD, or D:
66- if s [:2 ] == "CM" : # 900
56+ if s [:2 ] == "CM" : # 900
6757 s = s [2 :]
68- elif s [:2 ] == "CD" : # 400
58+ elif s [:2 ] == "CD" : # 400
6959 s = s [2 :]
7060 else :
7161 if s [:1 ] == "D" : # 500
@@ -75,8 +65,6 @@ def is_valid_roman_numeral(s):
7565 if s [:1 ] == "C" :
7666 s = s [1 :]
7767 # now the tens
78- print ("the tens" )
79- print (f"{ s = } " )
8068 # There can be one of either XC, XL or L
8169 if s [:2 ] == "XC" : # 90
8270 s = s [2 :]
@@ -90,8 +78,6 @@ def is_valid_roman_numeral(s):
9078 if s [:1 ] == "X" :
9179 s = s [1 :]
9280 # and the ones
93- print ("the ones" )
94- print (f"{ s = } " )
9581 # There can be one of IX, IV or V
9682 if s [:2 ] == "IX" : # 9
9783 s = s [2 :]
@@ -100,15 +86,11 @@ def is_valid_roman_numeral(s):
10086 else :
10187 if s [:1 ] == "V" : # 5
10288 s = s [1 :]
103- print ("looking for the Is" )
104- print (f"{ s = } " )
10589 # There can be up to three Is
10690 for _ in range (3 ):
10791 if s [:1 ] == "I" : # 1
10892 s = s [1 :]
10993 # if there is anything left, it's not a valid Roman numeral
110- print ("done" )
111- print (f"{ s = } " )
11294 if s :
11395 return False
11496 else :
@@ -125,7 +107,6 @@ def from_roman(s):
125107 while s [index :index + len (numeral )] == numeral :
126108 result += integer
127109 index += len (numeral )
128- # print(f'found, {numeral} of length, {len(numeral)} adding {integer}')
129110 return result
130111
131112
@@ -203,27 +184,27 @@ def test_to_roman_known_values():
203184
204185def test_too_large ():
205186 """
206- to_roman should raise an ValueError when passed
187+ to_roman should raise a ValueError when passed
207188 values over 3999
208189 """
209190 with pytest .raises (ValueError ):
210191 to_roman (4000 )
211192
212193
213194def test_zero ():
214- """to_roman should raise an ValueError with 0 input"""
195+ """to_roman should raise a ValueError with 0 input"""
215196 with pytest .raises (ValueError ):
216197 to_roman (0 )
217198
218199
219200def test_negative ():
220- """to_roman should raise an ValueError with negative input"""
201+ """to_roman should raise a ValueError with negative input"""
221202 with pytest .raises (ValueError ):
222203 to_roman (- 1 )
223204
224205
225206def test_non_integer ():
226- """to_roman should raise an ValueError with non-integer input"""
207+ """to_roman should raise a ValueError with non-integer input"""
227208 with pytest .raises (ValueError ):
228209 to_roman (0.5 )
229210
@@ -232,8 +213,10 @@ def test_float_with_integer_value():
232213 """to_roman should work for floats with integer values"""
233214 assert to_roman (3.0 ) == "III"
234215
216+
235217# ####################
236218# Tests for from_roman
219+ # ####################
237220
238221
239222def test_from_roman_known_values ():
@@ -251,7 +234,6 @@ def test_roundtrip():
251234 assert integer == result
252235
253236
254- # #####################
255237# The following to test for valid roman numerals
256238
257239def test_invalid_character ():
@@ -290,5 +272,3 @@ def test_malformed_antecedents():
290272 with pytest .raises (ValueError ):
291273 print (f"trying: { s } " )
292274 from_roman (s )
293-
294-
0 commit comments