Created
June 26, 2015 04:06
-
-
Save wmantly/f448e9f5acf0852537e3 to your computer and use it in GitHub Desktop.
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
| class roman_int( int ): | |
| def roman( self ): | |
| romanNum = [] | |
| symbols = ( ( 'M', 1000 ), ( 'C', 100 ), ( 'XC', 90 ), ( 'L', 50 ), ( 'X', 10 ), | |
| ( 'IX', 9 ), ('V', 5 ) , ( 'IV', 4 ), ( 'I', 1 ) ) | |
| for symbol, value in symbols: | |
| while self >= value: | |
| self -= value | |
| romanNum.append( symbol ) | |
| return roman_str( ''.join( romanNum ) ) | |
| class roman_str( str ): | |
| def fromRoman( self ): | |
| symbols = { 'M':1000, "D":500, 'C':100, 'L':50, 'X':10, 'V':5, 'I':1 } | |
| num = 0 | |
| last = 0 | |
| for i, value in enumerate( self[:: -1] ): | |
| if last < symbols[ value ]: | |
| num -= symbols[ value ] | |
| else: | |
| num += symbols[ value ] | |
| last = symbols[value] | |
| return roman_int(num) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment