1def divide_by_three(word):
2 """Return the Whole and the Fraction Part of a Word's Length divided by 3"""
3
4 length = len(word)
5 return length // 3, length % 3
6
7
8def beginning(word):
9 """Slice the first len(word)/3 Characters of a Word"""
10
11 whole, fract = divide_by_three(word)
12
13 if fract in (0, 1):
14 return word[:whole:]
15 elif fract == 2:
16 return word[:whole + 1:]
17
18
19def middle(word):
20 """Slice the middle Part of a Word
21
22 More specifically the Characters no sliced by the Functions `beginning()` and `end()`
23 """
24
25 whole, fract = divide_by_three(word)
26
27 if fract in (0, 1):
28 return word[whole:-whole:]
29 elif fract == 2:
30 return word[whole + 1:-whole - 1:]
31
32
33def end(word):
34 """Slice the last len(word)/3 Characters of a Word"""
35
36 whole, fract = divide_by_three(word)
37
38 if fract in (0, 1):
39 return word[-whole::]
40 elif fract == 2:
41 return word[-whole - 1::]
42
43
44def split_sentence(sentence):
45 """Return a sliced Sentence using the Functions `beginning()`, `middle()`, and `end()`"""
46
47 words = sentence.split()
48 result = []
49
50 for word in words:
51 result.append((beginning(word), middle(word), end(word)))
52
53 return result
.F....F.....
======================================================================
FAIL: test_mixed_sentence (test.TestSentence)
Test with mixed remainder input.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 75, in test_mixed_sentence
self.assertEqual(split_sentence('Здравейте момчета къде ми е отвертката'),
AssertionError: Lists differ: [('Зд[49 chars]', 'е'), ('м', '', 'и'), ('', '', 'е'), ('отв', 'ертк', 'ата')] != [('Зд[49 chars]', 'е'), ('м', '', 'и'), ('', 'е', ''), ('отв', 'ертк', 'ата')]
First differing element 4:
('', '', 'е')
('', 'е', '')
[('Здр', 'аве', 'йте'),
('мо', 'мче', 'та'),
('к', 'ъд', 'е'),
('м', '', 'и'),
- ('', '', 'е'),
+ ('', 'е', ''),
('отв', 'ертк', 'ата')]
======================================================================
FAIL: test_one_letter (test.TestWords)
Test with single letter.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 45, in test_one_letter
self.assertEqual(middle('#'), '#')
AssertionError: '' != '#'
+ #
----------------------------------------------------------------------
Ran 12 tests in 0.001s
FAILED (failures=2)
Георги Кунчев
13.10.2023 08:45Оценявам добавените докстрингове.
Нещо, което е важно за тях, но вчера пропуснах да спомена - пишат се в "заповедна" форма. Вместо "Slices the..." - "Slice the...". Иначе са топ.
|
Филип Филчев
11.10.2023 12:51Относно split - видях, че split функцията на string не работи с регекси пък аз си тествах с едно голямо изречение, което копирах, и затова използвах тази.
Относно спейсовете - ползвам PyCharm и му се доверих, че автоматично ще ми ги форматира както трябва, но ще разуча малко повече. Мисля, че сега са окей. Ако не са кажете и ги оправя. Ще оправям проблемите докато не останат никакви :)
|
Георги Кунчев
11.10.2023 10:55Като цяло нивото на PEP8 е добре, но прегледай инструкциите за интервали при слайсване. Там ти куца малко.
https://peps.python.org/pep-0008/#whitespace-in-expressions-and-statements
|
Филип Филчев
10.10.2023 22:32Исках да изтествам как се показват грешките :Д
|
f | 1 | def divide_by_three(word): | f | 1 | def divide_by_three(word): |
n | 2 | """Returns the whole and the Fraction Part of a Float divided by 3""" | n | 2 | """Return the Whole and the Fraction Part of a Word's Length divided by 3""" |
3 | |||||
3 | length = len(word) | 4 | length = len(word) | ||
4 | return length // 3, length % 3 | 5 | return length // 3, length % 3 | ||
5 | 6 | ||||
6 | 7 | ||||
7 | def beginning(word): | 8 | def beginning(word): | ||
n | 8 | """Slices the first len(word)/3 characters of word""" | n | 9 | """Slice the first len(word)/3 Characters of a Word""" |
10 | |||||
9 | whole, fract = divide_by_three(word) | 11 | whole, fract = divide_by_three(word) | ||
10 | 12 | ||||
11 | if fract in (0, 1): | 13 | if fract in (0, 1): | ||
12 | return word[:whole:] | 14 | return word[:whole:] | ||
13 | elif fract == 2: | 15 | elif fract == 2: | ||
14 | return word[:whole + 1:] | 16 | return word[:whole + 1:] | ||
15 | 17 | ||||
16 | 18 | ||||
17 | def middle(word): | 19 | def middle(word): | ||
n | 18 | """Slices the middle part of word, the part not sliced by beginning() and end()""" | n | 20 | """Slice the middle Part of a Word |
21 | |||||
22 | More specifically the Characters no sliced by the Functions `beginning()` and `end()` | ||||
23 | """ | ||||
24 | |||||
19 | whole, fract = divide_by_three(word) | 25 | whole, fract = divide_by_three(word) | ||
20 | 26 | ||||
21 | if fract in (0, 1): | 27 | if fract in (0, 1): | ||
22 | return word[whole:-whole:] | 28 | return word[whole:-whole:] | ||
23 | elif fract == 2: | 29 | elif fract == 2: | ||
24 | return word[whole + 1:-whole - 1:] | 30 | return word[whole + 1:-whole - 1:] | ||
25 | 31 | ||||
26 | 32 | ||||
27 | def end(word): | 33 | def end(word): | ||
n | 28 | """Slices the last len(word)/3 characters of word""" | n | 34 | """Slice the last len(word)/3 Characters of a Word""" |
35 | |||||
29 | whole, fract = divide_by_three(word) | 36 | whole, fract = divide_by_three(word) | ||
30 | 37 | ||||
31 | if fract in (0, 1): | 38 | if fract in (0, 1): | ||
32 | return word[-whole::] | 39 | return word[-whole::] | ||
33 | elif fract == 2: | 40 | elif fract == 2: | ||
34 | return word[-whole - 1::] | 41 | return word[-whole - 1::] | ||
35 | 42 | ||||
36 | 43 | ||||
t | 37 | def split_sentence(sentence: str): | t | 44 | def split_sentence(sentence): |
38 | """Returns a sliced sentence using the functions beginning(), middle(), and end()""" | 45 | """Return a sliced Sentence using the Functions `beginning()`, `middle()`, and `end()`""" | ||
46 | |||||
39 | words = sentence.split() | 47 | words = sentence.split() | ||
40 | result = [] | 48 | result = [] | ||
41 | 49 | ||||
42 | for word in words: | 50 | for word in words: | ||
43 | result.append((beginning(word), middle(word), end(word))) | 51 | result.append((beginning(word), middle(word), end(word))) | ||
44 | 52 | ||||
45 | return result | 53 | return result |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | def divide_by_three(word): | f | 1 | def divide_by_three(word): |
n | n | 2 | """Returns the whole and the Fraction Part of a Float divided by 3""" | ||
2 | length = len(word) | 3 | length = len(word) | ||
n | 3 | return length // 3, length % 3 # returns the whole and the fract part of a float divided by 3 | n | 4 | return length // 3, length % 3 |
4 | 5 | ||||
5 | 6 | ||||
6 | def beginning(word): | 7 | def beginning(word): | ||
n | n | 8 | """Slices the first len(word)/3 characters of word""" | ||
7 | whole, fract = divide_by_three(word) | 9 | whole, fract = divide_by_three(word) | ||
8 | 10 | ||||
9 | if fract in (0, 1): | 11 | if fract in (0, 1): | ||
10 | return word[:whole:] | 12 | return word[:whole:] | ||
11 | elif fract == 2: | 13 | elif fract == 2: | ||
12 | return word[:whole + 1:] | 14 | return word[:whole + 1:] | ||
13 | 15 | ||||
14 | 16 | ||||
15 | def middle(word): | 17 | def middle(word): | ||
n | n | 18 | """Slices the middle part of word, the part not sliced by beginning() and end()""" | ||
16 | whole, fract = divide_by_three(word) | 19 | whole, fract = divide_by_three(word) | ||
17 | 20 | ||||
18 | if fract in (0, 1): | 21 | if fract in (0, 1): | ||
19 | return word[whole:-whole:] | 22 | return word[whole:-whole:] | ||
20 | elif fract == 2: | 23 | elif fract == 2: | ||
21 | return word[whole + 1:-whole - 1:] | 24 | return word[whole + 1:-whole - 1:] | ||
22 | 25 | ||||
23 | 26 | ||||
24 | def end(word): | 27 | def end(word): | ||
n | n | 28 | """Slices the last len(word)/3 characters of word""" | ||
25 | whole, fract = divide_by_three(word) | 29 | whole, fract = divide_by_three(word) | ||
26 | 30 | ||||
27 | if fract in (0, 1): | 31 | if fract in (0, 1): | ||
28 | return word[-whole::] | 32 | return word[-whole::] | ||
29 | elif fract == 2: | 33 | elif fract == 2: | ||
30 | return word[-whole - 1::] | 34 | return word[-whole - 1::] | ||
31 | 35 | ||||
32 | 36 | ||||
33 | def split_sentence(sentence: str): | 37 | def split_sentence(sentence: str): | ||
t | t | 38 | """Returns a sliced sentence using the functions beginning(), middle(), and end()""" | ||
34 | words = sentence.split() | 39 | words = sentence.split() | ||
35 | result = [] | 40 | result = [] | ||
36 | 41 | ||||
37 | for word in words: | 42 | for word in words: | ||
38 | result.append((beginning(word), middle(word), end(word))) | 43 | result.append((beginning(word), middle(word), end(word))) | ||
39 | 44 | ||||
40 | return result | 45 | return result |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
n | 1 | def get_condition_and_a_third_of_the_length(word): | n | 1 | def divide_by_three(word): |
2 | length = len(word) | 2 | length = len(word) | ||
n | 3 | return length % 3, length // 3 | n | 3 | return length // 3, length % 3 # returns the whole and the fract part of a float divided by 3 |
4 | 4 | ||||
5 | 5 | ||||
6 | def beginning(word): | 6 | def beginning(word): | ||
n | 7 | condition, third = get_condition_and_a_third_of_the_length(word) | n | 7 | whole, fract = divide_by_three(word) |
8 | 8 | ||||
n | 9 | if condition in (0, 1): | n | 9 | if fract in (0, 1): |
10 | return word[:third:] | 10 | return word[:whole:] | ||
11 | elif condition == 2: | 11 | elif fract == 2: | ||
12 | return word[:third + 1:] | 12 | return word[:whole + 1:] | ||
13 | 13 | ||||
14 | 14 | ||||
15 | def middle(word): | 15 | def middle(word): | ||
n | 16 | condition, third = get_condition_and_a_third_of_the_length(word) | n | 16 | whole, fract = divide_by_three(word) |
17 | 17 | ||||
n | 18 | if condition in (0, 1): | n | 18 | if fract in (0, 1): |
19 | return word[third:-third:] | 19 | return word[whole:-whole:] | ||
20 | elif condition == 2: | 20 | elif fract == 2: | ||
21 | return word[third + 1:-third - 1:] | 21 | return word[whole + 1:-whole - 1:] | ||
22 | 22 | ||||
23 | 23 | ||||
24 | def end(word): | 24 | def end(word): | ||
n | 25 | condition, third = get_condition_and_a_third_of_the_length(word) | n | 25 | whole, fract = divide_by_three(word) |
26 | 26 | ||||
n | 27 | if condition in (0, 1): | n | 27 | if fract in (0, 1): |
28 | return word[-third::] | 28 | return word[-whole::] | ||
29 | elif condition == 2: | 29 | elif fract == 2: | ||
30 | return word[-third - 1::] | 30 | return word[-whole - 1::] | ||
31 | 31 | ||||
32 | 32 | ||||
33 | def split_sentence(sentence: str): | 33 | def split_sentence(sentence: str): | ||
t | 34 | words = sentence.split(" ") | t | 34 | words = sentence.split() |
35 | result = [] | 35 | result = [] | ||
36 | 36 | ||||
37 | for word in words: | 37 | for word in words: | ||
38 | result.append((beginning(word), middle(word), end(word))) | 38 | result.append((beginning(word), middle(word), end(word))) | ||
39 | 39 | ||||
40 | return result | 40 | return result |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
n | 1 | from re import split | n | 1 | def get_condition_and_a_third_of_the_length(word): |
2 | |||||
3 | |||||
4 | def parse_needed_info(word): | ||||
5 | length = len(word) | 2 | length = len(word) | ||
n | 6 | return length, length % 3, length // 3 | n | 3 | return length % 3, length // 3 |
7 | 4 | ||||
8 | 5 | ||||
9 | def beginning(word): | 6 | def beginning(word): | ||
n | 10 | length, condition, third = parse_needed_info(word) | n | 7 | condition, third = get_condition_and_a_third_of_the_length(word) |
11 | 8 | ||||
n | 12 | if condition in [0, 1]: | n | 9 | if condition in (0, 1): |
13 | return word[:third:] | 10 | return word[:third:] | ||
14 | elif condition == 2: | 11 | elif condition == 2: | ||
15 | return word[:third + 1:] | 12 | return word[:third + 1:] | ||
16 | 13 | ||||
17 | 14 | ||||
18 | def middle(word): | 15 | def middle(word): | ||
n | 19 | length, condition, third = parse_needed_info(word) | n | 16 | condition, third = get_condition_and_a_third_of_the_length(word) |
20 | 17 | ||||
n | 21 | if condition in [0, 1]: | n | 18 | if condition in (0, 1): |
22 | return word[third: -third:] | 19 | return word[third:-third:] | ||
23 | elif condition == 2: | 20 | elif condition == 2: | ||
n | 24 | return word[third + 1: -third - 1:] | n | 21 | return word[third + 1:-third - 1:] |
25 | 22 | ||||
26 | 23 | ||||
27 | def end(word): | 24 | def end(word): | ||
n | 28 | length, condition, third = parse_needed_info(word) | n | 25 | condition, third = get_condition_and_a_third_of_the_length(word) |
29 | 26 | ||||
n | 30 | if condition in [0, 1]: | n | 27 | if condition in (0, 1): |
31 | return word[-third::] | 28 | return word[-third::] | ||
32 | elif condition == 2: | 29 | elif condition == 2: | ||
33 | return word[-third - 1::] | 30 | return word[-third - 1::] | ||
34 | 31 | ||||
35 | 32 | ||||
36 | def split_sentence(sentence: str): | 33 | def split_sentence(sentence: str): | ||
t | 37 | words = split('[\\s,.]+', sentence) | t | 34 | words = sentence.split(" ") |
38 | result = [] | 35 | result = [] | ||
39 | 36 | ||||
40 | for word in words: | 37 | for word in words: | ||
41 | result.append((beginning(word), middle(word), end(word))) | 38 | result.append((beginning(word), middle(word), end(word))) | ||
42 | 39 | ||||
43 | return result | 40 | return result |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | from re import split | f | 1 | from re import split |
2 | 2 | ||||
3 | 3 | ||||
4 | def parse_needed_info(word): | 4 | def parse_needed_info(word): | ||
5 | length = len(word) | 5 | length = len(word) | ||
6 | return length, length % 3, length // 3 | 6 | return length, length % 3, length // 3 | ||
7 | 7 | ||||
8 | 8 | ||||
9 | def beginning(word): | 9 | def beginning(word): | ||
10 | length, condition, third = parse_needed_info(word) | 10 | length, condition, third = parse_needed_info(word) | ||
11 | 11 | ||||
12 | if condition in [0, 1]: | 12 | if condition in [0, 1]: | ||
13 | return word[:third:] | 13 | return word[:third:] | ||
14 | elif condition == 2: | 14 | elif condition == 2: | ||
15 | return word[:third + 1:] | 15 | return word[:third + 1:] | ||
16 | 16 | ||||
17 | 17 | ||||
18 | def middle(word): | 18 | def middle(word): | ||
19 | length, condition, third = parse_needed_info(word) | 19 | length, condition, third = parse_needed_info(word) | ||
20 | 20 | ||||
21 | if condition in [0, 1]: | 21 | if condition in [0, 1]: | ||
n | 22 | return word[third: -third + condition:] | n | 22 | return word[third: -third:] |
23 | elif condition == 2: | 23 | elif condition == 2: | ||
t | 24 | return word[third + 1: -third + 1:] | t | 24 | return word[third + 1: -third - 1:] |
25 | 25 | ||||
26 | 26 | ||||
27 | def end(word): | 27 | def end(word): | ||
28 | length, condition, third = parse_needed_info(word) | 28 | length, condition, third = parse_needed_info(word) | ||
29 | 29 | ||||
30 | if condition in [0, 1]: | 30 | if condition in [0, 1]: | ||
31 | return word[-third::] | 31 | return word[-third::] | ||
32 | elif condition == 2: | 32 | elif condition == 2: | ||
33 | return word[-third - 1::] | 33 | return word[-third - 1::] | ||
34 | 34 | ||||
35 | 35 | ||||
36 | def split_sentence(sentence: str): | 36 | def split_sentence(sentence: str): | ||
37 | words = split('[\\s,.]+', sentence) | 37 | words = split('[\\s,.]+', sentence) | ||
38 | result = [] | 38 | result = [] | ||
39 | 39 | ||||
40 | for word in words: | 40 | for word in words: | ||
41 | result.append((beginning(word), middle(word), end(word))) | 41 | result.append((beginning(word), middle(word), end(word))) | ||
42 | 42 | ||||
43 | return result | 43 | return result |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | from re import split | f | 1 | from re import split |
2 | 2 | ||||
3 | 3 | ||||
4 | def parse_needed_info(word): | 4 | def parse_needed_info(word): | ||
5 | length = len(word) | 5 | length = len(word) | ||
6 | return length, length % 3, length // 3 | 6 | return length, length % 3, length // 3 | ||
7 | 7 | ||||
8 | 8 | ||||
9 | def beginning(word): | 9 | def beginning(word): | ||
10 | length, condition, third = parse_needed_info(word) | 10 | length, condition, third = parse_needed_info(word) | ||
11 | 11 | ||||
12 | if condition in [0, 1]: | 12 | if condition in [0, 1]: | ||
13 | return word[:third:] | 13 | return word[:third:] | ||
14 | elif condition == 2: | 14 | elif condition == 2: | ||
15 | return word[:third + 1:] | 15 | return word[:third + 1:] | ||
n | 16 | return '' | n | ||
17 | 16 | ||||
18 | 17 | ||||
19 | def middle(word): | 18 | def middle(word): | ||
20 | length, condition, third = parse_needed_info(word) | 19 | length, condition, third = parse_needed_info(word) | ||
21 | 20 | ||||
22 | if condition in [0, 1]: | 21 | if condition in [0, 1]: | ||
23 | return word[third: -third + condition:] | 22 | return word[third: -third + condition:] | ||
24 | elif condition == 2: | 23 | elif condition == 2: | ||
25 | return word[third + 1: -third + 1:] | 24 | return word[third + 1: -third + 1:] | ||
n | 26 | return '' | n | ||
27 | 25 | ||||
28 | 26 | ||||
29 | def end(word): | 27 | def end(word): | ||
30 | length, condition, third = parse_needed_info(word) | 28 | length, condition, third = parse_needed_info(word) | ||
31 | 29 | ||||
32 | if condition in [0, 1]: | 30 | if condition in [0, 1]: | ||
33 | return word[-third::] | 31 | return word[-third::] | ||
34 | elif condition == 2: | 32 | elif condition == 2: | ||
35 | return word[-third - 1::] | 33 | return word[-third - 1::] | ||
t | 36 | return '' | t | ||
37 | 34 | ||||
38 | 35 | ||||
39 | def split_sentence(sentence: str): | 36 | def split_sentence(sentence: str): | ||
40 | words = split('[\\s,.]+', sentence) | 37 | words = split('[\\s,.]+', sentence) | ||
41 | result = [] | 38 | result = [] | ||
42 | 39 | ||||
43 | for word in words: | 40 | for word in words: | ||
44 | result.append((beginning(word), middle(word), end(word))) | 41 | result.append((beginning(word), middle(word), end(word))) | ||
45 | 42 | ||||
46 | return result | 43 | return result |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | from re import split | f | 1 | from re import split |
2 | 2 | ||||
3 | 3 | ||||
4 | def parse_needed_info(word): | 4 | def parse_needed_info(word): | ||
5 | length = len(word) | 5 | length = len(word) | ||
6 | return length, length % 3, length // 3 | 6 | return length, length % 3, length // 3 | ||
7 | 7 | ||||
8 | 8 | ||||
9 | def beginning(word): | 9 | def beginning(word): | ||
10 | length, condition, third = parse_needed_info(word) | 10 | length, condition, third = parse_needed_info(word) | ||
11 | 11 | ||||
n | 12 | if not condition: | n | 12 | if condition in [0, 1]: |
13 | return word[:third:] | 13 | return word[:third:] | ||
14 | elif condition == 2: | 14 | elif condition == 2: | ||
15 | return word[:third + 1:] | 15 | return word[:third + 1:] | ||
n | 16 | elif condition == 1: | n | ||
17 | return word[:third:] | ||||
18 | return '' | 16 | return '' | ||
19 | 17 | ||||
20 | 18 | ||||
21 | def middle(word): | 19 | def middle(word): | ||
22 | length, condition, third = parse_needed_info(word) | 20 | length, condition, third = parse_needed_info(word) | ||
23 | 21 | ||||
n | 24 | if not condition: | n | 22 | if condition in [0, 1]: |
25 | return word[third: third * 2:] | 23 | return word[third: -third + condition:] | ||
26 | elif condition == 2: | 24 | elif condition == 2: | ||
n | 27 | return word[third + 1: third * 2 + 1:] | n | 25 | return word[third + 1: -third + 1:] |
28 | elif condition == 1: | ||||
29 | return word[third: third * 2 + 1:] | ||||
30 | return '' | 26 | return '' | ||
31 | 27 | ||||
32 | 28 | ||||
33 | def end(word): | 29 | def end(word): | ||
34 | length, condition, third = parse_needed_info(word) | 30 | length, condition, third = parse_needed_info(word) | ||
35 | 31 | ||||
n | 36 | if not condition: | n | 32 | if condition in [0, 1]: |
37 | return word[-third::] | 33 | return word[-third::] | ||
38 | elif condition == 2: | 34 | elif condition == 2: | ||
t | 39 | return word[-third - 1:length + 1:] | t | ||
40 | elif condition == 1: | ||||
41 | return word[-third::] | 35 | return word[-third - 1::] | ||
42 | return '' | 36 | return '' | ||
43 | 37 | ||||
44 | 38 | ||||
45 | def split_sentence(sentence: str): | 39 | def split_sentence(sentence: str): | ||
46 | words = split('[\\s,.]+', sentence) | 40 | words = split('[\\s,.]+', sentence) | ||
47 | result = [] | 41 | result = [] | ||
48 | 42 | ||||
49 | for word in words: | 43 | for word in words: | ||
50 | result.append((beginning(word), middle(word), end(word))) | 44 | result.append((beginning(word), middle(word), end(word))) | ||
51 | 45 | ||||
52 | return result | 46 | return result |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | from re import split | f | 1 | from re import split |
2 | 2 | ||||
3 | 3 | ||||
4 | def parse_needed_info(word): | 4 | def parse_needed_info(word): | ||
5 | length = len(word) | 5 | length = len(word) | ||
6 | return length, length % 3, length // 3 | 6 | return length, length % 3, length // 3 | ||
7 | 7 | ||||
8 | 8 | ||||
9 | def beginning(word): | 9 | def beginning(word): | ||
10 | length, condition, third = parse_needed_info(word) | 10 | length, condition, third = parse_needed_info(word) | ||
11 | 11 | ||||
12 | if not condition: | 12 | if not condition: | ||
13 | return word[:third:] | 13 | return word[:third:] | ||
14 | elif condition == 2: | 14 | elif condition == 2: | ||
15 | return word[:third + 1:] | 15 | return word[:third + 1:] | ||
16 | elif condition == 1: | 16 | elif condition == 1: | ||
17 | return word[:third:] | 17 | return word[:third:] | ||
18 | return '' | 18 | return '' | ||
19 | 19 | ||||
20 | 20 | ||||
21 | def middle(word): | 21 | def middle(word): | ||
22 | length, condition, third = parse_needed_info(word) | 22 | length, condition, third = parse_needed_info(word) | ||
23 | 23 | ||||
24 | if not condition: | 24 | if not condition: | ||
25 | return word[third: third * 2:] | 25 | return word[third: third * 2:] | ||
26 | elif condition == 2: | 26 | elif condition == 2: | ||
27 | return word[third + 1: third * 2 + 1:] | 27 | return word[third + 1: third * 2 + 1:] | ||
28 | elif condition == 1: | 28 | elif condition == 1: | ||
29 | return word[third: third * 2 + 1:] | 29 | return word[third: third * 2 + 1:] | ||
30 | return '' | 30 | return '' | ||
31 | 31 | ||||
32 | 32 | ||||
33 | def end(word): | 33 | def end(word): | ||
34 | length, condition, third = parse_needed_info(word) | 34 | length, condition, third = parse_needed_info(word) | ||
35 | 35 | ||||
36 | if not condition: | 36 | if not condition: | ||
37 | return word[-third::] | 37 | return word[-third::] | ||
38 | elif condition == 2: | 38 | elif condition == 2: | ||
39 | return word[-third - 1:length + 1:] | 39 | return word[-third - 1:length + 1:] | ||
40 | elif condition == 1: | 40 | elif condition == 1: | ||
41 | return word[-third::] | 41 | return word[-third::] | ||
42 | return '' | 42 | return '' | ||
43 | 43 | ||||
44 | 44 | ||||
45 | def split_sentence(sentence: str): | 45 | def split_sentence(sentence: str): | ||
46 | words = split('[\\s,.]+', sentence) | 46 | words = split('[\\s,.]+', sentence) | ||
47 | result = [] | 47 | result = [] | ||
48 | 48 | ||||
49 | for word in words: | 49 | for word in words: | ||
t | 50 | result.append((beginning(word), middle(word))) | t | 50 | result.append((beginning(word), middle(word), end(word))) |
51 | 51 | ||||
52 | return result | 52 | return result |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | from re import split | f | 1 | from re import split |
2 | 2 | ||||
3 | 3 | ||||
4 | def parse_needed_info(word): | 4 | def parse_needed_info(word): | ||
5 | length = len(word) | 5 | length = len(word) | ||
6 | return length, length % 3, length // 3 | 6 | return length, length % 3, length // 3 | ||
7 | 7 | ||||
8 | 8 | ||||
9 | def beginning(word): | 9 | def beginning(word): | ||
10 | length, condition, third = parse_needed_info(word) | 10 | length, condition, third = parse_needed_info(word) | ||
11 | 11 | ||||
12 | if not condition: | 12 | if not condition: | ||
13 | return word[:third:] | 13 | return word[:third:] | ||
14 | elif condition == 2: | 14 | elif condition == 2: | ||
15 | return word[:third + 1:] | 15 | return word[:third + 1:] | ||
16 | elif condition == 1: | 16 | elif condition == 1: | ||
17 | return word[:third:] | 17 | return word[:third:] | ||
18 | return '' | 18 | return '' | ||
19 | 19 | ||||
20 | 20 | ||||
21 | def middle(word): | 21 | def middle(word): | ||
22 | length, condition, third = parse_needed_info(word) | 22 | length, condition, third = parse_needed_info(word) | ||
23 | 23 | ||||
24 | if not condition: | 24 | if not condition: | ||
25 | return word[third: third * 2:] | 25 | return word[third: third * 2:] | ||
26 | elif condition == 2: | 26 | elif condition == 2: | ||
27 | return word[third + 1: third * 2 + 1:] | 27 | return word[third + 1: third * 2 + 1:] | ||
28 | elif condition == 1: | 28 | elif condition == 1: | ||
29 | return word[third: third * 2 + 1:] | 29 | return word[third: third * 2 + 1:] | ||
30 | return '' | 30 | return '' | ||
31 | 31 | ||||
32 | 32 | ||||
33 | def end(word): | 33 | def end(word): | ||
34 | length, condition, third = parse_needed_info(word) | 34 | length, condition, third = parse_needed_info(word) | ||
35 | 35 | ||||
36 | if not condition: | 36 | if not condition: | ||
37 | return word[-third::] | 37 | return word[-third::] | ||
38 | elif condition == 2: | 38 | elif condition == 2: | ||
39 | return word[-third - 1:length + 1:] | 39 | return word[-third - 1:length + 1:] | ||
40 | elif condition == 1: | 40 | elif condition == 1: | ||
41 | return word[-third::] | 41 | return word[-third::] | ||
42 | return '' | 42 | return '' | ||
43 | 43 | ||||
44 | 44 | ||||
45 | def split_sentence(sentence: str): | 45 | def split_sentence(sentence: str): | ||
46 | words = split('[\\s,.]+', sentence) | 46 | words = split('[\\s,.]+', sentence) | ||
47 | result = [] | 47 | result = [] | ||
48 | 48 | ||||
49 | for word in words: | 49 | for word in words: | ||
t | 50 | result.append((beginning(word), middle(word), end(word))) | t | 50 | result.append((beginning(word), middle(word))) |
51 | 51 | ||||
52 | return result | 52 | return result |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|