1def beginning(word):
2 residue = len(word) % 3
3 length = len(word) // 3
4 if residue in (0, 1):
5 return word[:length]
6 else:
7 return word[:length + 1]
8
9def middle(word):
10 residue = len(word) % 3
11 length = len(word) // 3
12 if residue in (0, 1):
13 return word[length:length*2 + residue]
14 else:
15 return word[length + 1:length*2 + residue - 1]
16
17def end(word):
18 residue = len(word) % 3
19 length = len(word) // 3
20 if residue in (0, 1):
21 return word[-length:]
22 else:
23 return word[-length - 1:]
24
25def split_sentence(sentence):
26 sentence_by_word = sentence.split()
27 splited = []
28 for word in sentence_by_word:
29 splited.append((beginning(word), middle(word), end(word)))
30 return splited
.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)
f | 1 | def beginning(word): | f | 1 | def beginning(word): |
n | 2 | res = len(word) % 3 | n | 2 | residue = len(word) % 3 |
3 | n = len(word) // 3 | 3 | length = len(word) // 3 | ||
4 | if res == 0: | 4 | if residue in (0, 1): | ||
5 | return word[0:n] | 5 | return word[:length] | ||
6 | elif res == 1: | ||||
7 | return word[0:n] | ||||
8 | else: | 6 | else: | ||
n | 9 | return word[0:n + 1] | n | 7 | return word[:length + 1] |
8 | |||||
10 | def middle(word): | 9 | def middle(word): | ||
n | 11 | res = len(word) % 3 | n | 10 | residue = len(word) % 3 |
12 | n = len(word) // 3 | 11 | length = len(word) // 3 | ||
13 | if res == 0: | 12 | if residue in (0, 1): | ||
14 | return word[n:n*2 + res] | 13 | return word[length:length*2 + residue] | ||
15 | elif res == 1: | ||||
16 | return word[n:n*2 + res] | ||||
17 | else: | 14 | else: | ||
n | 18 | return word[n+1:n*2 + res - 1] | n | 15 | return word[length + 1:length*2 + residue - 1] |
16 | |||||
19 | def end(word): | 17 | def end(word): | ||
n | 20 | res = len(word) % 3 | n | 18 | residue = len(word) % 3 |
21 | n = len(word) // 3 | 19 | length = len(word) // 3 | ||
22 | if res == 0: | 20 | if residue in (0, 1): | ||
23 | return word[-n:] | 21 | return word[-length:] | ||
24 | elif res == 1: | ||||
25 | return word[-n:] | ||||
26 | else: | 22 | else: | ||
n | 27 | return word[-n-1:] | n | 23 | return word[-length - 1:] |
28 | sentence = "Kазвам се Джон Сноу" | 24 | |||
29 | def split_sentence(sentence): | 25 | def split_sentence(sentence): | ||
30 | sentence_by_word = sentence.split() | 26 | sentence_by_word = sentence.split() | ||
31 | splited = [] | 27 | splited = [] | ||
32 | for word in sentence_by_word: | 28 | for word in sentence_by_word: | ||
n | 33 | splited.append((beginning(word),middle(word),end(word))) | n | 29 | splited.append((beginning(word), middle(word), end(word))) |
34 | return splited | 30 | return splited | ||
t | 35 | print(split_sentence(sentence)) | t |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|