Skip to content

Instantly share code, notes, and snippets.

@wmantly
Created June 26, 2015 04:06
Show Gist options
  • Select an option

  • Save wmantly/f448e9f5acf0852537e3 to your computer and use it in GitHub Desktop.

Select an option

Save wmantly/f448e9f5acf0852537e3 to your computer and use it in GitHub Desktop.
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