1def beginning(word):
2 str_len = len(word)
3 if str_len % 3 == 0:
4 step = (str_len // 3)
5 return word[:step]
6 elif str_len % 3 == 2:
7 symbols = (str_len-1)//2
8 return word[:symbols]
9 elif str_len % 3 == 1:
10 if str_len % 2 == 0:
11 symbols = (str_len - 2) // 2
12 else:
13 symbols = (str_len - 3) // 2
14 return word[:symbols]
15
16
17def middle(word):
18 str_len = len(word)
19 if str_len % 3 == 0:
20 step = (str_len // 3)
21 return word[step:step*2]
22 elif str_len % 3 == 2:
23 symbols = (str_len-1)//2
24 if str_len % 2 == 1:
25 return word[symbols]
26 else:
27 return word[symbols:symbols+2]
28 elif str_len % 3 == 1:
29 if str_len % 2 == 0:
30 symbols = (str_len - 2) // 2
31 return word[symbols:symbols + 2]
32 else:
33 symbols = (str_len - 3) // 2
34 return word[symbols:symbols + 3]
35
36
37def end(word):
38 str_len = len(word)
39 if str_len % 3 == 0:
40 step = (str_len // 3)
41 return word[step*2:]
42 elif str_len % 3 == 2:
43 symbols = (str_len-1)//2
44 return word[-symbols:]
45 elif str_len % 3 == 1:
46 if str_len % 2 == 0:
47 symbols = (str_len - 2) // 2
48 else:
49 symbols = (str_len - 3) // 2
50 return word[-symbols:]
51
52
53def split_sentence(sentence):
54 word_list = sentence.split()
55 result = []
56 for word in word_list:
57 res_list = [beginning(word), middle(word), end(word)]
58 result.append(tuple(res_list))
59 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: [('Зд[51 chars] 'е'), ('', 'ми', 'ми'), ('', 'е', ''), ('отве', 'рт', 'ката')] != [('Зд[51 chars] 'е'), ('м', '', 'и'), ('', 'е', ''), ('отв', 'ертк', 'ата')]
First differing element 3:
('', 'ми', 'ми')
('м', '', 'и')
[('Здр', 'аве', 'йте'),
('мо', 'мче', 'та'),
('к', 'ъд', 'е'),
- ('', 'ми', 'ми'),
+ ('м', '', 'и'),
('', 'е', ''),
- ('отве', 'рт', 'ката')]
? - -
+ ('отв', 'ертк', 'ата')]
? + +
======================================================================
FAIL: test_remainder_1 (test.TestWords)
Test splitting a word that has a remainder of 1 when divided by 3.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 31, in test_remainder_1
self.assertEqual(beginning(word), 'ааа')
AssertionError: 'аааб' != 'ааа'
- аааб
? -
+ ааа
----------------------------------------------------------------------
Ran 12 tests in 0.001s
FAILED (failures=2)
Виктор Бечев
17.10.2023 09:19Стилово нямам големи забележки, но се загубих из ифове и математически операции. Ако има някакъв бъг - good luck finding it.
|
17.10.2023 09:18