f | # Homework 1 | f | # Homework 1 |
| | | |
| WORD_DIVIDER_NUMBER = 3 | | WORD_DIVIDER_NUMBER = 3 |
| | | |
| # The function should return the beginning of the word | | # The function should return the beginning of the word |
| def beginning(word): | | def beginning(word): |
| word_length = len(word) | | word_length = len(word) |
| division_res = word_length // WORD_DIVIDER_NUMBER | | division_res = word_length // WORD_DIVIDER_NUMBER |
| remainder = word_length % WORD_DIVIDER_NUMBER | | remainder = word_length % WORD_DIVIDER_NUMBER |
| | | |
| if remainder == 2: | | if remainder == 2: |
| return word[:(division_res + 1)] | | return word[:(division_res + 1)] |
| | | |
| return word[:division_res] | | return word[:division_res] |
| | | |
| # The function should return the end of the word | | # The function should return the end of the word |
| def end(word): | | def end(word): |
| reversed_word = word[::-1] | | reversed_word = word[::-1] |
| word_ending = beginning(reversed_word) | | word_ending = beginning(reversed_word) |
| return word_ending[::-1] | | return word_ending[::-1] |
| | | |
| # The function should return the middle of the word | | # The function should return the middle of the word |
| def middle(word): | | def middle(word): |
| beginning_of_word = beginning(word) | | beginning_of_word = beginning(word) |
| end_of_word = end(word) | | end_of_word = end(word) |
| | | |
t | cut_beginning = word.replace(beginning_of_word, '') | t | cut_beginning = word.removeprefix(beginning_of_word) |
| cut_end = cut_beginning.replace(end_of_word, '') | | cut_end = cut_beginning.removesuffix(end_of_word) |
| | | |
| return cut_end | | return cut_end |
| | | |
| # The function splits the words inside the sentence | | # The function splits the words inside the sentence |
| def split_sentence(sentence): | | def split_sentence(sentence): |
| splitted_words = sentence.split() | | splitted_words = sentence.split() |
| splitted_words_in_sentence = [] | | splitted_words_in_sentence = [] |
| | | |
| for word in splitted_words: | | for word in splitted_words: |
| splitted_word = (beginning(word), middle(word), end(word)) | | splitted_word = (beginning(word), middle(word), end(word)) |
| splitted_words_in_sentence.append(splitted_word) | | splitted_words_in_sentence.append(splitted_word) |
| | | |
| return splitted_words_in_sentence | | return splitted_words_in_sentence |
17.10.2023 09:14