Question

In: Computer Science

(Python) Using these lists: ones = [ "I","II","III","IV","V","VI","VII","VIII","IX" ] tens = [ "X", "XX","XXX", "XL", "L",...

(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.

Solutions

Expert Solution

  • A class named Roman will be defined to convert an int to roman number
  • firstly, define all the lists given in question as tuples.
  • The integer has to be passed as an argument to the contructor.
  • The constructor must call a function named covert() that will convert the integer stored in class to roman numeral
  • convert() will follow this process:
    • set self.r to an empty string
    • if self.i//100 != 0(the hundreds digit of integer), then append the hundreds[integer//100 - 1] to self.r
    • if (self.i%100)//10!=0 (the second digit of integer) , then append tens[(self.i%100)//10 - 1] to self.r
    • if self.i % 10 != 0(the unit digit of integer), then append[self.i%10 - 1] to self.r
  • define a __str__() function that returns self.r

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:


Related Solutions

​(i) product constituents, (ii) branding, (iii) packaging, (iv) product appearance, and (v) product quality
Give one example each , as to how(i) product constituents, (ii) branding, (iii) packaging, (iv) product appearance, and (v) product quality are adopted in global marketing efforts in an effort to meet the demands of any of the many stakeholders such as customer, government, and others. - list in bullet points format - List one point each for the five items listed above.
Q1b.Briefly explain the following properties if flowcharts. i. Input/output ii. Finiteness iii. Definiteness iv. Efficiency v....
Q1b.Briefly explain the following properties if flowcharts. i. Input/output ii. Finiteness iii. Definiteness iv. Efficiency v. Generality 5 Marks Q1c. Briefly outline the purpose five symbols used in flowcharts designing with respect to software design and modelling. 5 Marks
1. The general formula of alkanes is: (i) CnH2n+2 (ii) CnH2n-2 (iii) CnH2n (iv) CnH2n+4 (v)...
1. The general formula of alkanes is: (i) CnH2n+2 (ii) CnH2n-2 (iii) CnH2n (iv) CnH2n+4 (v) Cn+2H2n 2. The name of the following compound is: a. Tetrabenzene b. Chrysene c. Coronene d. Anthracene e. Pyrene 3. To achieve noble gas configuration, hydrogen requires………………..whereas chlorine requires…………………in outermost shell. a. Octet, duet b. Singlet, duet c. Duet, octet d. Octet, singlet e. None of the above 4. The ……………. the number of relatively stable resonance contributors, the …………… the resonance energy. a....
(I)Age (II)Physician Visits in the Past Year (III)Monthly Income (IV)Case Contacts in Past Year (V)Gender 74...
(I)Age (II)Physician Visits in the Past Year (III)Monthly Income (IV)Case Contacts in Past Year (V)Gender 74 8 $2,347 10 M 81 7 2,434 8 M 83 11 1,636 13 F 77 4 1,963 7 M 76 5 2,358 6 F 79 13 1,968 15 F 79 7 2,683 6 M 3. Suppose you are a caseworker for Eldergarden, an agency that provides social services to the elderly. You are assigned to provide some descriptive statistics for the agency's caseload for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT