1def beginning(word):
2 end_ind = len(word) // 3
3
4 if len(word) % 3 == 2: # add extra letter to 'beginning'
5 end_ind += 1
6 return word[:end_ind]
7
8
9def middle(word):
10 beg_ind = len(word) // 3
11 end_ind = beg_ind + len(word) // 3
12
13 if len(word) % 3 == 1: # extra letter for 'middle'
14 end_ind += 1
15 elif len(word) % 3 == 2: # extra letter for 'beginning', so shift 'middle' right
16 end_ind += 1
17 beg_ind += 1
18
19 return word[beg_ind:end_ind]
20
21
22def end(word):
23 beg_ind = (len(word) // 3) * 2
24
25 # extra letter in 'beginning' or 'middle', so shift 'end' right
26 if len(word) % 3 == 1 or len(word) % 3 == 2:
27 beg_ind += 1
28
29 return word[beg_ind:]
30
31
32def split_sentence(sentence):
33 words = sentence.split()
34 tuples = []
35 for word in words:
36 elem = (beginning(word), middle(word), end(word))
37 tuples.append(elem)
38 return tuples
............
----------------------------------------------------------------------
Ran 12 tests in 0.000s
OK
Георги Кунчев
15.10.2023 11:07Ако просто сложиш логиката за всяка част от думата в съответната функция, махайки двете допълнителни функции, които си дефинирала, ще е далеч по-четимо и по-лесно. Ще имаш малко повече дублиране на код, но за сметка на по-лесен за разбиране и поддръжка код.
|
f | 1 | def beginning(word): | f | 1 | def beginning(word): |
n | 2 | beg_ind = 0 | n | ||
3 | end_ind = len(word) // 3 | 2 | end_ind = len(word) // 3 | ||
4 | 3 | ||||
5 | if len(word) % 3 == 2: # add extra letter to 'beginning' | 4 | if len(word) % 3 == 2: # add extra letter to 'beginning' | ||
6 | end_ind += 1 | 5 | end_ind += 1 | ||
n | 7 | return word[beg_ind:end_ind] | n | 6 | return word[:end_ind] |
8 | 7 | ||||
9 | 8 | ||||
10 | def middle(word): | 9 | def middle(word): | ||
11 | beg_ind = len(word) // 3 | 10 | beg_ind = len(word) // 3 | ||
12 | end_ind = beg_ind + len(word) // 3 | 11 | end_ind = beg_ind + len(word) // 3 | ||
13 | 12 | ||||
14 | if len(word) % 3 == 1: # extra letter for 'middle' | 13 | if len(word) % 3 == 1: # extra letter for 'middle' | ||
15 | end_ind += 1 | 14 | end_ind += 1 | ||
16 | elif len(word) % 3 == 2: # extra letter for 'beginning', so shift 'middle' right | 15 | elif len(word) % 3 == 2: # extra letter for 'beginning', so shift 'middle' right | ||
17 | end_ind += 1 | 16 | end_ind += 1 | ||
18 | beg_ind += 1 | 17 | beg_ind += 1 | ||
19 | 18 | ||||
20 | return word[beg_ind:end_ind] | 19 | return word[beg_ind:end_ind] | ||
21 | 20 | ||||
22 | 21 | ||||
23 | def end(word): | 22 | def end(word): | ||
24 | beg_ind = (len(word) // 3) * 2 | 23 | beg_ind = (len(word) // 3) * 2 | ||
n | 25 | end_ind = len(word) | n | ||
26 | 24 | ||||
27 | # extra letter in 'beginning' or 'middle', so shift 'end' right | 25 | # extra letter in 'beginning' or 'middle', so shift 'end' right | ||
28 | if len(word) % 3 == 1 or len(word) % 3 == 2: | 26 | if len(word) % 3 == 1 or len(word) % 3 == 2: | ||
29 | beg_ind += 1 | 27 | beg_ind += 1 | ||
30 | 28 | ||||
t | 31 | return word[beg_ind:end_ind] | t | 29 | return word[beg_ind:] |
32 | 30 | ||||
33 | 31 | ||||
34 | def split_sentence(sentence): | 32 | def split_sentence(sentence): | ||
35 | words = sentence.split() | 33 | words = sentence.split() | ||
36 | tuples = [] | 34 | tuples = [] | ||
37 | for word in words: | 35 | for word in words: | ||
38 | elem = (beginning(word), middle(word), end(word)) | 36 | elem = (beginning(word), middle(word), end(word)) | ||
39 | tuples.append(elem) | 37 | tuples.append(elem) | ||
40 | return tuples | 38 | return tuples |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
n | 1 | def calculate_lengths(word): | n | 1 | def beginning(word): |
2 | # calculate lengths of individual parts of the word | 2 | beg_ind = 0 | ||
3 | res = [] | 3 | end_ind = len(word) // 3 | ||
4 | match len(word) % 3: | ||||
5 | case 0: # equally distributed letters | ||||
6 | res += [len(word) // 3] * 3 | ||||
7 | case 1: # extra letter is in the middle | ||||
8 | res.append(len(word) // 3) | ||||
9 | res.append(len(word) // 3 + 1) | ||||
10 | res.append(len(word) // 3) | ||||
11 | case 2: # distribute letters on both sides | ||||
12 | res.append(len(word) // 3 + 1) | ||||
13 | res.append(len(word) // 3) | ||||
14 | res.append(len(word) // 3 + 1) | ||||
15 | 4 | ||||
n | 16 | return res | n | 5 | if len(word) % 3 == 2: # add extra letter to 'beginning' |
17 | 6 | end_ind += 1 | |||
18 | 7 | return word[beg_ind:end_ind] | |||
19 | # this function will be used in beginning(), middle() and end() | ||||
20 | # the parameter 'my_type' will be 0 for 'beginning', 1 for 'middle' and 2 for 'end' | ||||
21 | def helper_func(my_type, word): | ||||
22 | lengths = calculate_lengths(word) | ||||
23 | offset = 0 | ||||
24 | for i in range(my_type): | ||||
25 | offset += lengths[i] | ||||
26 | return word[offset:offset + lengths[my_type]] | ||||
27 | |||||
28 | |||||
29 | def beginning(word): | ||||
30 | return helper_func(0, word) | ||||
31 | 8 | ||||
32 | 9 | ||||
33 | def middle(word): | 10 | def middle(word): | ||
n | 34 | return helper_func(1, word) | n | 11 | beg_ind = len(word) // 3 |
12 | end_ind = beg_ind + len(word) // 3 | ||||
13 | |||||
14 | if len(word) % 3 == 1: # extra letter for 'middle' | ||||
15 | end_ind += 1 | ||||
16 | elif len(word) % 3 == 2: # extra letter for 'beginning', so shift 'middle' right | ||||
17 | end_ind += 1 | ||||
18 | beg_ind += 1 | ||||
19 | |||||
20 | return word[beg_ind:end_ind] | ||||
35 | 21 | ||||
36 | 22 | ||||
37 | def end(word): | 23 | def end(word): | ||
n | 38 | return helper_func(2, word) | n | 24 | beg_ind = (len(word) // 3) * 2 |
25 | end_ind = len(word) | ||||
26 | |||||
27 | # extra letter in 'beginning' or 'middle', so shift 'end' right | ||||
28 | if len(word) % 3 == 1 or len(word) % 3 == 2: | ||||
29 | beg_ind += 1 | ||||
30 | |||||
31 | return word[beg_ind:end_ind] | ||||
39 | 32 | ||||
40 | 33 | ||||
41 | def split_sentence(sentence): | 34 | def split_sentence(sentence): | ||
t | 42 | words = sentence.split() # split sentence into words | t | 35 | words = sentence.split() |
43 | tuples = [] | 36 | tuples = [] | ||
44 | for word in words: | 37 | for word in words: | ||
45 | elem = (beginning(word), middle(word), end(word)) | 38 | elem = (beginning(word), middle(word), end(word)) | ||
46 | tuples.append(elem) | 39 | tuples.append(elem) | ||
47 | return tuples | 40 | return tuples |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|