1def beginning(word):
2 if len(word) % 3 == 0 or len(word) % 3 == 1:
3 end = len(word) // 3
4 return word[0:end]
5 elif len(word) % 3 == 2:
6 end = len(word) // 3 + 1
7 return word[0:end]
8
9
10def middle(word):
11 if len(word) % 3 == 0:
12 start = len(word) // 3
13 lengthM = len(word) // 3
14 return word[start:start + lengthM]
15 elif len(word) % 3 == 1:
16 start = len(word) // 3
17 end = len(word) // 3 + 1
18 return word[start:start + end]
19 else:
20 start = len(word) // 3 + 1
21 end = len(word) // 3
22 return word[start:start + end]
23
24
25def end(word):
26 if len(word) % 3 == 0:
27 start = 2 * len(word) // 3
28 lenE = len(word) // 3
29 return word[start:start + lenE]
30 elif len(word) % 3 == 2:
31 start = (len(word) // 3 + 1) + len(word) // 3
32 endE = len(word) // 3 + 1
33 return word[start:start + endE]
34 else:
35 start = (len(word) // 3 + 1) + len(word) // 3
36 endE = len(word) // 3
37 return word[start:start + endE]
38
39
40def split_sentence(sentence):
41 currentList = sentence.split()
42 myNewList = []
43 for word in currentList:
44 myTuple = (beginning(word), middle(word), end(word))
45 myNewList.append(myTuple)
46 return myNewList
47
48
49
50
51#tests
52#print(beginning('аб'))
53#print(middle('аб'))
54#print(end('аб'))
55#print(split_sentence('Kазвам се Джон Сноу'))
............
----------------------------------------------------------------------
Ran 12 tests in 0.000s
OK
Виктор Бечев
17.10.2023 09:24На много места създаваш променливи, които използваш точно веднъж - на реда след създаването им.
Вероятно го правиш с цел четимост, но на много места е излишно (много, не всички). Например за това с tuple-а в последната функция съм готов да споря. :p
|
f | 1 | | f | 1 | |
2 | def beginning(word): | 2 | def beginning(word): | ||
3 | if len(word) % 3 == 0 or len(word) % 3 == 1: | 3 | if len(word) % 3 == 0 or len(word) % 3 == 1: | ||
4 | end = len(word) // 3 | 4 | end = len(word) // 3 | ||
5 | return word[0:end] | 5 | return word[0:end] | ||
6 | elif len(word) % 3 == 2: | 6 | elif len(word) % 3 == 2: | ||
7 | end = len(word) // 3 + 1 | 7 | end = len(word) // 3 + 1 | ||
8 | return word[0:end] | 8 | return word[0:end] | ||
9 | 9 | ||||
10 | 10 | ||||
11 | def middle(word): | 11 | def middle(word): | ||
12 | if len(word) % 3 == 0: | 12 | if len(word) % 3 == 0: | ||
13 | start = len(word) // 3 | 13 | start = len(word) // 3 | ||
14 | lengthM = len(word) // 3 | 14 | lengthM = len(word) // 3 | ||
15 | return word[start:start + lengthM] | 15 | return word[start:start + lengthM] | ||
16 | elif len(word) % 3 == 1: | 16 | elif len(word) % 3 == 1: | ||
17 | start = len(word) // 3 | 17 | start = len(word) // 3 | ||
18 | end = len(word) // 3 + 1 | 18 | end = len(word) // 3 + 1 | ||
19 | return word[start:start + end] | 19 | return word[start:start + end] | ||
20 | else: | 20 | else: | ||
21 | start = len(word) // 3 + 1 | 21 | start = len(word) // 3 + 1 | ||
22 | end = len(word) // 3 | 22 | end = len(word) // 3 | ||
23 | return word[start:start + end] | 23 | return word[start:start + end] | ||
24 | 24 | ||||
25 | 25 | ||||
26 | def end(word): | 26 | def end(word): | ||
27 | if len(word) % 3 == 0: | 27 | if len(word) % 3 == 0: | ||
28 | start = 2 * len(word) // 3 | 28 | start = 2 * len(word) // 3 | ||
29 | lenE = len(word) // 3 | 29 | lenE = len(word) // 3 | ||
30 | return word[start:start + lenE] | 30 | return word[start:start + lenE] | ||
31 | elif len(word) % 3 == 2: | 31 | elif len(word) % 3 == 2: | ||
32 | start = (len(word) // 3 + 1) + len(word) // 3 | 32 | start = (len(word) // 3 + 1) + len(word) // 3 | ||
33 | endE = len(word) // 3 + 1 | 33 | endE = len(word) // 3 + 1 | ||
34 | return word[start:start + endE] | 34 | return word[start:start + endE] | ||
35 | else: | 35 | else: | ||
36 | start = (len(word) // 3 + 1) + len(word) // 3 | 36 | start = (len(word) // 3 + 1) + len(word) // 3 | ||
37 | endE = len(word) // 3 | 37 | endE = len(word) // 3 | ||
38 | return word[start:start + endE] | 38 | return word[start:start + endE] | ||
39 | 39 | ||||
40 | 40 | ||||
41 | def split_sentence(sentence): | 41 | def split_sentence(sentence): | ||
42 | currentList = sentence.split() | 42 | currentList = sentence.split() | ||
43 | myNewList = [] | 43 | myNewList = [] | ||
44 | for word in currentList: | 44 | for word in currentList: | ||
45 | myTuple = (beginning(word), middle(word), end(word)) | 45 | myTuple = (beginning(word), middle(word), end(word)) | ||
46 | myNewList.append(myTuple) | 46 | myNewList.append(myTuple) | ||
47 | return myNewList | 47 | return myNewList | ||
48 | 48 | ||||
49 | 49 | ||||
50 | 50 | ||||
51 | 51 | ||||
52 | #tests | 52 | #tests | ||
t | 53 | print(beginning('аб')) | t | 53 | #print(beginning('аб')) |
54 | print(middle('аб')) | 54 | #print(middle('аб')) | ||
55 | print(end('аб')) | 55 | #print(end('аб')) | ||
56 | print(split_sentence('Kазвам се Джон Сноу')) | 56 | #print(split_sentence('Kазвам се Джон Сноу')) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
17.10.2023 09:26
17.10.2023 09:21