Skip to content

Commit 10ec3f7

Browse files
authored
Merge pull request #1440 from ivan1016017/october30
adding algo
2 parents 387894c + f26b62e commit 10ec3f7

3 files changed

Lines changed: 62 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
import re
4+
5+
class Solution:
6+
def isPalindrome(self, s: str) -> bool:
7+
8+
# to lowercase
9+
s = s.lower()
10+
11+
# Remove non-alphanumerical characters
12+
s = re.sub(pattern='[^a-zA-Z0-9]', repl='', string=s)
13+
14+
15+
# Identify palindrome
16+
17+
len_s = len(s)
18+
19+
for i in range(len_s//2):
20+
21+
if s[i] != s[len_s - 1 - i]:
22+
return False
23+
24+
return True
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_21\
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)