Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. def roman_to_arabic(roman_numeral):
  2. """
  3. Note: Does not support shorthand notation yet!
  4. Use IIII instead of IV, VIIII instead of IX, etc.
  5. Args:
  6. roman_numeral (str): the Roman numeral to convert
  7.  
  8. Returns:
  9. int: the number, in Arabic number system
  10. """
  11. try:
  12. # guard example - what if a user provides shorthand?
  13. # TODO: Implement shorthand conversions
  14. if any([
  15. 'IV' in roman_numeral,
  16. 'IX' in roman_numeral,
  17. 'XL' in roman_numeral,
  18. 'XC' in roman_numeral,
  19. 'CD' in roman_numeral,
  20. 'CM' in roman_numeral]):
  21. print('Converter does not yet support shorthand! Use IIII instead of IV, VIIII instead of IX, etc. Your input was \'{0}\''.format(roman_numeral))
  22. return
  23.  
  24. roman_numerals = list(roman_numeral.upper())
  25.  
  26. lookup_table = {
  27. 'I': 1,
  28. 'V': 5,
  29. 'X': 10,
  30. 'L': 50,
  31. 'C': 100,
  32. 'D': 500,
  33. 'M': 1000
  34. }
  35.  
  36. roman_values = [lookup_table[numeral] for numeral in roman_numerals]
  37.  
  38. return sum(roman_values)
  39.  
  40. except KeyError:
  41. print('Looks like you have a character other than one of \'MDCLXVI\' in your argument, \'{0}\'!'.format(roman_numeral))
  42.  
  43. except (AttributeError, TypeError) as error:
  44. print('Looks like you need to provide an argument that is a string, like \'MCXVIII\'! Here\'s your error: {0}'.format(error))
  45.  
  46. print(roman_to_arabic('MMXVIII'))
  47. print(roman_to_arabic('MCCIX'))
  48. print(roman_to_arabic('MCAXVI I~I'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement