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

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

8 точки общо

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

 1def beginning(word):
 2    if len(word) % 3 in (0, 1):
 3        slice_to = len(word) // 3
 4    else:
 5        slice_to = len(word) // 3 + 1
 6    return word[:slice_to]
 7
 8
 9def middle(word):
10    if len(word) % 3 in (0, 2):
11        slice_offset = len(word) // 3
12    else:
13        slice_offset = len(word) // 3 + 1
14
15    if len(word) % 3 in (0, 1):
16        slice_from = len(word) // 3
17    else:
18        slice_from = len(word) // 3 + 1
19    slice_to = slice_from + slice_offset
20    return word[slice_from:slice_to]
21
22
23def end(word):
24    if len(word) % 3 in (0, 1):
25        slice_to = len(word) // 3
26    else:
27        slice_to = len(word) // 3 + 1
28    return word[-slice_to:]
29
30
31def split_sentence(sentence):
32    words = sentence.split()
33    result_split = []
34    for word in words:
35        beginning_word = beginning(word)
36        middle_word = middle(word)
37        end_word = end(word)
38        result_split.append((beginning_word, middle_word, end_word))
39    return result_split
40
41
42if __name__ == '__main__':
43    result = split_sentence("Kазвам се Джон Сноу")
44    print(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: [('Зд[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)

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

n1# This is a sample Python script.n
2 
3# Press Shift+F10 to execute it or replace it with your code.
4# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
51
6def beginning(word):2def beginning(word):
n7    slice_to = 0n3    if len(word) % 3 in (0, 1):
8    sliced_string = ""
9    if len(word) % 3 == 0 or len(word) % 3 == 1:
10        slice_to = len(word) // 34        slice_to = len(word) // 3
11    else:5    else:
12        slice_to = len(word) // 3 + 16        slice_to = len(word) // 3 + 1
n13    sliced_string = word[:slice_to]n7    return word[:slice_to]
14    return sliced_string
158
169
17def middle(word):10def middle(word):
n18    slice_offset = 0n11    if len(word) % 3 in (0, 2):
19    slice_to = 0
20    slice_from = 0
21    sliced_string = ""
22    if len(word) % 3 == 0 or len(word) % 3 == 2:
23        slice_offset = len(word) // 312        slice_offset = len(word) // 3
24    else:13    else:
25        slice_offset = len(word) // 3 + 114        slice_offset = len(word) // 3 + 1
2615
n27    if len(word) % 3 == 0 or len(word) % 3 == 1:n16    if len(word) % 3 in (0, 1):
28        slice_from = len(word) // 317        slice_from = len(word) // 3
29    else:18    else:
30        slice_from = len(word) // 3 + 119        slice_from = len(word) // 3 + 1
31    slice_to = slice_from + slice_offset20    slice_to = slice_from + slice_offset
n32    sliced_string = word[slice_from:slice_to]n21    return word[slice_from:slice_to]
33    return sliced_string
3422
3523
36def end(word):24def end(word):
n37    slice_to = 0n25    if len(word) % 3 in (0, 1):
38    sliced_string = ""
39    if len(word) % 3 == 0 or len(word) % 3 == 1:
40        slice_to = len(word) // 326        slice_to = len(word) // 3
41    else:27    else:
42        slice_to = len(word) // 3 + 128        slice_to = len(word) // 3 + 1
t43    sliced_string = word[-slice_to:]t29    return word[-slice_to:]
44    return sliced_string
4530
4631
47def split_sentence(sentence):32def split_sentence(sentence):
48    words = sentence.split()33    words = sentence.split()
49    result_split = []34    result_split = []
50    for word in words:35    for word in words:
51        beginning_word = beginning(word)36        beginning_word = beginning(word)
52        middle_word = middle(word)37        middle_word = middle(word)
53        end_word = end(word)38        end_word = end(word)
54        result_split.append((beginning_word, middle_word, end_word))39        result_split.append((beginning_word, middle_word, end_word))
55    return result_split40    return result_split
5641
5742
58if __name__ == '__main__':43if __name__ == '__main__':
59    result = split_sentence("Kазвам се Джон Сноу")44    result = split_sentence("Kазвам се Джон Сноу")
60    print(result)45    print(result)
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op