Skip to content

Commit 5203cf3

Browse files
authored
Merge pull request #1479 from ivan1016017/december08
adding algo
2 parents 49585e4 + 053402d commit 5203cf3

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def lengthOfLastWord(self, s: str) -> int:
6+
7+
lst_s = s.split()
8+
9+
return len(lst_s[-1])
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def longestCommonPrefix(self, strs: List[str]) -> str:
6+
7+
if not strs:
8+
return ''
9+
10+
min_strs, max_strs = min(strs), max(strs)
11+
count = 0
12+
13+
for i in range(len(min_strs)):
14+
15+
if min_strs[i] == max_strs[i]:
16+
count += 1
17+
else:
18+
break
19+
20+
return min_strs[:count]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_19_length_last_word import Solution
4+
5+
class LengthLastWordTestCase(unittest.TestCase):
6+
7+
def test_length_last_word(self):
8+
solution = Solution()
9+
output = solution.lengthOfLastWord(s="Hello World")
10+
target = 5
11+
self.assertEqual(output, target)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_20_longest_common_prefix import Solution
4+
5+
class LongestCommonPrefixTestCase(unittest.TestCase):
6+
7+
def test_longest_common_prefix(self):
8+
solution = Solution()
9+
output = solution.longestCommonPrefix(strs=["flower","flow","flight"])
10+
target = 'fl'
11+
self.assertEqual(target, output)
12+
13+
def test_longest_no_common_prefix(self):
14+
solution = Solution()
15+
output = solution.longestCommonPrefix(strs=["dog","racecar","car"])
16+
target = ''
17+
self.assertEqual(target, output)
18+
19+
def test_longest_common_prefix_null_list(self):
20+
solution = Solution()
21+
output = solution.longestCommonPrefix(strs=[])
22+
target = ''
23+
self.assertEqual(target, output)

0 commit comments

Comments
 (0)