1def beginning(word):
2 word_length = len(word)
3 size = int(word_length / 3)
4
5 if word_length % 3 == 2:
6 size = int(word_length / 3 + 1)
7
8 result = word[:size]
9
10 return result
11
12
13def middle(word):
14 word_length = len(word)
15 size = int(word_length / 3)
16
17 if word_length % 3 == 2:
18 size = int(word_length / 3 + 1)
19
20 result = word[size:-size]
21
22 return result
23
24
25def end(word):
26 word_length = len(word)
27 size = int(word_length / 3)
28
29 if word_length % 3 == 2:
30 size = int(word_length / 3 + 1)
31
32 result = word[-size:]
33
34 return result
35
36
37def split_sentence(sentence):
38 words = sentence.split()
39 result = []
40
41 for word in words :
42 my_tuple = beginning(word), middle(word), end(word)
43 result.append(my_tuple)
44
45 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)
Георги Кунчев
15.10.2023 10:44Не смятам, че е добра идея да вадиш сметките в отделната функция checkword. Реално останалите три функции стават еднакви и само играят ролята на посредник към истинската функционалност.
|
n | 1 | def beginning(word) : | n | 1 | def beginning(word): |
2 | word_length = len(word) | 2 | word_length = len(word) | ||
n | 3 | size = int(word_length/3) | n | 3 | size = int(word_length / 3) |
4 | 4 | ||||
5 | if word_length % 3 == 2: | 5 | if word_length % 3 == 2: | ||
n | 6 | size = int(word_length/3 + 1) | n | 6 | size = int(word_length / 3 + 1) |
7 | 7 | ||||
n | 8 | result = ''.join(word[0:size]) | n | 8 | result = word[:size] |
9 | 9 | ||||
10 | return result | 10 | return result | ||
11 | 11 | ||||
12 | 12 | ||||
n | 13 | def middle(word) : | n | 13 | def middle(word): |
14 | word_length = len(word) | 14 | word_length = len(word) | ||
n | 15 | size = int(word_length/3) | n | 15 | size = int(word_length / 3) |
16 | 16 | ||||
17 | if word_length % 3 == 2: | 17 | if word_length % 3 == 2: | ||
n | 18 | size = int(word_length/3 + 1) | n | 18 | size = int(word_length / 3 + 1) |
19 | 19 | ||||
n | 20 | result = ''.join(word[size:-size]) | n | 20 | result = word[size:-size] |
21 | 21 | ||||
22 | return result | 22 | return result | ||
23 | 23 | ||||
24 | 24 | ||||
25 | def end(word): | 25 | def end(word): | ||
26 | word_length = len(word) | 26 | word_length = len(word) | ||
n | 27 | size = int(word_length/3) | n | 27 | size = int(word_length / 3) |
28 | 28 | ||||
29 | if word_length % 3 == 2: | 29 | if word_length % 3 == 2: | ||
n | 30 | size = int(word_length/3 + 1) | n | 30 | size = int(word_length / 3 + 1) |
31 | 31 | ||||
n | 32 | result = ''.join(word[-size:]) | n | 32 | result = word[-size:] |
33 | 33 | ||||
34 | return result | 34 | return result | ||
35 | 35 | ||||
36 | 36 | ||||
37 | def split_sentence(sentence): | 37 | def split_sentence(sentence): | ||
38 | words = sentence.split() | 38 | words = sentence.split() | ||
39 | result = [] | 39 | result = [] | ||
40 | 40 | ||||
41 | for word in words : | 41 | for word in words : | ||
t | 42 | my_tuple = beginning(word),middle(word),end(word) | t | 42 | my_tuple = beginning(word), middle(word), end(word) |
43 | result.append(my_tuple) | 43 | result.append(my_tuple) | ||
44 | 44 | ||||
45 | return result | 45 | return result |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
t | 1 | def beginning(word) : | t | 1 | def beginning(word) : |
2 | word_length = len(word) | 2 | word_length = len(word) | ||
3 | size = int(word_length/3) | 3 | size = int(word_length/3) | ||
4 | 4 | ||||
5 | if word_length % 3 == 2: | 5 | if word_length % 3 == 2: | ||
6 | size = int(word_length/3 + 1) | 6 | size = int(word_length/3 + 1) | ||
7 | 7 | ||||
8 | result = ''.join(word[0:size]) | 8 | result = ''.join(word[0:size]) | ||
9 | 9 | ||||
10 | return result | 10 | return result | ||
11 | 11 | ||||
12 | 12 | ||||
13 | def middle(word) : | 13 | def middle(word) : | ||
14 | word_length = len(word) | 14 | word_length = len(word) | ||
15 | size = int(word_length/3) | 15 | size = int(word_length/3) | ||
16 | 16 | ||||
17 | if word_length % 3 == 2: | 17 | if word_length % 3 == 2: | ||
18 | size = int(word_length/3 + 1) | 18 | size = int(word_length/3 + 1) | ||
19 | 19 | ||||
20 | result = ''.join(word[size:-size]) | 20 | result = ''.join(word[size:-size]) | ||
21 | 21 | ||||
22 | return result | 22 | return result | ||
23 | 23 | ||||
24 | 24 | ||||
25 | def end(word): | 25 | def end(word): | ||
26 | word_length = len(word) | 26 | word_length = len(word) | ||
27 | size = int(word_length/3) | 27 | size = int(word_length/3) | ||
28 | 28 | ||||
29 | if word_length % 3 == 2: | 29 | if word_length % 3 == 2: | ||
30 | size = int(word_length/3 + 1) | 30 | size = int(word_length/3 + 1) | ||
31 | 31 | ||||
32 | result = ''.join(word[-size:]) | 32 | result = ''.join(word[-size:]) | ||
33 | 33 | ||||
34 | return result | 34 | return result | ||
35 | 35 | ||||
36 | 36 | ||||
37 | def split_sentence(sentence): | 37 | def split_sentence(sentence): | ||
38 | words = sentence.split() | 38 | words = sentence.split() | ||
39 | result = [] | 39 | result = [] | ||
40 | 40 | ||||
41 | for word in words : | 41 | for word in words : | ||
42 | my_tuple = beginning(word),middle(word),end(word) | 42 | my_tuple = beginning(word),middle(word),end(word) | ||
43 | result.append(my_tuple) | 43 | result.append(my_tuple) | ||
44 | 44 | ||||
45 | return result | 45 | return result |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
n | 1 | def checkWord(characters, length, orderedFrom): | n | 1 | def beginning(word) : |
2 | actualSize=int(length/3) | 2 | word_length = len(word) | ||
3 | size = int(word_length/3) | ||||
3 | 4 | ||||
n | 4 | if length % 3 == 2 : | n | 5 | if word_length % 3 == 2: |
5 | actualSize=int(length/3 + 1) | 6 | size = int(word_length/3 + 1) | ||
6 | |||||
7 | if orderedFrom == "beginning" : | ||||
8 | return characters[0:actualSize] | ||||
9 | elif orderedFrom == "middle" : | ||||
10 | return characters[actualSize:-actualSize] | ||||
11 | elif orderedFrom == "end" : | ||||
12 | return characters[-actualSize:] | ||||
13 | 7 | ||||
n | 14 | n | 8 | result = ''.join(word[0:size]) | |
15 | def beginning(word) : | ||||
16 | word_size = len(word) | ||||
17 | |||||
18 | characters = list(word) | ||||
19 | begin = checkWord(characters, word_size, "beginning") | ||||
20 | result=''.join(begin) | ||||
21 | 9 | ||||
22 | return result | 10 | return result | ||
23 | 11 | ||||
24 | 12 | ||||
25 | def middle(word) : | 13 | def middle(word) : | ||
n | 26 | word_size = len(word) | n | 14 | word_length = len(word) |
15 | size = int(word_length/3) | ||||
27 | 16 | ||||
n | 28 | characters = list(word) | n | 17 | if word_length % 3 == 2: |
29 | mid = checkWord(characters, word_size, "middle") | 18 | size = int(word_length/3 + 1) | ||
30 | result=''.join(mid) | 19 | |||
20 | result = ''.join(word[size:-size]) | ||||
31 | 21 | ||||
32 | return result | 22 | return result | ||
33 | 23 | ||||
34 | 24 | ||||
35 | def end(word): | 25 | def end(word): | ||
n | 36 | word_size = len(word) | n | 26 | word_length = len(word) |
27 | size = int(word_length/3) | ||||
37 | 28 | ||||
n | 38 | characters = list(word) | n | 29 | if word_length % 3 == 2: |
39 | ending = checkWord(characters, word_size, "end") | 30 | size = int(word_length/3 + 1) | ||
40 | result=''.join(ending) | 31 | |||
32 | result = ''.join(word[-size:]) | ||||
41 | 33 | ||||
42 | return result | 34 | return result | ||
43 | 35 | ||||
44 | 36 | ||||
45 | def split_sentence(sentence): | 37 | def split_sentence(sentence): | ||
46 | words = sentence.split() | 38 | words = sentence.split() | ||
47 | result = [] | 39 | result = [] | ||
48 | 40 | ||||
t | 49 | for i in range(0,len(words)) : | t | 41 | for word in words : |
50 | currentWord = [] | 42 | my_tuple = beginning(word),middle(word),end(word) | ||
51 | |||||
52 | currentWord.append(beginning(words[i])) | ||||
53 | currentWord.append(middle(words[i])) | ||||
54 | currentWord.append(end(words[i])) | ||||
55 | |||||
56 | myTuple = tuple(currentWord) | ||||
57 | |||||
58 | result.append(myTuple) | 43 | result.append(my_tuple) | ||
59 | 44 | ||||
60 | return result | 45 | return result |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | def checkWord(characters, length, orderedFrom): | f | 1 | def checkWord(characters, length, orderedFrom): |
2 | actualSize=int(length/3) | 2 | actualSize=int(length/3) | ||
3 | 3 | ||||
4 | if length % 3 == 2 : | 4 | if length % 3 == 2 : | ||
5 | actualSize=int(length/3 + 1) | 5 | actualSize=int(length/3 + 1) | ||
6 | 6 | ||||
7 | if orderedFrom == "beginning" : | 7 | if orderedFrom == "beginning" : | ||
n | 8 | return characters[0:actualSize] | n | 8 | return characters[0:actualSize] |
9 | elif orderedFrom == "middle" : | 9 | elif orderedFrom == "middle" : | ||
n | 10 | return characters[actualSize:-actualSize] | n | 10 | return characters[actualSize:-actualSize] |
11 | elif orderedFrom == "end" : | 11 | elif orderedFrom == "end" : | ||
t | 12 | return characters[-actualSize:] | t | 12 | return characters[-actualSize:] |
13 | 13 | ||||
14 | 14 | ||||
15 | def beginning(word) : | 15 | def beginning(word) : | ||
16 | word_size = len(word) | 16 | word_size = len(word) | ||
17 | 17 | ||||
18 | characters = list(word) | 18 | characters = list(word) | ||
19 | begin = checkWord(characters, word_size, "beginning") | 19 | begin = checkWord(characters, word_size, "beginning") | ||
20 | result=''.join(begin) | 20 | result=''.join(begin) | ||
21 | 21 | ||||
22 | return result | 22 | return result | ||
23 | 23 | ||||
24 | 24 | ||||
25 | def middle(word) : | 25 | def middle(word) : | ||
26 | word_size = len(word) | 26 | word_size = len(word) | ||
27 | 27 | ||||
28 | characters = list(word) | 28 | characters = list(word) | ||
29 | mid = checkWord(characters, word_size, "middle") | 29 | mid = checkWord(characters, word_size, "middle") | ||
30 | result=''.join(mid) | 30 | result=''.join(mid) | ||
31 | 31 | ||||
32 | return result | 32 | return result | ||
33 | 33 | ||||
34 | 34 | ||||
35 | def end(word): | 35 | def end(word): | ||
36 | word_size = len(word) | 36 | word_size = len(word) | ||
37 | 37 | ||||
38 | characters = list(word) | 38 | characters = list(word) | ||
39 | ending = checkWord(characters, word_size, "end") | 39 | ending = checkWord(characters, word_size, "end") | ||
40 | result=''.join(ending) | 40 | result=''.join(ending) | ||
41 | 41 | ||||
42 | return result | 42 | return result | ||
43 | 43 | ||||
44 | 44 | ||||
45 | def split_sentence(sentence): | 45 | def split_sentence(sentence): | ||
46 | words = sentence.split() | 46 | words = sentence.split() | ||
47 | result = [] | 47 | result = [] | ||
48 | 48 | ||||
49 | for i in range(0,len(words)) : | 49 | for i in range(0,len(words)) : | ||
50 | currentWord = [] | 50 | currentWord = [] | ||
51 | 51 | ||||
52 | currentWord.append(beginning(words[i])) | 52 | currentWord.append(beginning(words[i])) | ||
53 | currentWord.append(middle(words[i])) | 53 | currentWord.append(middle(words[i])) | ||
54 | currentWord.append(end(words[i])) | 54 | currentWord.append(end(words[i])) | ||
55 | 55 | ||||
56 | myTuple = tuple(currentWord) | 56 | myTuple = tuple(currentWord) | ||
57 | 57 | ||||
58 | result.append(myTuple) | 58 | result.append(myTuple) | ||
59 | 59 | ||||
60 | return result | 60 | return result |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
t | 1 | def checkWord(characters, length, orderedFrom): | t | 1 | def checkWord(characters, length, orderedFrom): |
2 | actualSize=int(length/3) | 2 | actualSize=int(length/3) | ||
3 | 3 | ||||
4 | if length % 3 == 2 : | 4 | if length % 3 == 2 : | ||
5 | actualSize=int(length/3 + 1) | 5 | actualSize=int(length/3 + 1) | ||
6 | 6 | ||||
7 | if orderedFrom == "beginning" : | 7 | if orderedFrom == "beginning" : | ||
8 | return characters[0:actualSize] | 8 | return characters[0:actualSize] | ||
9 | elif orderedFrom == "middle" : | 9 | elif orderedFrom == "middle" : | ||
10 | return characters[actualSize:-actualSize] | 10 | return characters[actualSize:-actualSize] | ||
11 | elif orderedFrom == "end" : | 11 | elif orderedFrom == "end" : | ||
12 | return characters[-actualSize:] | 12 | return characters[-actualSize:] | ||
13 | 13 | ||||
14 | 14 | ||||
15 | def beginning(word) : | 15 | def beginning(word) : | ||
16 | word_size = len(word) | 16 | word_size = len(word) | ||
17 | 17 | ||||
18 | characters = list(word) | 18 | characters = list(word) | ||
19 | begin = checkWord(characters, word_size, "beginning") | 19 | begin = checkWord(characters, word_size, "beginning") | ||
20 | result=''.join(begin) | 20 | result=''.join(begin) | ||
21 | 21 | ||||
22 | return result | 22 | return result | ||
23 | 23 | ||||
24 | 24 | ||||
25 | def middle(word) : | 25 | def middle(word) : | ||
26 | word_size = len(word) | 26 | word_size = len(word) | ||
27 | 27 | ||||
28 | characters = list(word) | 28 | characters = list(word) | ||
29 | mid = checkWord(characters, word_size, "middle") | 29 | mid = checkWord(characters, word_size, "middle") | ||
30 | result=''.join(mid) | 30 | result=''.join(mid) | ||
31 | 31 | ||||
32 | return result | 32 | return result | ||
33 | 33 | ||||
34 | 34 | ||||
35 | def end(word): | 35 | def end(word): | ||
36 | word_size = len(word) | 36 | word_size = len(word) | ||
37 | 37 | ||||
38 | characters = list(word) | 38 | characters = list(word) | ||
39 | ending = checkWord(characters, word_size, "end") | 39 | ending = checkWord(characters, word_size, "end") | ||
40 | result=''.join(ending) | 40 | result=''.join(ending) | ||
41 | 41 | ||||
42 | return result | 42 | return result | ||
43 | 43 | ||||
44 | 44 | ||||
45 | def split_sentence(sentence): | 45 | def split_sentence(sentence): | ||
46 | words = sentence.split() | 46 | words = sentence.split() | ||
47 | result = [] | 47 | result = [] | ||
48 | 48 | ||||
49 | for i in range(0,len(words)) : | 49 | for i in range(0,len(words)) : | ||
50 | currentWord = [] | 50 | currentWord = [] | ||
51 | 51 | ||||
52 | currentWord.append(beginning(words[i])) | 52 | currentWord.append(beginning(words[i])) | ||
53 | currentWord.append(middle(words[i])) | 53 | currentWord.append(middle(words[i])) | ||
54 | currentWord.append(end(words[i])) | 54 | currentWord.append(end(words[i])) | ||
55 | 55 | ||||
56 | myTuple = tuple(currentWord) | 56 | myTuple = tuple(currentWord) | ||
57 | 57 | ||||
58 | result.append(myTuple) | 58 | result.append(myTuple) | ||
59 | 59 | ||||
60 | return result | 60 | return result |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
17.10.2023 14:42