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

Резултати
10 точки от тестове
1 точки от учител

11 точки общо

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

 1NR_OF_PARTS_OF_DIVIDED_WORD = 3
 2
 3
 4def quotient_and_remainder(word):
 5    """ This function calculates the quotient and remainder of the length
 6    of a certain word divided by NR_OF_PARTS_OF_DIVIDED_WORD. """
 7
 8    length_word = len(word)
 9    quotient = length_word // NR_OF_PARTS_OF_DIVIDED_WORD
10    remainder = length_word % NR_OF_PARTS_OF_DIVIDED_WORD
11    return quotient, remainder
12
13
14def beginning(word):
15    """ This function returns the beginning of a certain word. """
16
17    quotient, remainder = quotient_and_remainder(word)
18    if remainder == 2:
19        return word[:quotient + 1]
20    return word[:quotient]
21
22
23def end(word):
24    """This function returns the end of a certain word. """
25
26    quotient, remainder = quotient_and_remainder(word)
27    length_word = len(word)
28    if remainder == 2:
29        return word[length_word - (quotient + 1):]
30    return word[length_word - quotient:]
31
32
33def middle(word):
34    """ This function returns the middle of a certain word. """
35
36    new_word = word.replace(beginning(word), '', 1)
37    new_word = new_word[::-1]
38    new_word = new_word.replace(end(word)[::-1], '', 1)
39    new_word = new_word[::-1]
40    return new_word
41
42
43def split_sentence(sentence):
44    """ This function splits every word from a
45    sentence by NR_OF_PARTS_OF_DIVIDED_WORD parts. """
46    sentence_list = []
47    for word in sentence.split():
48        first_part = beginning(word)
49        second_part = middle(word)
50        third_part = end(word)
51        sentence_list.append((first_part, second_part, third_part))
52    return sentence_list

............
----------------------------------------------------------------------
Ran 12 tests in 0.000s

OK

Дискусия
Виктор Бечев
17.10.2023 08:56

Като цяло добро решение - докстрингове, чистото решение и стил. :)
История
Това решение има само една версия.