Skip to content

Commit

Permalink
- added support for 0 / N
Browse files Browse the repository at this point in the history
  • Loading branch information
dataflake committed Apr 14, 2019
1 parent 8e8e65a commit 88bdf83
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ CHANGES
3.2 (unreleased)
----------------

- Added support for 0 -> N
(see https://en.wikipedia.org/wiki/Roman_numerals#Zero)

- Added support for Python 3.8


Expand Down
13 changes: 11 additions & 2 deletions src/roman.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ def toRoman(n):
"""convert integer to Roman numeral"""
if not isinstance(n, int):
raise NotIntegerError("decimals can not be converted")
if not (0 < n < 5000):
raise OutOfRangeError("number out of range (must be 1..4999)")
if not (-1 < n < 5000):
raise OutOfRangeError("number out of range (must be 0..4999)")

# special case
if n == 0:
return 'N'

result = ""
for numeral, integer in romanNumeralMap:
Expand All @@ -68,6 +72,11 @@ def fromRoman(s):
"""convert Roman numeral to integer"""
if not s:
raise InvalidRomanNumeralError('Input can not be blank')

# special case
if s == 'N':
return 0

if not romanNumeralPattern.search(s):
raise InvalidRomanNumeralError('Invalid Roman numeral: %s' % s)

Expand Down
2 changes: 2 additions & 0 deletions src/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
class TestRoman(unittest.TestCase):

def test_toRoman(self):
self.assertEqual(roman.toRoman(0), 'N')
self.assertEqual(roman.toRoman(1), 'I')
self.assertEqual(roman.toRoman(2013), 'MMXIII')

Expand All @@ -12,6 +13,7 @@ def test_toRoman_errors(self):
self.assertRaises(roman.NotIntegerError, roman.toRoman, '1')

def test_fromRoman(self):
self.assertEqual(roman.fromRoman('N'), 0)
self.assertEqual(roman.fromRoman('I'), 1)
self.assertEqual(roman.fromRoman('MMXIII'), 2013)

Expand Down

0 comments on commit 88bdf83

Please sign in to comment.