-1

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.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Spuddy
  • 19
  • 4
  • What do you mean by "summation table of roman numbers corresponding to the decimal integers from 1 up to the number inputted."? Like, if the number is 5, you want the sum of 1+2+3+4+5? – Rodrigo Rodrigues Mar 24 '23 at 18:09
  • The answer is to be in the form of a table of separate roman numerals for each number up to that number. e.g. if the number was 7, it would be a table with 7 rows for each individual number up to 7 in two columns one decimal integer and one of the roman numerial. – Spuddy Mar 24 '23 at 18:13

1 Answers1

1

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

ProfDFrancis
  • 8,816
  • 1
  • 17
  • 26
  • 1
    this is the first one of these coding tasks i've done, lecturer never went through them so the coding was a bit clouded for me. but in the question on my paper is said summation table so the lecturer probably mis-worded it. Thank you for the help tho – Spuddy Mar 24 '23 at 18:21