1def beginning(word):
2 length = len(word)
3 if length % 3 == 0:
4 return_word = word[:(length//3)]
5 elif length % 3 == 2:
6 return_word = word[:(length//3+1)]
7 else:
8 return_word = word[:(length//3)]
9 return return_word
10
11def middle(word):
12 length = len(word)
13 if length % 3 == 0:
14 return_word = word[(length//3) : (2*length//3)]
15 elif length % 3 == 2:
16 return_word = word[(length//3 + 1) : (2*length//3)]
17 else:
18 return_word = word[(length//3) : (2*length//3+1)]
19 return return_word
20
21def end(word):
22 length = len(word)
23 if length % 3 == 0:
24 return_word = word[(2*length//3):]
25 elif length % 3 == 2:
26 return_word = word[(2*length//3):]
27 else:
28 return_word = word[(2*length//3 + 1):]
29 return return_word
30
31def split_sentence(sentence):
32 words = sentence.split()
33 result = []
34 for word in words:
35 curr_beg = beginning(word)
36 curr_mid = middle(word)
37 curr_end = end(word)
38 curr_split = (curr_beg, curr_mid, curr_end)
39 result.append(curr_split)
40 return result
41
42print(split_sentence("Kазвам се Джон Сноу"))
[('Kа', 'зв', 'ам'), ('с', '', 'е'), ('Д', 'жо', 'н'), ('С', 'но', 'у')]
............
----------------------------------------------------------------------
Ran 12 tests in 0.000s
OK
Александър Ангелов
12.10.2023 17:43представям вариант 2.0 на домашното.
|
Георги Кунчев
12.10.2023 17:13В общия случай не би трябвало да имаш проблем с версията.
За кейсинга:
- в Пайтън е прието да се ползва `snake_case`
- в условието дефинираме точните имена на класове и функции, които тестовете ни ще търсят. Спазвай ги, за да ти минат тестовете.
|
Александър Ангелов
12.10.2023 16:38Здравейте, ползвам Python 12.0. Видях, че се изисква ползване на някоя версия на Python 10. Надявам се това да не представлява проблем. На мен кода ми тръгва и връща правилния резултат. Забелязах, че като качих кода ми се скара от автоматичната проверка, че нямам "split_sentece" функция. Аз ползвах функция на име "splitSentece" - свикнал съм с cammel case. Редактирах я на split_sentence, та въпорсът ми е за в бъдеще в рамките на курса да се придържам ли към snake_case, който е препоръка от PEP 8 ?
п.с не знам защо, но като напиша split"долна черта"sentence ми реже долната черта и си го чете splitsentence. Ще го пусна във форума с надеждата да получа звезда :)
|
f | 1 | def beginning(word): | f | 1 | def beginning(word): |
2 | length = len(word) | 2 | length = len(word) | ||
n | 3 | return_word = "" | n | ||
4 | if length % 3 == 0: | 3 | if length % 3 == 0: | ||
n | 5 | return_word = word[0 : (length//3)] | n | 4 | return_word = word[:(length//3)] |
6 | elif length % 3 == 2: | 5 | elif length % 3 == 2: | ||
n | 7 | return_word = word[0 : (length//3+1)] | n | 6 | return_word = word[:(length//3+1)] |
8 | else: | 7 | else: | ||
n | 9 | return_word = word[0 : (length//3)] | n | 8 | return_word = word[:(length//3)] |
10 | return return_word | 9 | return return_word | ||
11 | 10 | ||||
12 | def middle(word): | 11 | def middle(word): | ||
13 | length = len(word) | 12 | length = len(word) | ||
n | 14 | return_word = "" | n | ||
15 | if length % 3 == 0: | 13 | if length % 3 == 0: | ||
16 | return_word = word[(length//3) : (2*length//3)] | 14 | return_word = word[(length//3) : (2*length//3)] | ||
17 | elif length % 3 == 2: | 15 | elif length % 3 == 2: | ||
18 | return_word = word[(length//3 + 1) : (2*length//3)] | 16 | return_word = word[(length//3 + 1) : (2*length//3)] | ||
19 | else: | 17 | else: | ||
20 | return_word = word[(length//3) : (2*length//3+1)] | 18 | return_word = word[(length//3) : (2*length//3+1)] | ||
21 | return return_word | 19 | return return_word | ||
22 | 20 | ||||
23 | def end(word): | 21 | def end(word): | ||
24 | length = len(word) | 22 | length = len(word) | ||
n | 25 | return_word = "" | n | ||
26 | if length % 3 == 0: | 23 | if length % 3 == 0: | ||
n | 27 | return_word = word[(2*length//3) : length] | n | 24 | return_word = word[(2*length//3):] |
28 | elif length % 3 == 2: | 25 | elif length % 3 == 2: | ||
n | 29 | return_word = word[(2*length//3) : length] | n | 26 | return_word = word[(2*length//3):] |
30 | else: | 27 | else: | ||
t | 31 | return_word = word[(2*length//3 + 1) : length] | t | 28 | return_word = word[(2*length//3 + 1):] |
32 | return return_word | 29 | return return_word | ||
33 | 30 | ||||
34 | def split_sentence(sentence): | 31 | def split_sentence(sentence): | ||
35 | words = sentence.split() | 32 | words = sentence.split() | ||
36 | result = [] | 33 | result = [] | ||
37 | for word in words: | 34 | for word in words: | ||
38 | curr_beg = beginning(word) | 35 | curr_beg = beginning(word) | ||
39 | curr_mid = middle(word) | 36 | curr_mid = middle(word) | ||
40 | curr_end = end(word) | 37 | curr_end = end(word) | ||
41 | curr_split = (curr_beg, curr_mid, curr_end) | 38 | curr_split = (curr_beg, curr_mid, curr_end) | ||
42 | result.append(curr_split) | 39 | result.append(curr_split) | ||
43 | return result | 40 | return result | ||
44 | 41 | ||||
45 | print(split_sentence("Kазвам се Джон Сноу")) | 42 | print(split_sentence("Kазвам се Джон Сноу")) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
n | 1 | def result(word, start, end, returnWord): | n | ||
2 | for i in range(start, end): | ||||
3 | returnWord += word[i] | ||||
4 | return returnWord | ||||
5 | |||||
6 | def beginning(word): | 1 | def beginning(word): | ||
7 | length = len(word) | 2 | length = len(word) | ||
n | 8 | returnWord = "" | n | 3 | return_word = "" |
9 | if length % 3 == 0: | 4 | if length % 3 == 0: | ||
n | 10 | returnWord = result(word, 0, length // 3, returnWord) | n | 5 | return_word = word[0 : (length//3)] |
11 | elif length % 3 == 2: | 6 | elif length % 3 == 2: | ||
n | 12 | returnWord = result(word, 0, length // 3 + 1, returnWord) | n | 7 | return_word = word[0 : (length//3+1)] |
13 | else: | 8 | else: | ||
n | 14 | returnWord = result(word, 0, length // 3, returnWord) | n | 9 | return_word = word[0 : (length//3)] |
15 | return returnWord | 10 | return return_word | ||
16 | 11 | ||||
17 | def middle(word): | 12 | def middle(word): | ||
18 | length = len(word) | 13 | length = len(word) | ||
n | 19 | returnWord = "" | n | 14 | return_word = "" |
20 | if length % 3 == 0: | 15 | if length % 3 == 0: | ||
n | 21 | returnWord = result(word, length // 3, 2*length//3, returnWord) | n | 16 | return_word = word[(length//3) : (2*length//3)] |
22 | elif length % 3 == 2: | 17 | elif length % 3 == 2: | ||
n | 23 | returnWord = result(word, length // 3 + 1, 2*length//3, returnWord) | n | 18 | return_word = word[(length//3 + 1) : (2*length//3)] |
24 | else: | 19 | else: | ||
n | 25 | returnWord = result(word, length // 3, 2*length//3 + 1, returnWord) | n | 20 | return_word = word[(length//3) : (2*length//3+1)] |
26 | return returnWord | 21 | return return_word | ||
27 | 22 | ||||
28 | def end(word): | 23 | def end(word): | ||
29 | length = len(word) | 24 | length = len(word) | ||
n | 30 | returnWord = "" | n | 25 | return_word = "" |
31 | if length % 3 == 0: | 26 | if length % 3 == 0: | ||
n | 32 | returnWord = result(word, 2* length // 3, length, returnWord) | n | 27 | return_word = word[(2*length//3) : length] |
33 | elif length % 3 == 2: | 28 | elif length % 3 == 2: | ||
n | 34 | returnWord = result(word, 2* length // 3, length, returnWord) | n | 29 | return_word = word[(2*length//3) : length] |
35 | else: | 30 | else: | ||
n | 36 | returnWord = result(word, 2* length // 3 + 1, length, returnWord) | n | 31 | return_word = word[(2*length//3 + 1) : length] |
37 | return returnWord | 32 | return return_word | ||
38 | 33 | ||||
39 | def split_sentence(sentence): | 34 | def split_sentence(sentence): | ||
40 | words = sentence.split() | 35 | words = sentence.split() | ||
41 | result = [] | 36 | result = [] | ||
42 | for word in words: | 37 | for word in words: | ||
t | 43 | currBeg = beginning(word) | t | 38 | curr_beg = beginning(word) |
44 | currMid = middle(word) | 39 | curr_mid = middle(word) | ||
45 | currEnd = end(word) | 40 | curr_end = end(word) | ||
46 | currSplit = (currBeg, currMid, currEnd) | 41 | curr_split = (curr_beg, curr_mid, curr_end) | ||
47 | result.append(currSplit) | 42 | result.append(curr_split) | ||
48 | return result | 43 | return result | ||
49 | 44 | ||||
50 | print(split_sentence("Kазвам се Джон Сноу")) | 45 | print(split_sentence("Kазвам се Джон Сноу")) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | def result(word, start, end, returnWord): | f | 1 | def result(word, start, end, returnWord): |
2 | for i in range(start, end): | 2 | for i in range(start, end): | ||
3 | returnWord += word[i] | 3 | returnWord += word[i] | ||
4 | return returnWord | 4 | return returnWord | ||
5 | 5 | ||||
6 | def beginning(word): | 6 | def beginning(word): | ||
7 | length = len(word) | 7 | length = len(word) | ||
8 | returnWord = "" | 8 | returnWord = "" | ||
9 | if length % 3 == 0: | 9 | if length % 3 == 0: | ||
10 | returnWord = result(word, 0, length // 3, returnWord) | 10 | returnWord = result(word, 0, length // 3, returnWord) | ||
11 | elif length % 3 == 2: | 11 | elif length % 3 == 2: | ||
12 | returnWord = result(word, 0, length // 3 + 1, returnWord) | 12 | returnWord = result(word, 0, length // 3 + 1, returnWord) | ||
13 | else: | 13 | else: | ||
14 | returnWord = result(word, 0, length // 3, returnWord) | 14 | returnWord = result(word, 0, length // 3, returnWord) | ||
15 | return returnWord | 15 | return returnWord | ||
16 | 16 | ||||
17 | def middle(word): | 17 | def middle(word): | ||
18 | length = len(word) | 18 | length = len(word) | ||
19 | returnWord = "" | 19 | returnWord = "" | ||
20 | if length % 3 == 0: | 20 | if length % 3 == 0: | ||
21 | returnWord = result(word, length // 3, 2*length//3, returnWord) | 21 | returnWord = result(word, length // 3, 2*length//3, returnWord) | ||
22 | elif length % 3 == 2: | 22 | elif length % 3 == 2: | ||
23 | returnWord = result(word, length // 3 + 1, 2*length//3, returnWord) | 23 | returnWord = result(word, length // 3 + 1, 2*length//3, returnWord) | ||
24 | else: | 24 | else: | ||
25 | returnWord = result(word, length // 3, 2*length//3 + 1, returnWord) | 25 | returnWord = result(word, length // 3, 2*length//3 + 1, returnWord) | ||
26 | return returnWord | 26 | return returnWord | ||
27 | 27 | ||||
28 | def end(word): | 28 | def end(word): | ||
29 | length = len(word) | 29 | length = len(word) | ||
30 | returnWord = "" | 30 | returnWord = "" | ||
31 | if length % 3 == 0: | 31 | if length % 3 == 0: | ||
32 | returnWord = result(word, 2* length // 3, length, returnWord) | 32 | returnWord = result(word, 2* length // 3, length, returnWord) | ||
33 | elif length % 3 == 2: | 33 | elif length % 3 == 2: | ||
34 | returnWord = result(word, 2* length // 3, length, returnWord) | 34 | returnWord = result(word, 2* length // 3, length, returnWord) | ||
35 | else: | 35 | else: | ||
36 | returnWord = result(word, 2* length // 3 + 1, length, returnWord) | 36 | returnWord = result(word, 2* length // 3 + 1, length, returnWord) | ||
37 | return returnWord | 37 | return returnWord | ||
38 | 38 | ||||
n | 39 | def splitSentence(sentence): | n | 39 | def split_sentence(sentence): |
40 | words = sentence.split() | 40 | words = sentence.split() | ||
41 | result = [] | 41 | result = [] | ||
42 | for word in words: | 42 | for word in words: | ||
43 | currBeg = beginning(word) | 43 | currBeg = beginning(word) | ||
44 | currMid = middle(word) | 44 | currMid = middle(word) | ||
45 | currEnd = end(word) | 45 | currEnd = end(word) | ||
46 | currSplit = (currBeg, currMid, currEnd) | 46 | currSplit = (currBeg, currMid, currEnd) | ||
47 | result.append(currSplit) | 47 | result.append(currSplit) | ||
48 | return result | 48 | return result | ||
49 | 49 | ||||
t | 50 | print(splitSentence("Kазвам се Джон Сноу")) | t | 50 | print(split_sentence("Kазвам се Джон Сноу")) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|