From 053402d25b21f746f01a9196f3826a2a1a9c7791 Mon Sep 17 00:00:00 2001 From: ivan Date: Mon, 8 Dec 2025 04:04:06 -0600 Subject: [PATCH] adding algo --- .../ex_19_length_last_word.py | 9 ++++++++ .../ex_20_longest_common_prefix.py | 20 ++++++++++++++++ .../test_19_length_last_word_round_22.py | 11 +++++++++ .../test_20_longest_common_prefix_round_22.py | 23 +++++++++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_19_length_last_word.py create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_20_longest_common_prefix.py create mode 100644 tests/test_150_questions_round_22/test_19_length_last_word_round_22.py create mode 100644 tests/test_150_questions_round_22/test_20_longest_common_prefix_round_22.py diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_19_length_last_word.py b/src/my_project/interviews/top_150_questions_round_22/ex_19_length_last_word.py new file mode 100644 index 00000000..12a2640a --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_19_length_last_word.py @@ -0,0 +1,9 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def lengthOfLastWord(self, s: str) -> int: + + lst_s = s.split() + + return len(lst_s[-1]) \ No newline at end of file diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_20_longest_common_prefix.py b/src/my_project/interviews/top_150_questions_round_22/ex_20_longest_common_prefix.py new file mode 100644 index 00000000..050923dc --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_20_longest_common_prefix.py @@ -0,0 +1,20 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def longestCommonPrefix(self, strs: List[str]) -> str: + + if not strs: + return '' + + min_strs, max_strs = min(strs), max(strs) + count = 0 + + for i in range(len(min_strs)): + + if min_strs[i] == max_strs[i]: + count += 1 + else: + break + + return min_strs[:count] \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_19_length_last_word_round_22.py b/tests/test_150_questions_round_22/test_19_length_last_word_round_22.py new file mode 100644 index 00000000..e1c3db98 --- /dev/null +++ b/tests/test_150_questions_round_22/test_19_length_last_word_round_22.py @@ -0,0 +1,11 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_19_length_last_word import Solution + +class LengthLastWordTestCase(unittest.TestCase): + + def test_length_last_word(self): + solution = Solution() + output = solution.lengthOfLastWord(s="Hello World") + target = 5 + self.assertEqual(output, target) \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_20_longest_common_prefix_round_22.py b/tests/test_150_questions_round_22/test_20_longest_common_prefix_round_22.py new file mode 100644 index 00000000..673ae894 --- /dev/null +++ b/tests/test_150_questions_round_22/test_20_longest_common_prefix_round_22.py @@ -0,0 +1,23 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_20_longest_common_prefix import Solution + +class LongestCommonPrefixTestCase(unittest.TestCase): + + def test_longest_common_prefix(self): + solution = Solution() + output = solution.longestCommonPrefix(strs=["flower","flow","flight"]) + target = 'fl' + self.assertEqual(target, output) + + def test_longest_no_common_prefix(self): + solution = Solution() + output = solution.longestCommonPrefix(strs=["dog","racecar","car"]) + target = '' + self.assertEqual(target, output) + + def test_longest_common_prefix_null_list(self): + solution = Solution() + output = solution.longestCommonPrefix(strs=[]) + target = '' + self.assertEqual(target, output) \ No newline at end of file