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

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

8 точки общо

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

 1def divide_by_three(word):
 2    """Return the Whole and the Fraction Part of a Word's Length divided by 3"""
 3
 4    length = len(word)
 5    return length // 3, length % 3
 6
 7
 8def beginning(word):
 9    """Slice the first len(word)/3 Characters of a Word"""
10
11    whole, fract = divide_by_three(word)
12
13    if fract in (0, 1):
14        return word[:whole:]
15    elif fract == 2:
16        return word[:whole + 1:]
17
18
19def middle(word):
20    """Slice the middle Part of a Word
21
22    More specifically the Characters no sliced by the Functions `beginning()` and `end()`
23    """
24
25    whole, fract = divide_by_three(word)
26
27    if fract in (0, 1):
28        return word[whole:-whole:]
29    elif fract == 2:
30        return word[whole + 1:-whole - 1:]
31
32
33def end(word):
34    """Slice the last len(word)/3 Characters of a Word"""
35
36    whole, fract = divide_by_three(word)
37
38    if fract in (0, 1):
39        return word[-whole::]
40    elif fract == 2:
41        return word[-whole - 1::]
42
43
44def split_sentence(sentence):
45    """Return a sliced Sentence using the Functions `beginning()`, `middle()`, and `end()`"""
46
47    words = sentence.split()
48    result = []
49
50    for word in words:
51        result.append((beginning(word), middle(word), end(word)))
52
53    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)

Дискусия
Георги Кунчев
13.10.2023 08:45

Оценявам добавените докстрингове. Нещо, което е важно за тях, но вчера пропуснах да спомена - пишат се в "заповедна" форма. Вместо "Slices the..." - "Slice the...". Иначе са топ.
Филип Филчев
11.10.2023 12:51

Относно split - видях, че split функцията на string не работи с регекси пък аз си тествах с едно голямо изречение, което копирах, и затова използвах тази. Относно спейсовете - ползвам PyCharm и му се доверих, че автоматично ще ми ги форматира както трябва, но ще разуча малко повече. Мисля, че сега са окей. Ако не са кажете и ги оправя. Ще оправям проблемите докато не останат никакви :)
Георги Кунчев
11.10.2023 10:55

Като цяло нивото на PEP8 е добре, но прегледай инструкциите за интервали при слайсване. Там ти куца малко. https://peps.python.org/pep-0008/#whitespace-in-expressions-and-statements
Филип Филчев
10.10.2023 22:32

Исках да изтествам как се показват грешките :Д
История

f1def divide_by_three(word):f1def divide_by_three(word):
n2    """Returns the whole and the Fraction Part of a Float divided by 3"""n2    """Return the Whole and the Fraction Part of a Word's Length divided by 3"""
3 
3    length = len(word)4    length = len(word)
4    return length // 3, length % 35    return length // 3, length % 3
56
67
7def beginning(word):8def beginning(word):
n8    """Slices the first len(word)/3 characters of word"""n9    """Slice the first len(word)/3 Characters of a Word"""
10 
9    whole, fract = divide_by_three(word)11    whole, fract = divide_by_three(word)
1012
11    if fract in (0, 1):13    if fract in (0, 1):
12        return word[:whole:]14        return word[:whole:]
13    elif fract == 2:15    elif fract == 2:
14        return word[:whole + 1:]16        return word[:whole + 1:]
1517
1618
17def middle(word):19def middle(word):
n18    """Slices the middle part of word, the part not sliced by beginning() and end()"""n20    """Slice the middle Part of a Word
21 
22    More specifically the Characters no sliced by the Functions `beginning()` and `end()`
23    """
24 
19    whole, fract = divide_by_three(word)25    whole, fract = divide_by_three(word)
2026
21    if fract in (0, 1):27    if fract in (0, 1):
22        return word[whole:-whole:]28        return word[whole:-whole:]
23    elif fract == 2:29    elif fract == 2:
24        return word[whole + 1:-whole - 1:]30        return word[whole + 1:-whole - 1:]
2531
2632
27def end(word):33def end(word):
n28    """Slices the last len(word)/3 characters of word"""n34    """Slice the last len(word)/3 Characters of a Word"""
35 
29    whole, fract = divide_by_three(word)36    whole, fract = divide_by_three(word)
3037
31    if fract in (0, 1):38    if fract in (0, 1):
32        return word[-whole::]39        return word[-whole::]
33    elif fract == 2:40    elif fract == 2:
34        return word[-whole - 1::]41        return word[-whole - 1::]
3542
3643
t37def split_sentence(sentence: str):t44def split_sentence(sentence):
38    """Returns a sliced sentence using the functions beginning(), middle(), and end()"""45    """Return a sliced Sentence using the Functions `beginning()``middle()`, and `end()`"""
46 
39    words = sentence.split()47    words = sentence.split()
40    result = []48    result = []
4149
42    for word in words:50    for word in words:
43        result.append((beginning(word), middle(word), end(word)))51        result.append((beginning(word), middle(word), end(word)))
4452
45    return result53    return result
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1def divide_by_three(word):f1def divide_by_three(word):
nn2    """Returns the whole and the Fraction Part of a Float divided by 3"""
2    length = len(word)3    length = len(word)
n3    return length // 3, length % 3  # returns the whole and the fract part of a float divided by 3n4    return length // 3, length % 3
45
56
6def beginning(word):7def beginning(word):
nn8    """Slices the first len(word)/3 characters of word"""
7    whole, fract = divide_by_three(word)9    whole, fract = divide_by_three(word)
810
9    if fract in (0, 1):11    if fract in (0, 1):
10        return word[:whole:]12        return word[:whole:]
11    elif fract == 2:13    elif fract == 2:
12        return word[:whole + 1:]14        return word[:whole + 1:]
1315
1416
15def middle(word):17def middle(word):
nn18    """Slices the middle part of word, the part not sliced by beginning() and end()"""
16    whole, fract = divide_by_three(word)19    whole, fract = divide_by_three(word)
1720
18    if fract in (0, 1):21    if fract in (0, 1):
19        return word[whole:-whole:]22        return word[whole:-whole:]
20    elif fract == 2:23    elif fract == 2:
21        return word[whole + 1:-whole - 1:]24        return word[whole + 1:-whole - 1:]
2225
2326
24def end(word):27def end(word):
nn28    """Slices the last len(word)/3 characters of word"""
25    whole, fract = divide_by_three(word)29    whole, fract = divide_by_three(word)
2630
27    if fract in (0, 1):31    if fract in (0, 1):
28        return word[-whole::]32        return word[-whole::]
29    elif fract == 2:33    elif fract == 2:
30        return word[-whole - 1::]34        return word[-whole - 1::]
3135
3236
33def split_sentence(sentence: str):37def split_sentence(sentence: str):
tt38    """Returns a sliced sentence using the functions beginning(), middle(), and end()"""
34    words = sentence.split()39    words = sentence.split()
35    result = []40    result = []
3641
37    for word in words:42    for word in words:
38        result.append((beginning(word), middle(word), end(word)))43        result.append((beginning(word), middle(word), end(word)))
3944
40    return result45    return result
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

n1def get_condition_and_a_third_of_the_length(word):n1def divide_by_three(word):
2    length = len(word)2    length = len(word)
n3    return length % 3, length // 3n3    return length // 3, length % 3  # returns the whole and the fract part of a float divided by 3
44
55
6def beginning(word):6def beginning(word):
n7    condition, third = get_condition_and_a_third_of_the_length(word)n7    whole, fract = divide_by_three(word)
88
n9    if condition in (0, 1):n9    if fract in (0, 1):
10        return word[:third:]10        return word[:whole:]
11    elif condition == 2:11    elif fract == 2:
12        return word[:third + 1:]12        return word[:whole + 1:]
1313
1414
15def middle(word):15def middle(word):
n16    condition, third = get_condition_and_a_third_of_the_length(word)n16    whole, fract = divide_by_three(word)
1717
n18    if condition in (0, 1):n18    if fract in (0, 1):
19        return word[third:-third:]19        return word[whole:-whole:]
20    elif condition == 2:20    elif fract == 2:
21        return word[third + 1:-third - 1:]21        return word[whole + 1:-whole - 1:]
2222
2323
24def end(word):24def end(word):
n25    condition, third = get_condition_and_a_third_of_the_length(word)n25    whole, fract = divide_by_three(word)
2626
n27    if condition in (0, 1):n27    if fract in (0, 1):
28        return word[-third::]28        return word[-whole::]
29    elif condition == 2:29    elif fract == 2:
30        return word[-third - 1::]30        return word[-whole - 1::]
3131
3232
33def split_sentence(sentence: str):33def split_sentence(sentence: str):
t34    words = sentence.split(" ")t34    words = sentence.split()
35    result = []35    result = []
3636
37    for word in words:37    for word in words:
38        result.append((beginning(word), middle(word), end(word)))38        result.append((beginning(word), middle(word), end(word)))
3939
40    return result40    return result
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

n1from re import splitn1def get_condition_and_a_third_of_the_length(word):
2 
3 
4def parse_needed_info(word):
5    length = len(word)2    length = len(word)
n6    return length, length % 3, length // 3n3    return length % 3, length // 3
74
85
9def beginning(word):6def beginning(word):
n10    length, condition, third = parse_needed_info(word)n7    condition, third = get_condition_and_a_third_of_the_length(word)
118
n12    if condition in [0, 1]:n9    if condition in (0, 1):
13        return word[:third:]10        return word[:third:]
14    elif condition == 2:11    elif condition == 2:
15        return word[:third + 1:]12        return word[:third + 1:]
1613
1714
18def middle(word):15def middle(word):
n19    length, condition, third = parse_needed_info(word)n16    condition, third = get_condition_and_a_third_of_the_length(word)
2017
n21    if condition in [0, 1]:n18    if condition in (0, 1):
22        return word[third: -third:]19        return word[third:-third:]
23    elif condition == 2:20    elif condition == 2:
n24        return word[third + 1: -third - 1:]n21        return word[third + 1:-third - 1:]
2522
2623
27def end(word):24def end(word):
n28    length, condition, third = parse_needed_info(word)n25    condition, third = get_condition_and_a_third_of_the_length(word)
2926
n30    if condition in [0, 1]:n27    if condition in (0, 1):
31        return word[-third::]28        return word[-third::]
32    elif condition == 2:29    elif condition == 2:
33        return word[-third - 1::]30        return word[-third - 1::]
3431
3532
36def split_sentence(sentence: str):33def split_sentence(sentence: str):
t37    words = split('[\\s,.]+', sentence)t34    words = sentence.split(" ")
38    result = []35    result = []
3936
40    for word in words:37    for word in words:
41        result.append((beginning(word), middle(word), end(word)))38        result.append((beginning(word), middle(word), end(word)))
4239
43    return result40    return result
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1from re import splitf1from re import split
22
33
4def parse_needed_info(word):4def parse_needed_info(word):
5    length = len(word)5    length = len(word)
6    return length, length % 3, length // 36    return length, length % 3, length // 3
77
88
9def beginning(word):9def beginning(word):
10    length, condition, third = parse_needed_info(word)10    length, condition, third = parse_needed_info(word)
1111
12    if condition in [0, 1]:12    if condition in [0, 1]:
13        return word[:third:]13        return word[:third:]
14    elif condition == 2:14    elif condition == 2:
15        return word[:third + 1:]15        return word[:third + 1:]
1616
1717
18def middle(word):18def middle(word):
19    length, condition, third = parse_needed_info(word)19    length, condition, third = parse_needed_info(word)
2020
21    if condition in [0, 1]:21    if condition in [0, 1]:
n22        return word[third: -third + condition:]n22        return word[third: -third:]
23    elif condition == 2:23    elif condition == 2:
t24        return word[third + 1: -third + 1:]t24        return word[third + 1: -third - 1:]
2525
2626
27def end(word):27def end(word):
28    length, condition, third = parse_needed_info(word)28    length, condition, third = parse_needed_info(word)
2929
30    if condition in [0, 1]:30    if condition in [0, 1]:
31        return word[-third::]31        return word[-third::]
32    elif condition == 2:32    elif condition == 2:
33        return word[-third - 1::]33        return word[-third - 1::]
3434
3535
36def split_sentence(sentence: str):36def split_sentence(sentence: str):
37    words = split('[\\s,.]+', sentence)37    words = split('[\\s,.]+', sentence)
38    result = []38    result = []
3939
40    for word in words:40    for word in words:
41        result.append((beginning(word), middle(word), end(word)))41        result.append((beginning(word), middle(word), end(word)))
4242
43    return result43    return result
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1from re import splitf1from re import split
22
33
4def parse_needed_info(word):4def parse_needed_info(word):
5    length = len(word)5    length = len(word)
6    return length, length % 3, length // 36    return length, length % 3, length // 3
77
88
9def beginning(word):9def beginning(word):
10    length, condition, third = parse_needed_info(word)10    length, condition, third = parse_needed_info(word)
1111
12    if condition in [0, 1]:12    if condition in [0, 1]:
13        return word[:third:]13        return word[:third:]
14    elif condition == 2:14    elif condition == 2:
15        return word[:third + 1:]15        return word[:third + 1:]
n16    return ''n
1716
1817
19def middle(word):18def middle(word):
20    length, condition, third = parse_needed_info(word)19    length, condition, third = parse_needed_info(word)
2120
22    if condition in [0, 1]:21    if condition in [0, 1]:
23        return word[third: -third + condition:]22        return word[third: -third + condition:]
24    elif condition == 2:23    elif condition == 2:
25        return word[third + 1: -third + 1:]24        return word[third + 1: -third + 1:]
n26    return ''n
2725
2826
29def end(word):27def end(word):
30    length, condition, third = parse_needed_info(word)28    length, condition, third = parse_needed_info(word)
3129
32    if condition in [0, 1]:30    if condition in [0, 1]:
33        return word[-third::]31        return word[-third::]
34    elif condition == 2:32    elif condition == 2:
35        return word[-third - 1::]33        return word[-third - 1::]
t36    return ''t
3734
3835
39def split_sentence(sentence: str):36def split_sentence(sentence: str):
40    words = split('[\\s,.]+', sentence)37    words = split('[\\s,.]+', sentence)
41    result = []38    result = []
4239
43    for word in words:40    for word in words:
44        result.append((beginning(word), middle(word), end(word)))41        result.append((beginning(word), middle(word), end(word)))
4542
46    return result43    return result
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1from re import splitf1from re import split
22
33
4def parse_needed_info(word):4def parse_needed_info(word):
5    length = len(word)5    length = len(word)
6    return length, length % 3, length // 36    return length, length % 3, length // 3
77
88
9def beginning(word):9def beginning(word):
10    length, condition, third = parse_needed_info(word)10    length, condition, third = parse_needed_info(word)
1111
n12    if not condition:n12    if condition in [0, 1]:
13        return word[:third:]13        return word[:third:]
14    elif condition == 2:14    elif condition == 2:
15        return word[:third + 1:]15        return word[:third + 1:]
n16    elif condition == 1:n
17        return word[:third:]
18    return ''16    return ''
1917
2018
21def middle(word):19def middle(word):
22    length, condition, third = parse_needed_info(word)20    length, condition, third = parse_needed_info(word)
2321
n24    if not condition:n22    if condition in [0, 1]:
25        return word[third: third * 2:]23        return word[third: -third + condition:]
26    elif condition == 2:24    elif condition == 2:
n27        return word[third + 1: third * 2 + 1:]n25        return word[third + 1: -third + 1:]
28    elif condition == 1:
29        return word[third: third * 2 + 1:]
30    return ''26    return ''
3127
3228
33def end(word):29def end(word):
34    length, condition, third = parse_needed_info(word)30    length, condition, third = parse_needed_info(word)
3531
n36    if not condition:n32    if condition in [0, 1]:
37        return word[-third::]33        return word[-third::]
38    elif condition == 2:34    elif condition == 2:
t39        return word[-third - 1:length + 1:]t
40    elif condition == 1:
41        return word[-third::]35        return word[-third - 1::]
42    return ''36    return ''
4337
4438
45def split_sentence(sentence: str):39def split_sentence(sentence: str):
46    words = split('[\\s,.]+', sentence)40    words = split('[\\s,.]+', sentence)
47    result = []41    result = []
4842
49    for word in words:43    for word in words:
50        result.append((beginning(word), middle(word), end(word)))44        result.append((beginning(word), middle(word), end(word)))
5145
52    return result46    return result
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1from re import splitf1from re import split
22
33
4def parse_needed_info(word):4def parse_needed_info(word):
5    length = len(word)5    length = len(word)
6    return length, length % 3, length // 36    return length, length % 3, length // 3
77
88
9def beginning(word):9def beginning(word):
10    length, condition, third = parse_needed_info(word)10    length, condition, third = parse_needed_info(word)
1111
12    if not condition:12    if not condition:
13        return word[:third:]13        return word[:third:]
14    elif condition == 2:14    elif condition == 2:
15        return word[:third + 1:]15        return word[:third + 1:]
16    elif condition == 1:16    elif condition == 1:
17        return word[:third:]17        return word[:third:]
18    return ''18    return ''
1919
2020
21def middle(word):21def middle(word):
22    length, condition, third = parse_needed_info(word)22    length, condition, third = parse_needed_info(word)
2323
24    if not condition:24    if not condition:
25        return word[third: third * 2:]25        return word[third: third * 2:]
26    elif condition == 2:26    elif condition == 2:
27        return word[third + 1: third * 2 + 1:]27        return word[third + 1: third * 2 + 1:]
28    elif condition == 1:28    elif condition == 1:
29        return word[third: third * 2 + 1:]29        return word[third: third * 2 + 1:]
30    return ''30    return ''
3131
3232
33def end(word):33def end(word):
34    length, condition, third = parse_needed_info(word)34    length, condition, third = parse_needed_info(word)
3535
36    if not condition:36    if not condition:
37        return word[-third::]37        return word[-third::]
38    elif condition == 2:38    elif condition == 2:
39        return word[-third - 1:length + 1:]39        return word[-third - 1:length + 1:]
40    elif condition == 1:40    elif condition == 1:
41        return word[-third::]41        return word[-third::]
42    return ''42    return ''
4343
4444
45def split_sentence(sentence: str):45def split_sentence(sentence: str):
46    words = split('[\\s,.]+', sentence)46    words = split('[\\s,.]+', sentence)
47    result = []47    result = []
4848
49    for word in words:49    for word in words:
t50        result.append((beginning(word), middle(word)))t50        result.append((beginning(word), middle(word), end(word)))
5151
52    return result52    return result
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1from re import splitf1from re import split
22
33
4def parse_needed_info(word):4def parse_needed_info(word):
5    length = len(word)5    length = len(word)
6    return length, length % 3, length // 36    return length, length % 3, length // 3
77
88
9def beginning(word):9def beginning(word):
10    length, condition, third = parse_needed_info(word)10    length, condition, third = parse_needed_info(word)
1111
12    if not condition:12    if not condition:
13        return word[:third:]13        return word[:third:]
14    elif condition == 2:14    elif condition == 2:
15        return word[:third + 1:]15        return word[:third + 1:]
16    elif condition == 1:16    elif condition == 1:
17        return word[:third:]17        return word[:third:]
18    return ''18    return ''
1919
2020
21def middle(word):21def middle(word):
22    length, condition, third = parse_needed_info(word)22    length, condition, third = parse_needed_info(word)
2323
24    if not condition:24    if not condition:
25        return word[third: third * 2:]25        return word[third: third * 2:]
26    elif condition == 2:26    elif condition == 2:
27        return word[third + 1: third * 2 + 1:]27        return word[third + 1: third * 2 + 1:]
28    elif condition == 1:28    elif condition == 1:
29        return word[third: third * 2 + 1:]29        return word[third: third * 2 + 1:]
30    return ''30    return ''
3131
3232
33def end(word):33def end(word):
34    length, condition, third = parse_needed_info(word)34    length, condition, third = parse_needed_info(word)
3535
36    if not condition:36    if not condition:
37        return word[-third::]37        return word[-third::]
38    elif condition == 2:38    elif condition == 2:
39        return word[-third - 1:length + 1:]39        return word[-third - 1:length + 1:]
40    elif condition == 1:40    elif condition == 1:
41        return word[-third::]41        return word[-third::]
42    return ''42    return ''
4343
4444
45def split_sentence(sentence: str):45def split_sentence(sentence: str):
46    words = split('[\\s,.]+', sentence)46    words = split('[\\s,.]+', sentence)
47    result = []47    result = []
4848
49    for word in words:49    for word in words:
t50        result.append((beginning(word), middle(word), end(word)))t50        result.append((beginning(word), middle(word)))
5151
52    return result52    return result
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op