1def beginning(word):
2 if len(word) % 3 in (0, 1):
3 last = len(word) // 3
4 elif len(word) % 3 == 2:
5 last = len(word) // 3 + 1
6 return word[:last]
7
8def middle(word):
9 if len(word) % 3 == 0:
10 first = len(word) // 3
11 last = 2 * (len(word) // 3)
12 elif len(word) % 3 == 2:
13 first = len(word) // 3 + 1
14 last = 2 * (len(word) // 3 ) + 1
15 elif len(word) % 3 == 1:
16 first = len(word) // 3
17 last = 2 * (len(word) // 3) + 1
18 return word[first:last]
19
20def end(word):
21 if len(word) % 3 == 0:
22 first = 2 * (len(word) // 3 )
23 else:
24 first = 2 * (len(word) // 3 ) + 1
25 return word[first:]
26
27def split_sentence(sentence):
28 return [(beginning(words), middle(words), end(words)) for words in sentence.split()]
29
............
----------------------------------------------------------------------
Ran 12 tests in 0.000s
OK
Георги Кунчев
16.10.2023 21:11Можеш да качваш колкото пожелаеш. Това е идеята. Ние коментираме - ти се учиш :slightly_smiling_face:
|
Деян Диков
16.10.2023 19:48Мога ли да кача и по-редактираното решение? Сложил съм и string slicing и във 3-те функции.
|
f | 1 | def beginning(word): | f | 1 | def beginning(word): |
t | 2 | if len(word) % 3 == 0 or len(word) % 3 == 1: | t | 2 | if len(word) % 3 in (0, 1): |
3 | last = len(word) // 3 | 3 | last = len(word) // 3 | ||
4 | elif len(word) % 3 == 2: | 4 | elif len(word) % 3 == 2: | ||
5 | last = len(word) // 3 + 1 | 5 | last = len(word) // 3 + 1 | ||
6 | return word[:last] | 6 | return word[:last] | ||
7 | 7 | ||||
8 | def middle(word): | 8 | def middle(word): | ||
9 | if len(word) % 3 == 0: | 9 | if len(word) % 3 == 0: | ||
10 | first = len(word) // 3 | 10 | first = len(word) // 3 | ||
11 | last = 2 * (len(word) // 3) | 11 | last = 2 * (len(word) // 3) | ||
12 | elif len(word) % 3 == 2: | 12 | elif len(word) % 3 == 2: | ||
13 | first = len(word) // 3 + 1 | 13 | first = len(word) // 3 + 1 | ||
14 | last = 2 * (len(word) // 3 ) + 1 | 14 | last = 2 * (len(word) // 3 ) + 1 | ||
15 | elif len(word) % 3 == 1: | 15 | elif len(word) % 3 == 1: | ||
16 | first = len(word) // 3 | 16 | first = len(word) // 3 | ||
17 | last = 2 * (len(word) // 3) + 1 | 17 | last = 2 * (len(word) // 3) + 1 | ||
18 | return word[first:last] | 18 | return word[first:last] | ||
19 | 19 | ||||
20 | def end(word): | 20 | def end(word): | ||
21 | if len(word) % 3 == 0: | 21 | if len(word) % 3 == 0: | ||
22 | first = 2 * (len(word) // 3 ) | 22 | first = 2 * (len(word) // 3 ) | ||
23 | else: | 23 | else: | ||
24 | first = 2 * (len(word) // 3 ) + 1 | 24 | first = 2 * (len(word) // 3 ) + 1 | ||
25 | return word[first:] | 25 | return word[first:] | ||
26 | 26 | ||||
27 | def split_sentence(sentence): | 27 | def split_sentence(sentence): | ||
28 | return [(beginning(words), middle(words), end(words)) for words in sentence.split()] | 28 | return [(beginning(words), middle(words), end(words)) for words in sentence.split()] | ||
29 | 29 | ||||
30 | 30 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | def beginning(word): | f | 1 | def beginning(word): |
2 | if len(word) % 3 == 0 or len(word) % 3 == 1: | 2 | if len(word) % 3 == 0 or len(word) % 3 == 1: | ||
3 | last = len(word) // 3 | 3 | last = len(word) // 3 | ||
4 | elif len(word) % 3 == 2: | 4 | elif len(word) % 3 == 2: | ||
5 | last = len(word) // 3 + 1 | 5 | last = len(word) // 3 + 1 | ||
n | 6 | return word[0:last] | n | 6 | return word[:last] |
7 | 7 | ||||
8 | def middle(word): | 8 | def middle(word): | ||
9 | if len(word) % 3 == 0: | 9 | if len(word) % 3 == 0: | ||
10 | first = len(word) // 3 | 10 | first = len(word) // 3 | ||
11 | last = 2 * (len(word) // 3) | 11 | last = 2 * (len(word) // 3) | ||
12 | elif len(word) % 3 == 2: | 12 | elif len(word) % 3 == 2: | ||
13 | first = len(word) // 3 + 1 | 13 | first = len(word) // 3 + 1 | ||
14 | last = 2 * (len(word) // 3 ) + 1 | 14 | last = 2 * (len(word) // 3 ) + 1 | ||
15 | elif len(word) % 3 == 1: | 15 | elif len(word) % 3 == 1: | ||
16 | first = len(word) // 3 | 16 | first = len(word) // 3 | ||
17 | last = 2 * (len(word) // 3) + 1 | 17 | last = 2 * (len(word) // 3) + 1 | ||
18 | return word[first:last] | 18 | return word[first:last] | ||
19 | 19 | ||||
20 | def end(word): | 20 | def end(word): | ||
21 | if len(word) % 3 == 0: | 21 | if len(word) % 3 == 0: | ||
22 | first = 2 * (len(word) // 3 ) | 22 | first = 2 * (len(word) // 3 ) | ||
23 | else: | 23 | else: | ||
24 | first = 2 * (len(word) // 3 ) + 1 | 24 | first = 2 * (len(word) // 3 ) + 1 | ||
t | 25 | return word[first:len(word)] | t | 25 | return word[first:] |
26 | 26 | ||||
27 | def split_sentence(sentence): | 27 | def split_sentence(sentence): | ||
28 | return [(beginning(words), middle(words), end(words)) for words in sentence.split()] | 28 | return [(beginning(words), middle(words), end(words)) for words in sentence.split()] | ||
29 | 29 | ||||
30 | 30 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | def beginning(word): | f | 1 | def beginning(word): |
n | 2 | if len(word) % 3 == 0: | n | 2 | if len(word) % 3 == 0 or len(word) % 3 == 1: |
3 | last = len(word)//3 | 3 | last = len(word) // 3 | ||
4 | elif len(word) % 3 == 2: | 4 | elif len(word) % 3 == 2: | ||
5 | last = len(word) // 3 + 1 | 5 | last = len(word) // 3 + 1 | ||
n | 6 | elif len(word) % 3 == 1: | n | ||
7 | last = len(word) // 3 | ||||
8 | word_start = "" | ||||
9 | for i in range(0, last): | ||||
10 | word_start += word[i] | ||||
11 | return word_start | 6 | return word[0:last] | ||
12 | 7 | ||||
13 | def middle(word): | 8 | def middle(word): | ||
14 | if len(word) % 3 == 0: | 9 | if len(word) % 3 == 0: | ||
15 | first = len(word) // 3 | 10 | first = len(word) // 3 | ||
16 | last = 2 * (len(word) // 3) | 11 | last = 2 * (len(word) // 3) | ||
17 | elif len(word) % 3 == 2: | 12 | elif len(word) % 3 == 2: | ||
18 | first = len(word) // 3 + 1 | 13 | first = len(word) // 3 + 1 | ||
19 | last = 2 * (len(word) // 3 ) + 1 | 14 | last = 2 * (len(word) // 3 ) + 1 | ||
20 | elif len(word) % 3 == 1: | 15 | elif len(word) % 3 == 1: | ||
21 | first = len(word) // 3 | 16 | first = len(word) // 3 | ||
22 | last = 2 * (len(word) // 3) + 1 | 17 | last = 2 * (len(word) // 3) + 1 | ||
n | 23 | word_mid = "" | n | 18 | return word[first:last] |
24 | for i in range(first, last): | ||||
25 | word_mid += word[i] | ||||
26 | return word_mid | ||||
27 | 19 | ||||
28 | def end(word): | 20 | def end(word): | ||
29 | if len(word) % 3 == 0: | 21 | if len(word) % 3 == 0: | ||
30 | first = 2 * (len(word) // 3 ) | 22 | first = 2 * (len(word) // 3 ) | ||
n | 31 | elif len(word) % 3 == 2: | n | 23 | else: |
32 | first = 2 * (len(word) // 3 ) + 1 | 24 | first = 2 * (len(word) // 3 ) + 1 | ||
n | 33 | elif len(word) % 3 == 1: | n | 25 | return word[first:len(word)] |
34 | first = 2 * (len(word) // 3 ) + 1 | ||||
35 | word_end = "" | ||||
36 | for i in range(first, len(word)-1): | ||||
37 | word_end += word[i] | ||||
38 | word_end += word[len(word)-1] | ||||
39 | return word_end | ||||
40 | 26 | ||||
41 | def split_sentence(sentence): | 27 | def split_sentence(sentence): | ||
t | 42 | lst=[(beginning(words), middle(words), end(words)) for words in sentence.split()] | t | 28 | return [(beginning(words), middle(words), end(words)) for words in sentence.split()] |
43 | return lst | 29 | |||
44 | 30 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|