Skip to content

Commit 5c5e63d

Browse files
committed
feat: optimise code with precomputing
1 parent e718fb4 commit 5c5e63d

2 files changed

Lines changed: 15 additions & 11 deletions

File tree

Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,18 @@ def find_longest_common_prefix(strings: List[str]):
77
88
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.
99
"""
10+
11+
if len(strings) < 2:
12+
return ""
13+
14+
strings = sorted(strings)
15+
1016
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
17+
18+
for i in range(len(strings) - 1):
19+
common = find_common_prefix(strings[i], strings[i + 1])
20+
if len(common) > len(longest):
21+
longest = common
1622
return longest
1723

1824

Sprint-2/improve_with_precomputing/count_letters/count_letters.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ def count_letters(s: str) -> int:
22
"""
33
count_letters returns the number of letters which only occur in upper case in the passed string.
44
"""
5+
letters = set(s)
56
only_upper = set()
7+
68
for letter in s:
7-
if is_upper_case(letter):
8-
if letter.lower() not in s:
9+
if letter.isupper():
10+
if letter.lower() not in letters:
911
only_upper.add(letter)
1012
return len(only_upper)
11-
12-
13-
def is_upper_case(letter: str) -> bool:
14-
return letter == letter.upper()

0 commit comments

Comments
 (0)