Skip to content

Commit bf96a40

Browse files
authored
Merge pull request #1123 from ivan1016017/december15
adding roman to integer algo
2 parents 7f25785 + 399d1e9 commit bf96a40

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_12\
3+
.roman_to_integer import Solution
4+
5+
6+
class RomanToIntegerTestCase(unittest.TestCase):
7+
8+
def test_patter_one(self):
9+
solution = Solution()
10+
output = solution.romanToInt(s='VII')
11+
target = 7
12+
self.assertEqual(output, target)
13+
14+
def test_patter_two(self):
15+
solution = Solution()
16+
output = solution.romanToInt(s='IX')
17+
target = 9
18+
self.assertEqual(output, target)

0 commit comments

Comments
 (0)