0% found this document useful (0 votes)
23 views3 pages

New Text Document

yut

Uploaded by

umesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views3 pages

New Text Document

yut

Uploaded by

umesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

def write_roman(num):

roman = OrderedDict()
roman[1000] = "M"
roman[900] = "CM"
roman[500] = "D"
roman[400] = "CD"
roman[100] = "C"
roman[90] = "XC"
roman[50] = "L"
roman[40] = "XL"
roman[10] = "X"
roman[9] = "IX"
roman[5] = "V"
roman[4] = "IV"
roman[1] = "I"

def roman_num(num):
for r in roman.keys():
x, y = divmod(num, r)
yield roman[r] * x
num -= (r * x)
if num <= 0:
break

return "".join([a for a in roman_num(num)])


###################################################################################
################
num_map = [(1000, 'M'), (900, 'CM'), (500, 'D'), (400, 'CD'), (100, 'C'), (90,
'XC'),
(50, 'L'), (40, 'XL'), (10, 'X'), (9, 'IX'), (5, 'V'), (4, 'IV'), (1,
'I')]

def num2roman(num):

roman = ''

while num > 0:


for i, r in num_map:
while num >= i:
roman += r
num -= i

return roman

# test
>>> num2roman(2242)
'MMCCXLII'
###################################################################################
######################
ROMAN = [
(1000, "M"),
( 900, "CM"),
( 500, "D"),
( 400, "CD"),
( 100, "C"),
( 90, "XC"),
( 50, "L"),
( 40, "XL"),
( 10, "X"),
( 9, "IX"),
( 5, "V"),
( 4, "IV"),
( 1, "I"),
]

def int_to_roman(number):
result = ""
for (arabic, roman) in ROMAN:
(factor, number) = divmod(number, arabic)
result += roman * factor
return result
########################################################################
def int_to_roman(num):
_values = [
1000000, 900000, 500000, 400000, 100000, 90000, 50000, 40000, 10000, 9000,
5000, 4000, 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]

_strings = [
'M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', "M",
"CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]

result = ""
decimal = num

while decimal > 0:


for i in range(len(_values)):
if decimal >= _values[i]:
if _values[i] > 1000:
result += u'\u0304'.join(list(_strings[i])) + u'\u0304'
else:
result += _strings[i]
decimal -= _values[i]
break
return result
##################################################################################
roman_map = [(1000, 'M'), (900, 'CM'), (500, 'D'), (400, 'CD'), (100, 'C'), (90,
'XC'),
(50, 'L'), (40, 'XL'), (10, 'X'), (9, 'IX'), (5, 'V'), (4, 'IV'), (1, 'I')]

def IntToRoman (xn):


x = xn
y = 0
Str = ""
for i, r in roman_map:
# take the number and divisible by the roman number from 1000 to 1.
y = x//i

for j in range(0, y):


# If after divisibility is not 0 then take the roman number from list
into String.
Str = Str+r

# Take the remainder to next round.


x = x%i
print(Str)
return Str
###################################################################################
####
input = int(raw_input()) # enter your integerval from keyboard
Decimals =[1,4,5,9,10,40,50,90,100,400,500,900,1000]
Romans =['I','IV','V','IX','X','XL','L','XC','C','CD','D','CM','M']
romanletters= [] # empty array to fill each corresponding roman letter from the
Romans array
while input != 0:
# we use basic formula of converstion, therefore input is substracted from
# least maximum similar number from Decimals array until input reaches zero
for val in range (len(Decimals)):
if input >= Decimals[val]:
decimal = Decimals[val]
roman = Romans[val]
difference = input - decimal
romanletters.append(roman)
input = difference
dec_to_roman = ''.join(romanletters) # concatinate the values
print dec_to_roman
#########################################################################
def int2roman(number):
numerals={1:"I", 4:"IV", 5:"V", 9: "IX", 10:"X", 40:"XL", 50:"L",
90:"XC", 100:"C", 400:"CD", 500:"D", 900:"CM", 1000:"M"}
result=""
for value, numeral in sorted(numerals.items(), reverse=True):
while number >= value:
result += numeral
number -= value
return result
print int2roman(input("Enter a number (1 to 4999) in decimal form: "))
###########################################################################

You might also like