Skip to content

Commit 6bdfd0f

Browse files
authored
Merge pull request #1369 from ivan1016017/august20
adding updates
2 parents a983783 + 19e5e32 commit 6bdfd0f

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def romanToInt(self, s: str) -> int:
6+
7+
roman_to_dict = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}
8+
9+
len_s = len(s)
10+
11+
answer = roman_to_dict[s[len_s - 1]]
12+
13+
for i in range(len_s -1):
14+
15+
if roman_to_dict[s[len_s - 2 - i]] \
16+
< roman_to_dict[s[len_s - 1 - i]]:
17+
answer -= roman_to_dict[s[len_s - 2 - i]]
18+
else:
19+
answer += roman_to_dict[s[len_s - 2 - i]]
20+
21+
return answer
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_19\
3+
.roman_to_integer import Solution
4+
5+
class RomanToIntegerTestCase(unittest.TestCase):
6+
7+
def test_patter_one(self):
8+
solution = Solution()
9+
output = solution.romanToInt(s='VII')
10+
target = 7
11+
self.assertEqual(output, target)
12+
13+
def test_patter_two(self):
14+
solution = Solution()
15+
output = solution.romanToInt(s='IX')
16+
target = 9
17+
self.assertEqual(output, target)

0 commit comments

Comments
 (0)