Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I'm trying to write a function sum_Roman that takes integers and outputs a summation table of Roman numbers

The question itself is two parts, first part is to make a function of int_to_roman(num) that converts integers into their roman numerical counterparts, just displaying them. The second part is to use that code and make a function called sum_roman that takes a decimal integer as an input and returns the summation table of roman numbers corresponding to the decimal integers from 1 up to the number inputted.

I did a code for the first part in the form of:

class py_solution:


    def int_to_Roman(self, num):
        val = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
        syb = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
        roman_num = ''
        a = 0
        while  num > 0:
            for _ in range(num // val[a]):
                roman_num += syb[a]
                num -= val[a]
            a += 1
       return roman_num

This code worked for the first part giving me the roman numerials but I haven’t been able to find anything that works for part 2, as I don’t know the code to calculate all roman numerial up to that point or to make them into a table.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Don’t you just need a for loop?

def int_to_Roman( num):
    val = [1000, 900, 500, 400,
        100, 90, 50, 40,
        10, 9, 5, 4,
        1]
    syb = ["M", "CM", "D", "CD",
        "C", "XC", "L", "XL",
        "X", "IX", "V", "IV",
        "I"]
    roman_num = ''
    a = 0
    while  num > 0:
        for _ in range(num // val[a]):
            roman_num += syb[a]
            num -= val[a]
        a += 1
    return roman_num

    
def sum_roman(final):

    for i in range(final):
        decimal = 1+i # Start at 1 (not 0) and end at final (not final-1)
        roman = int_to_Roman(decimal)
        print(f"{decimal:6.0f}   {roman}")

sum_roman(10)

This gives:

     1   I
     2   II
     3   III
     4   IV
     5   V
     6   VI
     7   VII
     8   VIII
     9   IX
    10   X

I don’t know why you call this a "summation" table

Perhaps you mean a "summary" table?

I have not done any summation for you as I think you asked for that in error. But you are free to do summing, instead of summarising, if you wish.

Thank you for giving the exact wording of the task set for you. It does not say "summation".

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading