pihta24

Римские примеры

Feb 2nd, 2021
848
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.79 KB | None | 0 0
  1. CONV_TABLE = ((1000, 'M'), (900, 'CM'), (500, 'D'), (400, 'CD'), (100, 'C'), (90, 'XC'), (50, 'L'),
  2.               (40, 'XL'), (10, 'X'), (9, 'IX'), (5, 'V'), (4, 'IV'), (1, 'I'))
  3.  
  4.  
  5. def arab_to_roman(number):
  6.     if number <= 0:
  7.         return ''
  8.     ret = ''
  9.     for arab, roman in CONV_TABLE:
  10.         while number >= arab:
  11.             ret += roman
  12.             number -= arab
  13.  
  14.     return ret
  15.  
  16.  
  17. def roman_to_arab(txt):
  18.     txt = txt.upper()
  19.     ret = 0
  20.     for arab, roman in CONV_TABLE:
  21.         while txt.startswith(roman):
  22.             ret += arab
  23.             txt = txt[len(roman):]
  24.     return ret
  25.  
  26.  
  27. def roman():
  28.     global one
  29.     global two
  30.     global three
  31.     three = one + two
  32.     print('{} + {} = {}'.format(arab_to_roman(one), arab_to_roman(two), arab_to_roman(one + two)))
Advertisement
Add Comment
Please, Sign In to add comment