Домашни > Man who speaks the ends of words > Решения > Решението на Диан Василев

Резултати
8 точки от тестове
0 точки от учител

8 точки общо

10 успешни теста
2 неуспешни теста
Код
Скрий всички коментари

 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. Реално останалите три функции стават еднакви и само играят ролята на посредник към истинската функционалност.
История

n1def beginning(word) :n1def beginning(word):
2    word_length = len(word)2    word_length = len(word)
n3    size = int(word_length/3)n3    size = int(word_length / 3)
44
5    if word_length % 3 == 2:5    if word_length % 3 == 2:
n6        size = int(word_length/3 + 1)n6        size = int(word_length / 3 + 1)
77
n8    result = ''.join(word[0:size])n8    result = word[:size]
99
10    return result10    return result
1111
1212
n13def middle(word) :n13def middle(word):
14    word_length = len(word)14    word_length = len(word)
n15    size = int(word_length/3)n15    size = int(word_length / 3)
1616
17    if word_length % 3 == 2:17    if word_length % 3 == 2:
n18       size = int(word_length/3 + 1)n18       size = int(word_length / 3 + 1)
1919
n20    result = ''.join(word[size:-size])n20    result = word[size:-size]
2121
22    return result22    return result
2323
2424
25def end(word):25def end(word):
26    word_length = len(word)26    word_length = len(word)
n27    size = int(word_length/3)n27    size = int(word_length / 3)
2828
29    if word_length % 3 == 2:29    if word_length % 3 == 2:
n30        size = int(word_length/3 + 1)n30        size = int(word_length / 3 + 1)
3131
n32    result = ''.join(word[-size:])n32    result = word[-size:]
3333
34    return result34    return result
3535
3636
37def split_sentence(sentence):37def split_sentence(sentence):
38    words = sentence.split()38    words = sentence.split()
39    result = []39    result = []
4040
41    for word in words :41    for word in words :
t42       my_tuple = beginning(word),middle(word),end(word)t42       my_tuple = beginning(word), middle(word), end(word)
43       result.append(my_tuple)43       result.append(my_tuple)
4444
45    return result45    return result
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

t1def beginning(word) :t1def beginning(word) :
2    word_length = len(word)2    word_length = len(word)
3    size = int(word_length/3)3    size = int(word_length/3)
44
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)
77
8    result = ''.join(word[0:size])8    result = ''.join(word[0:size])
99
10    return result10    return result
1111
1212
13def middle(word) :13def middle(word) :
14    word_length = len(word)14    word_length = len(word)
15    size = int(word_length/3)15    size = int(word_length/3)
1616
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)
1919
20    result = ''.join(word[size:-size])20    result = ''.join(word[size:-size])
2121
22    return result22    return result
2323
2424
25def end(word):25def end(word):
26    word_length = len(word)26    word_length = len(word)
27    size = int(word_length/3)27    size = int(word_length/3)
2828
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)
3131
32    result = ''.join(word[-size:])32    result = ''.join(word[-size:])
3333
34    return result34    return result
3535
3636
37def split_sentence(sentence):37def split_sentence(sentence):
38    words = sentence.split()38    words = sentence.split()
39    result = []39    result = []
4040
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)
4444
45    return result45    return result
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

n1def checkWord(characters, length, orderedFrom):n1def beginning(word) :
2    actualSize=int(length/3)2    word_length = len(word)
3    size = int(word_length/3)
34
n4    if length % 3 == 2 :n5    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:]
137
n14 n8    result = ''.join(word[0:size])
15def beginning(word) :
16    word_size = len(word)
17 
18    characters = list(word)
19    begin = checkWord(characters, word_size, "beginning")
20    result=''.join(begin)
219
22    return result10    return result
2311
2412
25def middle(word) :13def middle(word) :
n26    word_size = len(word)n14    word_length = len(word)
15    size = int(word_length/3)
2716
n28    characters = list(word)n17    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])
3121
32    return result22    return result
3323
3424
35def end(word):25def end(word):
n36    word_size = len(word)n26    word_length = len(word)
27    size = int(word_length/3)
3728
n38    characters = list(word)n29    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:])
4133
42    return result34    return result
4335
4436
45def split_sentence(sentence):37def split_sentence(sentence):
46    words = sentence.split()38    words = sentence.split()
47    result = []39    result = []
4840
t49    for i in range(0,len(words)) :t41    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)
5944
60    return result45    return result
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1def checkWord(characters, length, orderedFrom):f1def checkWord(characters, length, orderedFrom):
2    actualSize=int(length/3)2    actualSize=int(length/3)
33
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" :
n8            return characters[0:actualSize]n8        return characters[0:actualSize]
9    elif orderedFrom == "middle" :9    elif orderedFrom == "middle" :
n10            return characters[actualSize:-actualSize]n10        return characters[actualSize:-actualSize]
11    elif orderedFrom == "end" :11    elif orderedFrom == "end" :
t12            return characters[-actualSize:]t12        return characters[-actualSize:]
1313
1414
15def beginning(word) :15def beginning(word) :
16    word_size = len(word)16    word_size = len(word)
1717
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)
2121
22    return result22    return result
2323
2424
25def middle(word) :25def middle(word) :
26    word_size = len(word)26    word_size = len(word)
2727
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)
3131
32    return result32    return result
3333
3434
35def end(word):35def end(word):
36    word_size = len(word)36    word_size = len(word)
3737
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)
4141
42    return result42    return result
4343
4444
45def split_sentence(sentence):45def split_sentence(sentence):
46    words = sentence.split()46    words = sentence.split()
47    result = []47    result = []
4848
49    for i in range(0,len(words)) :49    for i in range(0,len(words)) :
50        currentWord = []50        currentWord = []
5151
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]))
5555
56        myTuple = tuple(currentWord)56        myTuple = tuple(currentWord)
5757
58        result.append(myTuple)58        result.append(myTuple)
5959
60    return result60    return result
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

t1def checkWord(characters, length, orderedFrom):t1def checkWord(characters, length, orderedFrom):
2    actualSize=int(length/3)2    actualSize=int(length/3)
33
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:]
1313
1414
15def beginning(word) :15def beginning(word) :
16    word_size = len(word)16    word_size = len(word)
1717
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)
2121
22    return result22    return result
2323
2424
25def middle(word) :25def middle(word) :
26    word_size = len(word)26    word_size = len(word)
2727
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)
3131
32    return result32    return result
3333
3434
35def end(word):35def end(word):
36    word_size = len(word)36    word_size = len(word)
3737
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)
4141
42    return result42    return result
4343
4444
45def split_sentence(sentence):45def split_sentence(sentence):
46    words = sentence.split()46    words = sentence.split()
47    result = []47    result = []
4848
49    for i in range(0,len(words)) :49    for i in range(0,len(words)) :
50        currentWord = []50        currentWord = []
5151
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]))
5555
56        myTuple = tuple(currentWord)56        myTuple = tuple(currentWord)
5757
58        result.append(myTuple)58        result.append(myTuple)
5959
60    return result60    return result
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op