Skip to content

Commit 282b641

Browse files
committed
Sprint 2: Improve code with precomputing - clean PR
1 parent aad8ad2 commit 282b641

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

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+
if len(strings) < 2:
11+
return ""
12+
13+
# Precompute: Sort so similar strings are near each other
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: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,33 @@
1+
from typing import Set
2+
3+
14
def count_letters(s: str) -> int:
25
"""
36
count_letters returns the number of letters which only occur in upper case in the passed string.
47
"""
5-
only_upper = set()
8+
# only_upper = set()
9+
# for letter in s:
10+
# if is_upper_case(letter):
11+
# if letter.lower() not in s:
12+
# only_upper.add(letter)
13+
# return len(only_upper)
14+
uppercase_letters : Set[str] = set()
15+
lowercase_letters : Set[str] = set()
16+
17+
# Precompute uppercase and lowercase letters
618
for letter in s:
719
if is_upper_case(letter):
8-
if letter.lower() not in s:
9-
only_upper.add(letter)
10-
return len(only_upper)
20+
uppercase_letters.add(letter)
21+
elif letter.isalpha():
22+
lowercase_letters.add(letter)
23+
24+
# Count uppercase letters that don't appear in lowercase
25+
count = 0
26+
for letter in uppercase_letters:
27+
if letter.lower() not in lowercase_letters:
28+
count +=1
29+
30+
return count
1131

1232

1333
def is_upper_case(letter: str) -> bool:

0 commit comments

Comments
 (0)