HEX0x29A

римские числа

Oct 18th, 2020
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. rule_add = {
  2.     'I': 1,
  3.     'V': 5,
  4.     'X': 10,
  5.     'L': 50,
  6.     'C': 100,
  7.     'D': 500,
  8.     'M': 1000,
  9. }
  10.  
  11. rule_div = {
  12.     ('I', 'V'): 3,
  13.     ('I', 'X'): 8,
  14.     ('X', 'L'): 30,
  15.     ('X', 'C'): 80,
  16.     ('C', 'D'): 300,
  17.     ('C', 'M'): 800,
  18. }
  19.  
  20. def roman_to_arabic(roman_number):
  21.     number = 0
  22.     prev_literal = None
  23.     for literal in roman_number:
  24.         if prev_literal and rule_add[prev_literal] < rule_add[literal]:
  25.             number += rule_div[(prev_literal, literal)]
  26.         else:
  27.             number += rule_add[literal]
  28.         prev_literal = literal
  29.     return number
  30.  
  31. print (roman_to_arabic(input('Римская цифра ')))
Add Comment
Please, Sign In to add comment