Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.16 KB | None | 0 0
  1. romanNumerals = {"I": 1,
  2.                  "V": 5,
  3.                  "X": 10,
  4.                  "L": 50,
  5.                  "C": 100,
  6.                  "D": 500,
  7.                  "M": 1000,
  8.                  }
  9.  
  10. def reverse_roman(roman_string):
  11.     solution = 0
  12.     sum = 0
  13.     skipValue = False
  14.     for counter, element in enumerate(roman_string):
  15.         if skipValue:
  16.             skipValue = False
  17.             continue
  18.         if counter+1 < len(roman_string):
  19.             if romanNumerals[roman_string[counter+1]] > romanNumerals[element]:
  20.                 sum = romanNumerals[roman_string[counter+1]] - romanNumerals[element]
  21.                 solution = solution + sum
  22.                 skipValue = True
  23.                 continue
  24.         solution = solution + romanNumerals[element]
  25.     return solution
  26.  
  27. if __name__ == '__main__':
  28.     #These "asserts" using only for self-checking and not necessary for auto-testing
  29.     assert reverse_roman('VI') == 6, '6'
  30.     assert reverse_roman('LXXVI') == 76, '76'
  31.     assert reverse_roman('CDXCIX') == 499, '499'
  32.     assert reverse_roman('MMMDCCCLXXXVIII') == 3888, '3888'
  33.     print('Great! It is time to Check your code!');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement