1def process_word(word):
2 """
3 Split the given word (string) into 3 parts (beginning, middle, end) based on a given formula.
4 For a length that is exactly divisible by 3 the word has a length of 3 * x so each part has a length of x.
5 For modular division with inverse 2 the word has a length of x * 3 + 2. Beginning: x + 1, middle: x, end: x + 1.
6 For modular division with inverse 1 the word has a length of x * 3 + 1. Beginning: x, middle: x + 1, end: x.
7 """
8 if type(word) is not str:
9 word = str(word)
10 if len(word) < 2:
11 return '', word, ''
12
13 third_of_len = len(word) // 3
14 if not len(word) % 3:
15 return word[:third_of_len], word[third_of_len:-third_of_len], word[third_of_len * 2:]
16 elif len(word) % 3 == 2:
17 return word[:third_of_len + 1], word[third_of_len + 1:-(third_of_len + 1)], word[third_of_len * 2 + 1:]
18 # Last case is if the inverse is equal to 1
19 return word[:third_of_len], word[third_of_len:-third_of_len], word[third_of_len * 2 + 1:]
20
21
22def beginning(word):
23 """Split the input word into 3 parts and return the beginning part as a string."""
24 return process_word(word)[0]
25
26
27def middle(word):
28 """Split the input word into 3 parts and return the middle part as a string."""
29 return process_word(word)[1]
30
31
32def end(word):
33 """Split the input word into 3 parts and return the last part as a string."""
34 return process_word(word)[2]
35
36
37def split_sentence(sentence):
38 """Split the sentence into tuples of split words. Return [(split first word), (split second word), ...]."""
39 if not sentence:
40 return [('', '', '')]
41 if type(sentence) is not str:
42 sentence = str(sentence)
43 return [process_word(word) for word in sentence.split()]
............
----------------------------------------------------------------------
Ran 12 tests in 0.000s
OK
Лилия Костадинова
16.10.2023 12:04Благодаря за бързата обратна връзка, ще имам едно на ум за следващите домашни.
|
Георги Кунчев
16.10.2023 10:55Не съм голям фен на изваждането на цялата логика от трите функции в една обща. Общата функция `process_word` става доста сложна и претрупана. Не държа да го преправяш, но го имай предвид за следващия път. Старай се функцията да има single responsibility. Може да се каже, че разделянето на думата на три парчета е single responsibility, но пък логиката и математиката там стават достра сериозни.
|
f | 1 | def process_word(word): | f | 1 | def process_word(word): |
2 | """ | 2 | """ | ||
3 | Split the given word (string) into 3 parts (beginning, middle, end) based on a given formula. | 3 | Split the given word (string) into 3 parts (beginning, middle, end) based on a given formula. | ||
4 | For a length that is exactly divisible by 3 the word has a length of 3 * x so each part has a length of x. | 4 | For a length that is exactly divisible by 3 the word has a length of 3 * x so each part has a length of x. | ||
5 | For modular division with inverse 2 the word has a length of x * 3 + 2. Beginning: x + 1, middle: x, end: x + 1. | 5 | For modular division with inverse 2 the word has a length of x * 3 + 2. Beginning: x + 1, middle: x, end: x + 1. | ||
6 | For modular division with inverse 1 the word has a length of x * 3 + 1. Beginning: x, middle: x + 1, end: x. | 6 | For modular division with inverse 1 the word has a length of x * 3 + 1. Beginning: x, middle: x + 1, end: x. | ||
7 | """ | 7 | """ | ||
n | n | 8 | if type(word) is not str: | ||
9 | word = str(word) | ||||
8 | if len(word) < 2: | 10 | if len(word) < 2: | ||
9 | return '', word, '' | 11 | return '', word, '' | ||
n | n | 12 | |||
10 | third_of_len = len(word) // 3 | 13 | third_of_len = len(word) // 3 | ||
11 | if not len(word) % 3: | 14 | if not len(word) % 3: | ||
n | 12 | return word[0:third_of_len], word[third_of_len:-third_of_len], word[third_of_len * 2:] | n | 15 | return word[:third_of_len], word[third_of_len:-third_of_len], word[third_of_len * 2:] |
13 | elif len(word) % 3 == 2: | 16 | elif len(word) % 3 == 2: | ||
n | 14 | return word[0:third_of_len + 1], word[third_of_len + 1:-(third_of_len + 1)], word[third_of_len * 2 + 1:] | n | 17 | return word[:third_of_len + 1], word[third_of_len + 1:-(third_of_len + 1)], word[third_of_len * 2 + 1:] |
18 | # Last case is if the inverse is equal to 1 | ||||
15 | return word[0:third_of_len], word[third_of_len:-third_of_len], word[third_of_len * 2 + 1:] | 19 | return word[:third_of_len], word[third_of_len:-third_of_len], word[third_of_len * 2 + 1:] | ||
16 | 20 | ||||
17 | 21 | ||||
18 | def beginning(word): | 22 | def beginning(word): | ||
19 | """Split the input word into 3 parts and return the beginning part as a string.""" | 23 | """Split the input word into 3 parts and return the beginning part as a string.""" | ||
20 | return process_word(word)[0] | 24 | return process_word(word)[0] | ||
21 | 25 | ||||
22 | 26 | ||||
23 | def middle(word): | 27 | def middle(word): | ||
24 | """Split the input word into 3 parts and return the middle part as a string.""" | 28 | """Split the input word into 3 parts and return the middle part as a string.""" | ||
25 | return process_word(word)[1] | 29 | return process_word(word)[1] | ||
26 | 30 | ||||
27 | 31 | ||||
28 | def end(word): | 32 | def end(word): | ||
29 | """Split the input word into 3 parts and return the last part as a string.""" | 33 | """Split the input word into 3 parts and return the last part as a string.""" | ||
30 | return process_word(word)[2] | 34 | return process_word(word)[2] | ||
31 | 35 | ||||
32 | 36 | ||||
33 | def split_sentence(sentence): | 37 | def split_sentence(sentence): | ||
34 | """Split the sentence into tuples of split words. Return [(split first word), (split second word), ...].""" | 38 | """Split the sentence into tuples of split words. Return [(split first word), (split second word), ...].""" | ||
t | 35 | return [(beginning(word), middle(word), end(word)) for word in sentence.split()] | t | 39 | if not sentence: |
40 | return [('', '', '')] | ||||
41 | if type(sentence) is not str: | ||||
42 | sentence = str(sentence) | ||||
43 | return [process_word(word) for word in sentence.split()] | ||||
44 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | def process_word(word): | f | 1 | def process_word(word): |
2 | """ | 2 | """ | ||
3 | Split the given word (string) into 3 parts (beginning, middle, end) based on a given formula. | 3 | Split the given word (string) into 3 parts (beginning, middle, end) based on a given formula. | ||
4 | For a length that is exactly divisible by 3 the word has a length of 3 * x so each part has a length of x. | 4 | For a length that is exactly divisible by 3 the word has a length of 3 * x so each part has a length of x. | ||
5 | For modular division with inverse 2 the word has a length of x * 3 + 2. Beginning: x + 1, middle: x, end: x + 1. | 5 | For modular division with inverse 2 the word has a length of x * 3 + 2. Beginning: x + 1, middle: x, end: x + 1. | ||
6 | For modular division with inverse 1 the word has a length of x * 3 + 1. Beginning: x, middle: x + 1, end: x. | 6 | For modular division with inverse 1 the word has a length of x * 3 + 1. Beginning: x, middle: x + 1, end: x. | ||
7 | """ | 7 | """ | ||
8 | if len(word) < 2: | 8 | if len(word) < 2: | ||
t | 9 | return ' ', word, ' ' | t | 9 | return '', word, '' |
10 | third_of_len = len(word) // 3 | 10 | third_of_len = len(word) // 3 | ||
11 | if not len(word) % 3: | 11 | if not len(word) % 3: | ||
12 | return word[0:third_of_len], word[third_of_len:-third_of_len], word[third_of_len * 2:] | 12 | return word[0:third_of_len], word[third_of_len:-third_of_len], word[third_of_len * 2:] | ||
13 | elif len(word) % 3 == 2: | 13 | elif len(word) % 3 == 2: | ||
14 | return word[0:third_of_len + 1], word[third_of_len + 1:-(third_of_len + 1)], word[third_of_len * 2 + 1:] | 14 | return word[0:third_of_len + 1], word[third_of_len + 1:-(third_of_len + 1)], word[third_of_len * 2 + 1:] | ||
15 | return word[0:third_of_len], word[third_of_len:-third_of_len], word[third_of_len * 2 + 1:] | 15 | return word[0:third_of_len], word[third_of_len:-third_of_len], word[third_of_len * 2 + 1:] | ||
16 | 16 | ||||
17 | 17 | ||||
18 | def beginning(word): | 18 | def beginning(word): | ||
19 | """Split the input word into 3 parts and return the beginning part as a string.""" | 19 | """Split the input word into 3 parts and return the beginning part as a string.""" | ||
20 | return process_word(word)[0] | 20 | return process_word(word)[0] | ||
21 | 21 | ||||
22 | 22 | ||||
23 | def middle(word): | 23 | def middle(word): | ||
24 | """Split the input word into 3 parts and return the middle part as a string.""" | 24 | """Split the input word into 3 parts and return the middle part as a string.""" | ||
25 | return process_word(word)[1] | 25 | return process_word(word)[1] | ||
26 | 26 | ||||
27 | 27 | ||||
28 | def end(word): | 28 | def end(word): | ||
29 | """Split the input word into 3 parts and return the last part as a string.""" | 29 | """Split the input word into 3 parts and return the last part as a string.""" | ||
30 | return process_word(word)[2] | 30 | return process_word(word)[2] | ||
31 | 31 | ||||
32 | 32 | ||||
33 | def split_sentence(sentence): | 33 | def split_sentence(sentence): | ||
34 | """Split the sentence into tuples of split words. Return [(split first word), (split second word), ...].""" | 34 | """Split the sentence into tuples of split words. Return [(split first word), (split second word), ...].""" | ||
35 | return [(beginning(word), middle(word), end(word)) for word in sentence.split()] | 35 | return [(beginning(word), middle(word), end(word)) for word in sentence.split()] |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|