Skip to content

Commit 7b7e805

Browse files
authored
Merge pull request #1299 from ivan1016017/june11
adding algo
2 parents 6c8454b + f5fe161 commit 7b7e805

3 files changed

Lines changed: 58 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import re
2+
3+
class Solution:
4+
def isPalindrome(self, s: str) -> bool:
5+
6+
# to lowercase
7+
s = s.lower()
8+
9+
# remove non-alphanumerical characters
10+
s = re.sub(pattern='[^a-zA-Z0-9]', repl='', string=s)
11+
12+
len_s = len(s)
13+
14+
for i in range(len_s//2):
15+
if s[i] != s[len_s - 1 -i]:
16+
return False
17+
18+
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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_17\
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)
18+
19+

0 commit comments

Comments
 (0)