Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
397 views
in Technique[技术] by (71.8m points)

python - Converting the Numbers into equivalent Roman Numerals

I have tried the Number Conversion into its equivalent Roman numerals. Please suggest me to reduce the implementation of the code for getting output. And also please suggest the same scenario in the Erlang (mentioned in the tags) it will be more helpful to understand the Erlang.

Tried Code:

***Roman = {1000 :"M",900 : "CM",500 :"D",400 : "CD",100 : "C",90 : "XC",50 : "L",40 : "XL",10 : "X",9 : "IX",5 : "V",4:"IV",1:"I"}
    def sub(Num,Num2):
      Num1 = Num - Num2
      N = Num - Num1
      return N
    def roman_convert(Num,roman) :
      #print(roman)
      while( Num > 0):
        if Num >= 1000:
           N = sub(Num,1000)
           roman.append(Roman.get(N))
           Num = Num - 1000
        elif Num >= 900:
           N = sub(Num,900)
           roman.append(Roman.get(N))
           Num = Num - 900
        elif Num >= 500:
            N = sub(Num,500)
            roman.append(Roman.get(N))
            Num = Num - 500
        elif Num >= 400:
           N = sub(Num,400)
           roman.append(Roman.get(N))
           Num = Num - 400
        elif Num >= 100:
           N = sub(Num,100)
           roman.append(Roman.get(N))
           Num = Num - 100
        elif Num >= 90:
           N = sub(Num,90)
           roman.append(Roman.get(N))
           Num = Num - 90
        elif Num >= 50:
           N = sub(Num,50)
           roman.append(Roman.get(N))
           Num = Num - 50 
        elif Num >= 40:
           N = sub(Num,40)
           roman.append(Roman.get(N))
           Num = Num - 40   
        elif Num >= 10:
           N = sub(Num,10)
           roman.append(Roman.get(N))
           Num = Num - 10        
        elif Num >= 9:
           N = sub(Num,9)
           roman.append(Roman.get(N))
           Num = Num - 9
        elif Num >= 4:
           N = sub(Num,4)
           roman.append(Roman.get(N))
           Num = Num - 4
        elif Num >= 1:
           N = sub(Num,1)
           roman.append(Roman.get(N))
           Num = Num - 1
        else :
           roman.append(0) 
      return roman
    if __name__ == "__main__":
      print("Converting the Numbers into Roman:")
      Num = int(input("Enter the number :"))
      roman =[]
      R = roman_convert(Num,roman)
      Roman = "".join(str(x) for x in R)
      print(Roman)***

Please suggest me how to improvise the code ?

Output of the above code is below

**Converting the Numbers into Roman:
Enter the number :199
CXCIX**

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is a very rigorous question but here is one of the solutions that I personally follow and is easy as well. The answer mentioned here and the answer that I follow are similar but the following is shorter. But I believe the logic is the same.

def printRoman(number):
    num = [1, 4, 5, 9, 10, 40, 50, 90, 
           100, 400, 500, 900, 1000]
    sym = ["I", "IV", "V", "IX", "X", "XL", 
           "L", "XC", "C", "CD", "D", "CM", "M"]
    i = 12
    while number:
        div = number // num[i]
        number %= num[i]
 
        while div:
            print(sym[i], end = "")
            div -= 1
        i -= 1

n = int(input('Enter a number: '))
printRoman(n)

I had learnt about it from here you could refer there as well for detailed explanation on how this works.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...