Codewars Python – Integer to Roman conversion

Codewars Python in its 6kyu Kata has given us a problem to convert an integer into the Roman numeral Symbols. In Ancient Roman times, people use Roman numbers instead of integers.

Codewars python- Roman NumbersCodewars python- Integers

Roman numbers

We can use the Roman symbol for any number.  For e.g instead of 1, we can use I instead of 2 we can use II and instead of 3, we can use III. Remember that we can not use more than 3 identical symbols in a row to show a number. For e.g, we cannot use IIII to represent 4. The Symbols associated with value are: I for 1, V for 5, X for  10,  L for 50,  C for 100, D for 500, and M for 1000. With all these Symbols we can write any number up to 3999.

Codewars Python Solution

The Starter Code that Codewars gave us in Python is

def solution(n):
    # TODO convert int to roman string

    return

To know more about the Problem Discussion click Here

How will you approach this Kata, Comment?

My approach to the Problem:

 

Firstly, I made two Lists in Python, first to store the Numbers which have Single Roman Symbol Available and Second List for Roman Symbols.

def solution(n):
    # TODO convert int to roman string
    x = [1000,500,100,50,10,5,1] 
    symbol = ["M","D","C","L","X","V","I"]
    val = 0  // indexing the lists
    return_String = "" // return Roman String

Secondly, I started a loop until the Integer given is reduced to 0 the loop goes on.

        while n != 0:
               n   = n % x[val]

My approach to finding the Roman Number was to divide the number by 1000-500-100-50-10-5-1 one by one to get if the number is greater than a specific symbol. For e.g, if we have to find the Roman Symbol for 125 Dividing it by 1000-500 will not do anything but when divide it by 100 we get 125/100 = 1, where I print the 100 Symbol one time which is C after taking modulus we are left with 25, dividing it by 10, we will get 2 so printing  10 Roman Symbol  two times now over String will be like CXX, after taking modulus again left with 5 which has a Symbol V.   So the returned String = CXXV

def solution(n):
    # TODO convert int to roman string
    x = [1000,500,100,50,10,5,1]
    symbol = ["M","D","C","L","X","V","I"]
    val = 0
    return_String = ""
    while n != 0:
        z = int(n/x[val])    
     for i in range(0,z):       
        return_String = return_String + symbol[val]
     n = n % x[val]
     val = val+1
    return return_String

The approach was good but failed on numbers like 4 9, this approach was returning IIII and VIIII respectively. which was wrong. To deal with it I added an if statement that if dividing a number returns 4, it should return the Current symbol concatenated to the previous symbol. For e.g dividing the 4/1 = 4. It should return the current symbol which is I and the Previous symbol which is  V.  Concatenating will return = IV string.

Overall Code

def solution(n):
    # TODO convert int to roman string
    x = [1000,500,100,50,10,5,1]
    symbol = ["M","D","C","L","X","V","I"]
    val = 0
    return_String = ""
    while n != 0:
        z = int(n/x[val])
        if z==4:
            if(symbol[val - 1] == "X" or symbol[val - 1] == "I" or symbol[val - 1] == "C" or symbol[val - 1] == "M" or return_String == "" ):
                return_String = return_String + symbol[val] + symbol[val-1]
            elif(return_String != ""):
                if(return_String[-1] == symbol[val-1]):
                    new = return_String[0:-1] + symbol[val] + symbol[val -2]
                    return_String = new
                else:
                   return_String = return_String + symbol[val] + symbol[val-1] 
                    
        else:
            for i in range(0,z):
                return_String = return_String + symbol[val]
        n = n % x[val]
        val = val+1
    return return_String

When I run it on Codewars it passed all the tests.

 

READ MORE

Python-related posts Visit HERE

C++ related posts Visit HERE

Databases related posts Visit HERE

Data Structures related posts visit HERE

Algorithms related posts visit HERE

Data Science related posts visit HERE