Created
September 14, 2010 11:29
-
-
Save vaclavbohac/578892 to your computer and use it in GitHub Desktop.
Scripting languages homework
This file contains hidden or 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
#!/usr/bin/env python | |
def to_roman(num): | |
# is input between 0 and 4000? | |
if not 0 < int(num) < 4000: return False | |
length = len(num) | |
res = '' | |
for s in num: | |
# get dictionary for current word length | |
romans_dict = get_dictionary(length) | |
if not romans_dict: return False | |
# sort dictionary keys | |
romans_keys = romans_dict.keys() | |
romans_keys.sort() | |
numeral = int(s) | |
rom = '' | |
if numeral <= 3: # 0 - 3 | |
rom = romans_dict[1] * numeral | |
elif numeral <= 8: # 4 - 8 | |
if (numeral - 5) < 0: | |
rom = romans_dict[1] * abs(numeral - 5) | |
rom += romans_dict[5] | |
else: | |
rom = romans_dict[5] | |
rom += romans_dict[1] * (numeral - 5) | |
else: # 9 | |
rom = romans_dict[1] + romans_dict[10] | |
res += rom | |
length -= 1 | |
return res | |
def get_dictionary(length): | |
# is input between 1 and 5? | |
if not 0 < length < 5: return False | |
if length == 1: return {1:'I', 5: 'V', 10:'X'} | |
elif length == 2: return {1:'X', 5:'L', 10:'C'} | |
elif length == 3: return {1:'C', 5:'D', 10:'M'} | |
else: return {1:'M',} |
This file contains hidden or 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
#!/usr/bin/env python | |
""" Unit tests for roman converter """ | |
import roman | |
import unittest | |
class ToRomanKnownValues(unittest.TestCase): | |
knownValues = ( (1, 'I'), | |
(2, 'II'), | |
(3, 'III'), | |
(4, 'IV'), | |
(5, 'V'), | |
(6, 'VI'), | |
(7, 'VII'), | |
(8, 'VIII'), | |
(9, 'IX'), | |
(10, 'X'), | |
(11, 'XI'), | |
(12, 'XII'), | |
(13, 'XIII'), | |
(14, 'XIV'), | |
(15, 'XV'), | |
(100, 'C'), | |
(101, 'CI') ) | |
def testKnownValues(self): | |
for integer, numeral in self.knownValues: | |
result = roman.to_roman(str(integer)) | |
self.assertEquals(numeral, result) | |
def testBorders(self): | |
self.assertFalse(roman.to_roman(0)) | |
self.assertFalse(roman.to_roman(4000)) | |
class GetDictionary(unittest.TestCase): | |
def testKnownValues(self): | |
dictionary = roman.get_dictionary(1) | |
self.assertEquals('X', dictionary[10]) | |
dictionary = roman.get_dictionary(2) | |
self.assertEquals('C', dictionary[10]) | |
dictionary = roman.get_dictionary(3) | |
self.assertEquals('M', dictionary[10]) | |
dictionary = roman.get_dictionary(4) | |
self.assertEquals('M', dictionary[1]) | |
def testBorders(self): | |
dictionary = roman.get_dictionary(0) | |
self.assertFalse(dictionary) | |
dictionary = roman.get_dictionary(5) | |
self.assertFalse(dictionary) | |
if __name__ == "__main__": | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment