1def beginning(word):
2 length = len(word)
3 if length % 3 == 2:
4 return word[:length // 3 + 1]
5 else:
6 return word[:length // 3]
7
8def middle(word):
9 length = len(word)
10 if length % 3 == 0:
11 return word[length // 3 : (length * 2) // 3]
12 elif length % 3 == 1:
13 return word[length // 3 : (length * 2) // 3 + 1]
14 else:
15 return word[length // 3 + 1 : (length * 2) // 3]
16
17def end(word):
18 length = len(word)
19 if length % 3 == 1:
20 return word[(length * 2) // 3 + 1:]
21 else:
22 return word[(length * 2) // 3:]
23
24def split_sentence(sentence):
25 return [(beginning(word), middle(word), end(word)) for word in sentence.split()]
26
27print(split_sentence('Обичам да консумирам боб със зеле и за десерт вафла'))
[('Об', 'ич', 'ам'), ('д', '', 'а'), ('кон', 'суми', 'рам'), ('б', 'о', 'б'), ('с', 'ъ', 'с'), ('з', 'ел', 'е'), ('', 'и', ''), ('з', '', 'а'), ('де', 'се', 'рт'), ('ва', 'ф', 'ла')]
............
----------------------------------------------------------------------
Ran 12 tests in 0.000s
OK
f | 1 | def beginning(word): | f | 1 | def beginning(word): |
n | 2 | l = len(word) | n | 2 | length = len(word) |
3 | if l % 3 == 0: | 3 | if length % 3 == 2: | ||
4 | return word[:l // 3] | 4 | return word[:length // 3 + 1] | ||
5 | elif l % 3 == 1: | ||||
6 | return word[:l // 3] | ||||
7 | else: | 5 | else: | ||
n | 8 | return word[:l // 3 + 1] | n | 6 | return word[:length // 3] |
9 | 7 | ||||
10 | def middle(word): | 8 | def middle(word): | ||
n | 11 | l = len(word) | n | 9 | length = len(word) |
12 | if l % 3 == 0: | 10 | if length % 3 == 0: | ||
13 | return word[l // 3 : (l * 2) // 3] | 11 | return word[length // 3 : (length * 2) // 3] | ||
14 | elif l % 3 == 1: | 12 | elif length % 3 == 1: | ||
15 | return word[l // 3 : (l * 2) // 3 + 1] | 13 | return word[length // 3 : (length * 2) // 3 + 1] | ||
16 | else: | 14 | else: | ||
n | 17 | return word[l // 3 + 1 : (l * 2) // 3] | n | 15 | return word[length // 3 + 1 : (length * 2) // 3] |
18 | 16 | ||||
19 | def end(word): | 17 | def end(word): | ||
n | 20 | l = len(word) | n | 18 | length = len(word) |
21 | if l % 3 == 0: | ||||
22 | return word[(l * 2) // 3:] | ||||
23 | elif l % 3 == 1: | 19 | if length % 3 == 1: | ||
24 | return word[(l * 2) // 3 + 1:] | 20 | return word[(length * 2) // 3 + 1:] | ||
25 | else: | 21 | else: | ||
n | 26 | return word[(l * 2) // 3:] | n | 22 | return word[(length * 2) // 3:] |
27 | 23 | ||||
28 | def split_sentence(sentence): | 24 | def split_sentence(sentence): | ||
t | 29 | words = sentence.split() | t | 25 | return [(beginning(word), middle(word), end(word)) for word in sentence.split()] |
30 | for i in range(0,len(words)): | ||||
31 | words[i] = (beginning(words[i]),middle(words[i]),end(words[i])) | ||||
32 | return words | ||||
33 | 26 | ||||
34 | print(split_sentence('Обичам да консумирам боб със зеле и за десерт вафла')) | 27 | print(split_sentence('Обичам да консумирам боб със зеле и за десерт вафла')) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|