Skip to content

Commit eb5a674

Browse files
added code for the 2 exercises
1 parent 134e683 commit eb5a674

File tree

3 files changed

+62
-19
lines changed

3 files changed

+62
-19
lines changed
Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
from typing import List
22

3-
43
def find_longest_common_prefix(strings: List[str]):
54
"""
65
find_longest_common_prefix returns the longest string common at the start of any two strings in the passed list.
76
87
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.
98
"""
9+
if len(strings) < 2:
10+
return ""
11+
12+
# Precompute by sorting - longest common prefixes will be between adjacent strings
13+
sorted_strings = sorted(strings)
1014
longest = ""
11-
for string_index, string in enumerate(strings):
12-
for other_string in strings[string_index+1:]:
13-
common = find_common_prefix(string, other_string)
14-
if len(common) > len(longest):
15-
longest = common
15+
16+
# Compare each string with its neighbor to find common prefixes
17+
for i in range(len(sorted_strings) - 1):
18+
common = find_common_prefix(sorted_strings[i], sorted_strings[i + 1])
19+
if len(common) > len(longest):
20+
longest = common
21+
1622
return longest
1723

18-
1924
def find_common_prefix(left: str, right: str) -> str:
2025
min_length = min(len(left), len(right))
2126
for i in range(min_length):
2227
if left[i] != right[i]:
2328
return left[:i]
24-
return left[:min_length]
29+
return left[:min_length]

Sprint-2/improve_with_precomputing/common_prefix/common_prefix_test.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,32 @@ def test_really_long_list(self):
3636
common_prefix = find_longest_common_prefix(strings)
3737
self.assertRegex(common_prefix, "^hello.*$")
3838

39+
def test_multiple_equal_length_prefixes(self):
40+
"""Test when there are multiple prefixes of the same length"""
41+
strings = [
42+
"apple_pie",
43+
"apple_cider",
44+
"banana_split",
45+
"grape_bread",
46+
"grape_pie"
47+
]
48+
# Both "apple_" and "grape_" are length 6, either could be returned
49+
result = find_longest_common_prefix(strings)
50+
self.assertTrue(result in ["apple_", "grape_"])
51+
self.assertEqual(len(result), 6)
52+
53+
def test_identical_strings(self):
54+
"""Test when multiple strings are identical"""
55+
strings = [
56+
"identical",
57+
"different",
58+
"identical",
59+
"identical",
60+
"another"
61+
]
62+
# The longest prefix is between identical strings: "identical"
63+
self.assertEqual(find_longest_common_prefix(strings), "identical")
64+
65+
3966
if __name__ == "__main__":
4067
unittest.main()
Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
def count_letters(s: str) -> int:
2-
"""
2+
"""
33
count_letters returns the number of letters which only occur in upper case in the passed string.
44
"""
5-
only_upper = set()
6-
for letter in s:
7-
if is_upper_case(letter):
8-
if letter.lower() not in s:
9-
only_upper.add(letter)
10-
return len(only_upper)
11-
12-
13-
def is_upper_case(letter: str) -> bool:
14-
return letter == letter.upper()
5+
if not s:
6+
return 0
7+
8+
# Precompute using fixed arrays for maximum performance
9+
has_upper = [False] * 26
10+
has_lower = [False] * 26
11+
12+
for char in s:
13+
code = ord(char)
14+
if 65 <= code <= 90: # A-Z
15+
has_upper[code - 65] = True
16+
elif 97 <= code <= 122: # a-z
17+
has_lower[code - 97] = True
18+
19+
# Count uppercase letters without lowercase counterparts
20+
count = 0
21+
for i in range(26):
22+
if has_upper[i] and not has_lower[i]:
23+
count += 1
24+
25+
return count

0 commit comments

Comments
 (0)