Advertisement
Korotkodul

roman_number

Sep 6th, 2024
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. def roman_to_int(roman_number: str) -> int:
  2.     d_one = {"I": 1, "V": 5, "X": 10, "L": 50,  "C": 100, "D": 500, "M": 1000}
  3.     #d_two = {"IV": 4, "IX": 9, "XL": 40, "XC": 90, "CD": 400, "CM": 900}
  4.  
  5.     res = 0
  6.     ind = 0
  7.     """while ind < len(roman_number):
  8.        num = roman_number[ind: ind + 2]
  9.        if num in d_two:
  10.            res += d_two[num]
  11.            continue
  12.        res += d_one[roman_number[ind]]
  13.        ind += 1
  14.    return res"""
  15.     for i in range(len(roman_number) - 1):
  16.         if d_one[roman_number[i]] < d_one[roman_number[i+1]]:
  17.             res -= d_one[roman_number[i]]
  18.         else:
  19.             res += d_one[roman_number[i]]
  20.  
  21.     res += d_one[roman_number[-1]]
  22.     return res
  23.  
  24.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement