Advertisement
Guest User

Untitled

a guest
Dec 28th, 2015
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. # This is a program which can be used to convert arabic to roman numerals, and reverse.
  2. # Due to the "bar" symbol on Roman numerals 4,000 and up, I limited this to 3,999.
  3.  
  4. # Roman Numeral Array
  5. RomArray = ['MMM', 'MM', 'M', 'CM', 'DCCC', 'DCC', 'DC', 'D', 'CD', 'CCC', 'CC', 'C',
  6. 'XC', 'LXXX', 'LXX', 'LX', 'L', 'XL', 'XXX', 'XX', 'X', 'IX', 'VIII',
  7. 'VII', 'VI', 'V', 'IV', 'III', 'II', 'I']
  8.  
  9. # Arabic Numeral Array
  10. AraArray = [3000, 2000, 1000, 900, 800, 700, 600, 500, 400, 300, 200, 100, 90, 80, 70, 60,
  11. 50, 40, 30, 20, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
  12.  
  13. MIN_NUM = 1
  14. MAX_NUM = 3999
  15.  
  16. def arabic_to_roman(ar_num):
  17. numeral = ''
  18. for i, v in enumerate(AraArray):
  19. into = ar_num / v
  20. if into == 1:
  21. numeral += RomArray[i]
  22. remainder = ar_num - v
  23. ar_num = remainder
  24. return numeral
  25.  
  26. # Get a four digit Arabic Numeral from a user, convert it to a Roman Numeral
  27. u_arabic = raw_input('Please enter a number between %d and %d: ' % (MIN_NUM, MAX_NUM))
  28. u_arabic = int(u_arabic)
  29. if u_arabic > MAX_NUM:
  30. quit('%d is too big. Please try again.' % u_arabic)
  31. elif u_arabic < MIN_NUM:
  32. quit('%d is too small. Please try again.' % u_arabic)
  33.  
  34.  
  35. print "Your Roman numeral is: " + arabic_to_roman(u_arabic)
  36.  
  37. print '-' * 40
  38.  
  39. # Get a Roman Numeral from a user, convert it to an Arabic Numeral
  40. u_roman = raw_input('Please enter a Roman Numeral whose Arabic Numeral value is between %d and %d: ' % (MIN_NUM, MAX_NUM))
  41.  
  42.  
  43. def roman_to_arabic(ro_num):
  44. numeral = int()
  45. for i, v in enumerate(RomArray):
  46. if ro_num.startswith(v):
  47. numeral += AraArray[i]
  48. ro_num = ro_num[len(v):]
  49. if numeral == 0:
  50. numeral = 'not between %d and %d. Please try again. ' % (MIN_NUM, MAX_NUM)
  51. return numeral
  52.  
  53. print "Your Arabic Numeral is: " + str(roman_to_arabic(u_roman))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement