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

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

8 точки общо

10 успешни теста
2 неуспешни теста
Код

 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)

Дискусия
История

f1def beginning(word):f1def beginning(word):
n2    res = len(word) % 3n2    residue = len(word) % 3
3    n = len(word) // 33    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: 
n9        return word[0:n + 1]n7        return word[:length + 1]
8    
10def middle(word):9def middle(word):
n11    res = len(word) % 3 n10    residue = len(word) % 3 
12    n = len(word) // 311    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: 
n18        return word[n+1:n*2 + res - 1]n15        return word[length + 1:length*2 + residue - 1]
16    
19def end(word):17def end(word):
n20    res = len(word) % 3 n18    residue = len(word) % 3 
21    n = len(word) // 319    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: 
n27        return word[-n-1:]    n23        return word[-length - 1:]    
28sentence = "Kазвам се Джон Сноу"24 
29def split_sentence(sentence):25def 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:
n33       splited.append((beginning(word),middle(word),end(word)))n29       splited.append((beginning(word), middle(word), end(word)))
34    return splited30    return splited
t35print(split_sentence(sentence))t
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op