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

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

11 точки общо

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

 1# Homework 1
 2
 3WORD_DIVIDER_NUMBER = 3
 4
 5# The function should return the beginning of the word
 6def beginning(word):
 7    word_length = len(word)
 8    division_res = word_length // WORD_DIVIDER_NUMBER
 9    remainder = word_length % WORD_DIVIDER_NUMBER
10
11    if remainder == 2:
12        return word[:(division_res + 1)]
13    
14    return word[:division_res]
15
16# The function should return the end of the word
17def end(word):
18    reversed_word = word[::-1]
19    word_ending = beginning(reversed_word)
20    return word_ending[::-1]
21
22# The function should return the middle of the word
23def middle(word):
24    beginning_of_word = beginning(word)
25    end_of_word = end(word)
26
27    cut_beginning = word.removeprefix(beginning_of_word)
28    cut_end = cut_beginning.removesuffix(end_of_word)
29
30    return cut_end
31
32# The function splits the words inside the sentence
33def split_sentence(sentence):
34    splitted_words = sentence.split()
35    splitted_words_in_sentence = []
36
37    for word in splitted_words:
38        splitted_word = (beginning(word), middle(word), end(word))
39        splitted_words_in_sentence.append(splitted_word)
40
41    return splitted_words_in_sentence

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

OK

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

Иначе добро, нестандартно решение, браво.
История

f1# Homework 1f1# Homework 1
22
3WORD_DIVIDER_NUMBER = 33WORD_DIVIDER_NUMBER = 3
44
5# The function should return the beginning of the word5# The function should return the beginning of the word
6def beginning(word):6def beginning(word):
7    word_length = len(word)7    word_length = len(word)
8    division_res = word_length // WORD_DIVIDER_NUMBER8    division_res = word_length // WORD_DIVIDER_NUMBER
9    remainder = word_length % WORD_DIVIDER_NUMBER9    remainder = word_length % WORD_DIVIDER_NUMBER
1010
11    if remainder == 2:11    if remainder == 2:
12        return word[:(division_res + 1)]12        return word[:(division_res + 1)]
13    13    
14    return word[:division_res]14    return word[:division_res]
1515
16# The function should return the end of the word16# The function should return the end of the word
17def end(word):17def end(word):
18    reversed_word = word[::-1]18    reversed_word = word[::-1]
19    word_ending = beginning(reversed_word)19    word_ending = beginning(reversed_word)
20    return word_ending[::-1]20    return word_ending[::-1]
2121
22# The function should return the middle of the word22# The function should return the middle of the word
23def middle(word):23def middle(word):
24    beginning_of_word = beginning(word)24    beginning_of_word = beginning(word)
25    end_of_word = end(word)25    end_of_word = end(word)
2626
t27    cut_beginning = word.replace(beginning_of_word, '')t27    cut_beginning = word.removeprefix(beginning_of_word)
28    cut_end = cut_beginning.replace(end_of_word, '')28    cut_end = cut_beginning.removesuffix(end_of_word)
2929
30    return cut_end30    return cut_end
3131
32# The function splits the words inside the sentence32# The function splits the words inside the sentence
33def split_sentence(sentence):33def split_sentence(sentence):
34    splitted_words = sentence.split()34    splitted_words = sentence.split()
35    splitted_words_in_sentence = []35    splitted_words_in_sentence = []
3636
37    for word in splitted_words:37    for word in splitted_words:
38        splitted_word = (beginning(word), middle(word), end(word))38        splitted_word = (beginning(word), middle(word), end(word))
39        splitted_words_in_sentence.append(splitted_word)39        splitted_words_in_sentence.append(splitted_word)
4040
41    return splitted_words_in_sentence41    return splitted_words_in_sentence
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op