wwwRong

dec2roman

Aug 2nd, 2020 (edited)
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.52 KB | None | 0 0
  1. def dec2roman(num):
  2.     if num>3999:
  3.         return 'out of range'
  4.     rnum = {
  5.       '1': ['I','X','C','M'],
  6.       '2': ['II','XX','CC','MM'],
  7.       '3': ['III','XXX','CCC','MMM'],
  8.       '4': ['VI','LX','DC'],
  9.       '5': ['V','L','D'],
  10.       '6': ['IV','XL','CD'],
  11.       '7': ['IIV','XXL','CCD'],
  12.       '8': ['IIIV','XXXL','CCCD'],
  13.       '9': ['XI','CX','MD'],
  14.       '0': ['','','']
  15.     }
  16.     numstr = str(num)[::-1]
  17.     rstr = ''
  18.     for i in range(len(numstr)):
  19.         rstr += rnum[numstr[i:i+1]][i]
  20.     return rstr[::-1]
  21.  
  22. print(dec2roman(2345))
  23. print(dec2roman(456))
Add Comment
Please, Sign In to add comment