1def beginning(word):
2 length = len(word) # take the length
3 end = length # default end
4
5 # in both 0|1 remainder cases we have the same amount
6 if length % 3 != 2:
7 end = end // 3
8 return word[0:end]
9 # other wise the 2 remainder case
10 end = end // 3 + 1
11 return word[0:end]
12
13def middle(word):
14 length = len(word)
15 start = 0 #default start
16 end = length # default end
17 # take the portion from the first third to the second third
18 if length % 3 == 0:
19 start = length // 3
20 end = 2 * start
21 return word[start:end]
22 # both the beg and end are the same length - leftover is the middle
23 if length % 3 == 1:
24 start = length // 3
25 end = length - (length // 3)
26 return word[start:end]
27 # same as the above but account for the 'extra' chars a 1 more
28 if length % 3 == 2:
29 start = length // 3 + 1
30 end = length - (length // 3) - 1
31 return word[start:end]
32
33def end(word):
34 # take the beginning of the reversed string
35 reversed_end = beginning(word[::-1])
36 # reverse it back and return
37 return reversed_end[::-1]
38
39def split_sentence(sentence):
40 result=[] # init a list
41 words=sentence.split() # split the sentence
42 for word in words: # traverse the words
43 parts=(beginning(word),middle(word),end(word)) # construct the tuple
44 result.append(parts) # add to the list
45 return result
............
----------------------------------------------------------------------
Ran 12 tests in 0.000s
OK
Георги Кунчев
16.10.2023 12:17Ок е дори да нямаш нито един (за домашните). Иначе, на места си е добре да оставиш, ако кодът не е достатъчно ясен по въпроса ЗАЩО се прави нещо.
|
Мартин Кузманов
16.10.2023 12:12А по принцип, проблем ли е да липсват коментари? Питам понеже не знаех как стои въпроса при оценяването на домашна.
|
Георги Кунчев
16.10.2023 09:08Доста голямо количество коментари си оставил. Коментарите са хубаво нещо, но ако това, което те показват, е очевидно от кода, са по-скоро излишни и само прачат на четящия да се концентрира. Например "split the sentence, construct the tuple"...никаква допълнителна информация. Коментарите в `end` са добре.
|
n | 1 | def beggining(word): | n | 1 | def beginning(word): |
2 | length = len(word) # take the length | 2 | length = len(word) # take the length | ||
3 | end = length # default end | 3 | end = length # default end | ||
n | 4 | n | 4 | ||
5 | # in both 0|1 remainder cases we have the same amount | 5 | # in both 0|1 remainder cases we have the same amount | ||
6 | if length % 3 != 2: | 6 | if length % 3 != 2: | ||
7 | end = end // 3 | 7 | end = end // 3 | ||
8 | return word[0:end] | 8 | return word[0:end] | ||
9 | # other wise the 2 remainder case | 9 | # other wise the 2 remainder case | ||
10 | end = end // 3 + 1 | 10 | end = end // 3 + 1 | ||
11 | return word[0:end] | 11 | return word[0:end] | ||
12 | 12 | ||||
13 | def middle(word): | 13 | def middle(word): | ||
14 | length = len(word) | 14 | length = len(word) | ||
15 | start = 0 #default start | 15 | start = 0 #default start | ||
16 | end = length # default end | 16 | end = length # default end | ||
17 | # take the portion from the first third to the second third | 17 | # take the portion from the first third to the second third | ||
18 | if length % 3 == 0: | 18 | if length % 3 == 0: | ||
19 | start = length // 3 | 19 | start = length // 3 | ||
20 | end = 2 * start | 20 | end = 2 * start | ||
21 | return word[start:end] | 21 | return word[start:end] | ||
22 | # both the beg and end are the same length - leftover is the middle | 22 | # both the beg and end are the same length - leftover is the middle | ||
23 | if length % 3 == 1: | 23 | if length % 3 == 1: | ||
24 | start = length // 3 | 24 | start = length // 3 | ||
25 | end = length - (length // 3) | 25 | end = length - (length // 3) | ||
26 | return word[start:end] | 26 | return word[start:end] | ||
27 | # same as the above but account for the 'extra' chars a 1 more | 27 | # same as the above but account for the 'extra' chars a 1 more | ||
28 | if length % 3 == 2: | 28 | if length % 3 == 2: | ||
29 | start = length // 3 + 1 | 29 | start = length // 3 + 1 | ||
30 | end = length - (length // 3) - 1 | 30 | end = length - (length // 3) - 1 | ||
31 | return word[start:end] | 31 | return word[start:end] | ||
32 | 32 | ||||
33 | def end(word): | 33 | def end(word): | ||
n | 34 | # take the beggining of the reversed string | n | 34 | # take the beginning of the reversed string |
35 | reversed_end = beggining(word[::-1]) | 35 | reversed_end = beginning(word[::-1]) | ||
36 | # reverse it back and return | 36 | # reverse it back and return | ||
37 | return reversed_end[::-1] | 37 | return reversed_end[::-1] | ||
38 | 38 | ||||
39 | def split_sentence(sentence): | 39 | def split_sentence(sentence): | ||
40 | result=[] # init a list | 40 | result=[] # init a list | ||
41 | words=sentence.split() # split the sentence | 41 | words=sentence.split() # split the sentence | ||
42 | for word in words: # traverse the words | 42 | for word in words: # traverse the words | ||
t | 43 | parts=(beggining(word),middle(word),end(word)) # construct the tuple | t | 43 | parts=(beginning(word),middle(word),end(word)) # construct the tuple |
44 | result.append(parts) # add to the list | 44 | result.append(parts) # add to the list | ||
45 | return result | 45 | return result |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
16.10.2023 09:05
16.10.2023 09:06
16.10.2023 09:06