-
Notifications
You must be signed in to change notification settings - Fork 3
/
57_Amazon_Breakup_Text_Into_k_size_sentences.py
executable file
·64 lines (51 loc) · 1.92 KB
/
57_Amazon_Breakup_Text_Into_k_size_sentences.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
This problem was asked by Amazon.
Given a string s and an integer k,
break up the string into multiple texts such that each text has a length of k or less.
You must break it up so that words don't break across lines.
If there's no way to break the text up, then return null.
You can assume that there are no spaces at the ends of the
string and that there is exactly one space between each word.
For example, given the string "the quick brown fox jumps over the lazy dog" and k = 10,
you should return: ["the quick", "brown fox", "jumps over", "the lazy", "dog"].
No string in the list has a length of more than 10.
"""
def breakup_words(string:str):
return string.split()
def check_valid_string(s, word, k):
if len(s + word) > k:
return False
return True
def breakup_string(string, k):
words = breakup_words(string)
broken_text = []
current_string = ""
while words:
w = words[0]
if len(current_string) == 0:
# empty sentence "current_string"
if check_valid_string(current_string, w, k):
current_string += w
words = words[1:]
# check if the string we added was the last one
if not words:
broken_text.append(current_string)
else:
return None
else:
# sentence already contains a word
if check_valid_string(current_string+" ", w, k):
current_string += " " + w
words = words[1:]
else:
# sentence length too long
broken_text.append(current_string)
current_string = "" # reset current_string
if len(words)>0:
return None
return broken_text
if __name__ == '__main__':
string = "the quick brown fox jumps over the lazy dog"
k = 10
print(breakup_string(string, k))
print(breakup_string("phantasmagoria", k))