In: Computer Science
(Python)
Using these lists:
ones = [ "I","II","III","IV","V","VI","VII","VIII","IX" ]
tens = [ "X", "XX","XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"
]
hundreds = [ "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC",
"CM","M" ]
Convert these list to named tuples and store them in a class. Then write a member function(s) for the class to convert a 'int' to a 'roman numeral'. in the range of 1 to 1000. Use comprehensions, map, reduce or join where appropriate.
program:
class Roman:
def __init__(self,i):
self.ones = tuple([ "I","II","III","IV","V","VI","VII","VIII","IX"
])
self.tens = tuple([ "X", "XX","XXX", "XL", "L", "LX", "LXX",
"LXXX", "XC" ])
self.hundreds = tuple([ "C", "CC", "CCC", "CD", "D", "DC", "DCC",
"DCCC", "CM","M" ])
self.i = i
self.convert()
def convert(self):
self.r = ""
if(self.i//100!=0):
self.r = self.r+ self.hundreds[self.i//100 - 1]
if((self.i%100)//10!=0):
self.r = self.r + self.tens[(self.i%100)//10 - 1]
if((self.i%10)!=0):
self.r = self.r + self.ones[self.i%10 - 1]
def __str__(self):
return self.r
i = int(input("Enter a integer between 1 and 1000: "))
R1 = Roman(i)
print(f"{i} in roman numerals is {R1}")
sample input and output: