1def beginning(word):
2 if len(word) % 3 in (0, 1):
3 slice_to = len(word) // 3
4 else:
5 slice_to = len(word) // 3 + 1
6 return word[:slice_to]
7
8
9def middle(word):
10 if len(word) % 3 in (0, 2):
11 slice_offset = len(word) // 3
12 else:
13 slice_offset = len(word) // 3 + 1
14
15 if len(word) % 3 in (0, 1):
16 slice_from = len(word) // 3
17 else:
18 slice_from = len(word) // 3 + 1
19 slice_to = slice_from + slice_offset
20 return word[slice_from:slice_to]
21
22
23def end(word):
24 if len(word) % 3 in (0, 1):
25 slice_to = len(word) // 3
26 else:
27 slice_to = len(word) // 3 + 1
28 return word[-slice_to:]
29
30
31def split_sentence(sentence):
32 words = sentence.split()
33 result_split = []
34 for word in words:
35 beginning_word = beginning(word)
36 middle_word = middle(word)
37 end_word = end(word)
38 result_split.append((beginning_word, middle_word, end_word))
39 return result_split
40
41
42if __name__ == '__main__':
43 result = split_sentence("Kазвам се Джон Сноу")
44 print(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: [('Зд[50 chars], 'е'), ('м', '', 'и'), ('', 'е', 'е'), ('отв', 'ертк', 'ата')] != [('Зд[50 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 46, in test_one_letter
self.assertEqual(end('#'), '')
AssertionError: '#' != ''
- #
+
----------------------------------------------------------------------
Ran 12 tests in 0.001s
FAILED (failures=2)
| n | 1 | # This is a sample Python script. | n | ||
| 2 | |||||
| 3 | # Press Shift+F10 to execute it or replace it with your code. | ||||
| 4 | # Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings. | ||||
| 5 | 1 | ||||
| 6 | def beginning(word): | 2 | def beginning(word): | ||
| n | 7 | slice_to = 0 | n | 3 | if len(word) % 3 in (0, 1): |
| 8 | sliced_string = "" | ||||
| 9 | if len(word) % 3 == 0 or len(word) % 3 == 1: | ||||
| 10 | slice_to = len(word) // 3 | 4 | slice_to = len(word) // 3 | ||
| 11 | else: | 5 | else: | ||
| 12 | slice_to = len(word) // 3 + 1 | 6 | slice_to = len(word) // 3 + 1 | ||
| n | 13 | sliced_string = word[:slice_to] | n | 7 | return word[:slice_to] |
| 14 | return sliced_string | ||||
| 15 | 8 | ||||
| 16 | 9 | ||||
| 17 | def middle(word): | 10 | def middle(word): | ||
| n | 18 | slice_offset = 0 | n | 11 | if len(word) % 3 in (0, 2): |
| 19 | slice_to = 0 | ||||
| 20 | slice_from = 0 | ||||
| 21 | sliced_string = "" | ||||
| 22 | if len(word) % 3 == 0 or len(word) % 3 == 2: | ||||
| 23 | slice_offset = len(word) // 3 | 12 | slice_offset = len(word) // 3 | ||
| 24 | else: | 13 | else: | ||
| 25 | slice_offset = len(word) // 3 + 1 | 14 | slice_offset = len(word) // 3 + 1 | ||
| 26 | 15 | ||||
| n | 27 | if len(word) % 3 == 0 or len(word) % 3 == 1: | n | 16 | if len(word) % 3 in (0, 1): |
| 28 | slice_from = len(word) // 3 | 17 | slice_from = len(word) // 3 | ||
| 29 | else: | 18 | else: | ||
| 30 | slice_from = len(word) // 3 + 1 | 19 | slice_from = len(word) // 3 + 1 | ||
| 31 | slice_to = slice_from + slice_offset | 20 | slice_to = slice_from + slice_offset | ||
| n | 32 | sliced_string = word[slice_from:slice_to] | n | 21 | return word[slice_from:slice_to] |
| 33 | return sliced_string | ||||
| 34 | 22 | ||||
| 35 | 23 | ||||
| 36 | def end(word): | 24 | def end(word): | ||
| n | 37 | slice_to = 0 | n | 25 | if len(word) % 3 in (0, 1): |
| 38 | sliced_string = "" | ||||
| 39 | if len(word) % 3 == 0 or len(word) % 3 == 1: | ||||
| 40 | slice_to = len(word) // 3 | 26 | slice_to = len(word) // 3 | ||
| 41 | else: | 27 | else: | ||
| 42 | slice_to = len(word) // 3 + 1 | 28 | slice_to = len(word) // 3 + 1 | ||
| t | 43 | sliced_string = word[-slice_to:] | t | 29 | return word[-slice_to:] |
| 44 | return sliced_string | ||||
| 45 | 30 | ||||
| 46 | 31 | ||||
| 47 | def split_sentence(sentence): | 32 | def split_sentence(sentence): | ||
| 48 | words = sentence.split() | 33 | words = sentence.split() | ||
| 49 | result_split = [] | 34 | result_split = [] | ||
| 50 | for word in words: | 35 | for word in words: | ||
| 51 | beginning_word = beginning(word) | 36 | beginning_word = beginning(word) | ||
| 52 | middle_word = middle(word) | 37 | middle_word = middle(word) | ||
| 53 | end_word = end(word) | 38 | end_word = end(word) | ||
| 54 | result_split.append((beginning_word, middle_word, end_word)) | 39 | result_split.append((beginning_word, middle_word, end_word)) | ||
| 55 | return result_split | 40 | return result_split | ||
| 56 | 41 | ||||
| 57 | 42 | ||||
| 58 | if __name__ == '__main__': | 43 | if __name__ == '__main__': | ||
| 59 | result = split_sentence("Kазвам се Джон Сноу") | 44 | result = split_sentence("Kазвам се Джон Сноу") | ||
| 60 | print(result) | 45 | print(result) |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
16.10.2023 21:23