From 68984ec3a7741fdbbac27f9ff588c434804036c2 Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Sat, 28 Feb 2026 20:38:58 +0000 Subject: [PATCH 1/8] Return empty string when fewer than two strings are provided --- .../common_prefix/common_prefix.py | 7 +++---- 1 file changed, 3 insertions(+), 4 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..738561b 100644 --- a/Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py +++ b/Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py @@ -2,11 +2,10 @@ def find_longest_common_prefix(strings: List[str]): - """ - find_longest_common_prefix returns the longest string common at the start of any two strings in the passed list. - 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 "" + longest = "" for string_index, string in enumerate(strings): for other_string in strings[string_index+1:]: From 02781564d9d4a614353c5faf7fd943d780ff2650 Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Sat, 28 Feb 2026 20:41:58 +0000 Subject: [PATCH 2/8] if the list of strings has at least 2 items, first thing, sort the list alphabetically --- .../improve_with_precomputing/common_prefix/common_prefix.py | 2 ++ 1 file changed, 2 insertions(+) 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 738561b..54ad5f9 100644 --- a/Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py +++ b/Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py @@ -6,6 +6,8 @@ def find_longest_common_prefix(strings: List[str]): if len(strings) < 2: return "" + strings = sorted(strings) + longest = "" for string_index, string in enumerate(strings): for other_string in strings[string_index+1:]: From 237d8bcef32b73f3f5b393e2b1159f04e2fa162c Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Sat, 28 Feb 2026 20:48:24 +0000 Subject: [PATCH 3/8] update the for loop inside find_longest_common_prefix function so that each string in the list is compared with the one comes after it. This is a lot more efficient --- .../common_prefix/common_prefix.py | 7 +++---- 1 file changed, 3 insertions(+), 4 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 54ad5f9..f95d81c 100644 --- a/Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py +++ b/Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py @@ -9,10 +9,9 @@ def find_longest_common_prefix(strings: List[str]): strings = sorted(strings) 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): + for i in range(len(strings) - 1): + common = find_common_prefix(strings[i], strings[i + 1]) + if len(common) > len(longest): longest = common return longest From 5187863c09255e95111a06c1c3643c772a03d861 Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Sat, 28 Feb 2026 21:20:03 +0000 Subject: [PATCH 4/8] collect all lowercase letters present in s string into a set beforehand --- .../count_letters/count_letters.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) 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..0ad9e6c 100644 --- a/Sprint-2/improve_with_precomputing/count_letters/count_letters.py +++ b/Sprint-2/improve_with_precomputing/count_letters/count_letters.py @@ -1,7 +1,11 @@ def count_letters(s: str) -> int: - """ - count_letters returns the number of letters which only occur in upper case in the passed string. - """ + only_lower = set() + for letter in s: + if letter.islower(): + only_lower.add(letter) + + + only_upper = set() for letter in s: if is_upper_case(letter): From dc47abbe47ca5f1056b8e45003c004057dcbc62a Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Sat, 28 Feb 2026 21:23:08 +0000 Subject: [PATCH 5/8] update the 2nd loop to check agaisnt the pre-calcualted lowercase letters set instead of s string --- .../improve_with_precomputing/count_letters/count_letters.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) 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 0ad9e6c..5992448 100644 --- a/Sprint-2/improve_with_precomputing/count_letters/count_letters.py +++ b/Sprint-2/improve_with_precomputing/count_letters/count_letters.py @@ -4,12 +4,10 @@ def count_letters(s: str) -> int: if letter.islower(): only_lower.add(letter) - - only_upper = set() for letter in s: if is_upper_case(letter): - if letter.lower() not in s: + if letter.lower() not in only_lower: only_upper.add(letter) return len(only_upper) From fcbbe6bf8386c5e1f46f6c7b49b000ecd53b6318 Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Tue, 3 Mar 2026 22:54:28 +0000 Subject: [PATCH 6/8] Correctly indent line 15 --- .../improve_with_precomputing/common_prefix/common_prefix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 f95d81c..74de576 100644 --- a/Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py +++ b/Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py @@ -12,7 +12,7 @@ def find_longest_common_prefix(strings: List[str]): for i in range(len(strings) - 1): common = find_common_prefix(strings[i], strings[i + 1]) if len(common) > len(longest): - longest = common + longest = common return longest From 0b395e3be59b2e3208d1745a686002bbb75c1913 Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Tue, 3 Mar 2026 23:08:58 +0000 Subject: [PATCH 7/8] Use "Big O Notation" to explain why the new implementation is more efficient than the original --- .../common_prefix/common_prefix.py | 11 +++++++++++ 1 file changed, 11 insertions(+) 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 74de576..90f3c52 100644 --- a/Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py +++ b/Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py @@ -22,3 +22,14 @@ def find_common_prefix(left: str, right: str) -> str: if left[i] != right[i]: return left[:i] return left[:min_length] + + +""" +The new implementation sorts a string first, then compares each string +with the one that comes after it. On the other hand, the original imple- +mentation compares each string with every single while looping through +nested for loop. +As a result, the compelxity time drops from the original O(n^2 * m) to +around O(n log n * m). + +""" \ No newline at end of file From a7cdf3b0e80c17a66676142fd6812eb90436340e Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Tue, 3 Mar 2026 23:18:19 +0000 Subject: [PATCH 8/8] Use "Big O Notation" to explain why the new implmentation is more efficient than the previous one --- .../count_letters/count_letters.py | 11 +++++++++++ 1 file changed, 11 insertions(+) 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 5992448..1c69df2 100644 --- a/Sprint-2/improve_with_precomputing/count_letters/count_letters.py +++ b/Sprint-2/improve_with_precomputing/count_letters/count_letters.py @@ -14,3 +14,14 @@ def count_letters(s: str) -> int: def is_upper_case(letter: str) -> bool: return letter == letter.upper() + + + +""" +The old implementation scans through the entire string for every uppercase letter +we check against, therefore complexity in the worst case is O(n^2). However, in the +new implementation the lowercase letters a store beforehand in a set. This allows +checking against every uppercase letter without scanning through the entire string. +As a result, the complexixty time goes down to O(n) with the new implementation. + +"""