Created
April 4, 2016 03:06
-
-
Save iamsk/85631c5da02373f9e096b59a170bbf2c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
I = 'I' | |
V = 'V' | |
X = 'X' | |
L = 'L' | |
C = 'C' | |
M = 'M' | |
def convert(number): | |
if number <= 0: | |
raise Exception() | |
if number < 4: | |
return I * number | |
if number == 4: | |
return I + V | |
if 5 <= number <= 8: | |
return V + I * (number - 5) | |
if number == 9: | |
return I + X | |
if number >= 1000: | |
remainder = number % 1000 | |
times = number / 1000 | |
try: | |
_ = convert(remainder) | |
except: | |
_ = '' | |
return 'M' * times + _ | |
elif number >= 900: | |
try: | |
_ = convert(number - 900) | |
except: | |
_ = '' | |
return 'CM' + _ | |
elif number >= 500: | |
try: | |
_ = convert(number - 500) | |
except: | |
_ = '' | |
return 'D' + _ | |
if number >= 100: | |
remainder = number % 100 | |
times = number / 100 | |
try: | |
_ = convert(remainder) | |
except: | |
_ = '' | |
return 'C' * times + _ | |
elif number >= 90: | |
try: | |
_ = convert(number - 90) | |
except: | |
_ = '' | |
return 'XC' + _ | |
elif number >= 50: | |
try: | |
_ = convert(number - 50) | |
except: | |
_ = '' | |
return 'L' + _ | |
elif number >= 10: | |
remainder = number % 10 | |
times = number / 10 | |
try: | |
_ = convert(remainder) | |
except: | |
_ = '' | |
return 'X' * times + _ | |
class RomaTest(unittest.TestCase): | |
def test_0_raise_error(self): | |
with self.assertRaises(Exception): | |
convert(0) | |
def test_1_return_I(self): | |
roma = convert(1) | |
self.assertEqual(roma, I) | |
def test_2_return_II(self): | |
roma = convert(2) | |
self.assertEqual(roma, I + I) | |
def test_3_return_III(self): | |
roma = convert(3) | |
self.assertEqual(roma, I + I + I) | |
def test_4_return_IV(self): | |
roma = convert(4) | |
self.assertEqual(roma, I + V) | |
def test_5_return_V(self): | |
roma = convert(5) | |
self.assertEqual(roma, V) | |
def test_6_return_VI(self): | |
roma = convert(6) | |
self.assertEqual(roma, 'VI') | |
def test_9_return_IX(self): | |
roma = convert(9) | |
self.assertEqual(roma, I + X) | |
def test_10_return_C(self): | |
roma = convert(10) | |
self.assertEqual(roma, X) | |
def test_18_return_C(self): | |
roma = convert(18) | |
self.assertEqual(roma, 'XVIII') | |
def test_19_return_XIX(self): | |
roma = convert(19) | |
self.assertEqual(roma, 'XIX') | |
def test_20_return_XX(self): | |
roma = convert(20) | |
self.assertEqual(roma, 'XX') | |
def test_50_return_L(self): | |
roma = convert(50) | |
self.assertEqual(roma, L) | |
def test_66_return_LXVI(self): | |
roma = convert(66) | |
self.assertEqual(roma, 'LXVI') | |
def test_88_return_LXXXVIII(self): | |
roma = convert(88) | |
self.assertEqual(roma, 'LXXXVIII') | |
def test_90_return_XC(self): | |
roma = convert(90) | |
self.assertEqual(roma, 'XC') | |
def test_99_return_XCIX(self): | |
roma = convert(99) | |
self.assertEqual(roma, 'XCIX') | |
def test_101_return_CI(self): | |
roma = convert(101) | |
self.assertEqual(roma, 'CI') | |
def test_500_return_D(self): | |
roma = convert(500) | |
self.assertEqual(roma, 'D') | |
def test_990_return_CMXC(self): | |
roma = convert(990) | |
self.assertEqual(roma, 'CMXC') | |
def test_minus_1_raise_error(self): | |
with self.assertRaises(Exception): | |
convert(-1) | |
def test_1000_return_M(self): | |
roma = convert(1000) | |
self.assertEqual(roma, M) | |
def test_1001_return_M(self): | |
roma = convert(1001) | |
self.assertEqual(roma, 'MI') | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment