From 01e2c72d6ff6ab9d4ffa31ae1678b69770d85c35 Mon Sep 17 00:00:00 2001 From: Emilianouz <135679131+Emilianouz@users.noreply.github.com> Date: Sun, 1 Mar 2026 18:21:08 +0000 Subject: [PATCH 1/2] Refactor common_prefix and count_letter functions with precomputing. --- .../common_prefix/common_prefix.py | 15 +++++++++----- .../count_letters/count_letters.py | 20 ++++++++++++++----- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py b/Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py index f4839e7..a76fc98 100644 --- a/Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py +++ b/Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py @@ -7,12 +7,17 @@ def find_longest_common_prefix(strings: List[str]): In the event that an empty list, a list containing one string, or a list of strings with no common prefixes is passed, the empty string will be returned. """ + if len(strings) < 2: + return "" + # reducing comparison by sorting places strings with similar prefixes next to each other... + strings.sort() + longest = "" - for string_index, string in enumerate(strings): - for other_string in strings[string_index+1:]: - common = find_common_prefix(string, other_string) - if len(common) > len(longest): - longest = common + for i in range(len(strings) - 1): + # compares only adjacent strings after sorting + common = find_common_prefix(strings[i], strings[i + 1]) + if len(common) > len(longest): + longest = common return longest diff --git a/Sprint-2/improve_with_precomputing/count_letters/count_letters.py b/Sprint-2/improve_with_precomputing/count_letters/count_letters.py index 62c3ec0..71ad756 100644 --- a/Sprint-2/improve_with_precomputing/count_letters/count_letters.py +++ b/Sprint-2/improve_with_precomputing/count_letters/count_letters.py @@ -2,13 +2,23 @@ def count_letters(s: str) -> int: """ count_letters returns the number of letters which only occur in upper case in the passed string. """ - only_upper = set() + lower_case = set() + upper_case = set() + + # collects lowercase and uppercase letters for letter in s: - if is_upper_case(letter): - if letter.lower() not in s: - only_upper.add(letter) - return len(only_upper) + if letter.islower(): + lower_case.add(letter) + elif is_upper_case(letter): + upper_case.add(letter) + + # counts uppercase letters not in lowercase + count = 0 + for letter in upper_case: + if letter.lower() not in lower_case: + count += 1 + return count def is_upper_case(letter: str) -> bool: return letter == letter.upper() From 863555af6bc0993fbceead76b39a900db717f954 Mon Sep 17 00:00:00 2001 From: Emiliano Uruena Date: Mon, 2 Mar 2026 20:52:26 +0000 Subject: [PATCH 2/2] Optimize common prefix function using sorted(strings) to prevent the side effect. --- .../common_prefix/common_prefix.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py b/Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py index a76fc98..50b51be 100644 --- a/Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py +++ b/Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py @@ -10,12 +10,12 @@ def find_longest_common_prefix(strings: List[str]): if len(strings) < 2: return "" # reducing comparison by sorting places strings with similar prefixes next to each other... - strings.sort() + sorted_strings = sorted(strings) longest = "" - for i in range(len(strings) - 1): + for i in range(len(sorted_strings) - 1): # compares only adjacent strings after sorting - common = find_common_prefix(strings[i], strings[i + 1]) + common = find_common_prefix(sorted_strings[i], sorted_strings[i + 1]) if len(common) > len(longest): longest = common return longest