Домашни > Man who speaks the ends of words > Решения > Решението на Александър Ангелов

Резултати
10 точки от тестове
0 точки от учител

10 точки общо

12 успешни теста
0 неуспешни теста
Код

 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. Ще го пусна във форума с надеждата да получа звезда :)
История

f1def beginning(word):f1def beginning(word):
2    length = len(word)2    length = len(word)
n3    return_word = ""n
4    if length % 3 == 0:3    if length % 3 == 0:
n5        return_word = word[: (length//3)]n4        return_word = word[:(length//3)]
6    elif length % 3 == 2:5    elif length % 3 == 2:
n7        return_word = word[: (length//3+1)]n6        return_word = word[:(length//3+1)]
8    else:7    else:
n9        return_word = word[: (length//3)]n8        return_word = word[:(length//3)]
10    return return_word9    return return_word
11    10    
12def middle(word):11def middle(word):
13    length = len(word)12    length = len(word)
n14    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_word19    return return_word
22    20    
23def end(word):21def end(word):
24    length = len(word)22    length = len(word)
n25    return_word = "" n
26    if length % 3 == 0:23    if length % 3 == 0:
n27        return_word = word[(2*length//3) : length]n24        return_word = word[(2*length//3):]
28    elif length % 3 == 2:25    elif length % 3 == 2:
n29        return_word = word[(2*length//3) : length]n26        return_word = word[(2*length//3):]
30    else:27    else:
t31        return_word = word[(2*length//3 + 1) : length]t28        return_word = word[(2*length//3 + 1):]
32    return return_word29    return return_word
33    30    
34def split_sentence(sentence):31def 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 result40    return result
4441
45print(split_sentence("Kазвам се Джон Сноу"))     42print(split_sentence("Kазвам се Джон Сноу"))     
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

n1def result(word, start, end, returnWord):n
2    for i in range(start, end):
3        returnWord += word[i]
4    return returnWord
5 
6def beginning(word):1def beginning(word):
7    length = len(word)2    length = len(word)
n8    returnWord = ""n3    return_word = ""
9    if length % 3 == 0:4    if length % 3 == 0:
n10        returnWord = result(word, 0, length // 3, returnWord)n5        return_word = word[0 : (length//3)]
11    elif length % 3 == 2:6    elif length % 3 == 2:
n12        returnWord = result(word, 0, length // 3 + 1, returnWord)n7        return_word = word[0 : (length//3+1)]
13    else:8    else:
n14        returnWord = result(word, 0, length // 3, returnWord)n9        return_word = word[0 : (length//3)]
15    return returnWord10    return return_word
16    11    
17def middle(word):12def middle(word):
18    length = len(word)13    length = len(word)
n19    returnWord = "" n14    return_word = "" 
20    if length % 3 == 0:15    if length % 3 == 0:
n21        returnWord = result(word, length // 3, 2*length//3, returnWord)n16        return_word = word[(length//3) : (2*length//3)]
22    elif length % 3 == 2:17    elif length % 3 == 2:
n23        returnWord = result(word, length // 3 + 1, 2*length//3, returnWord)n18        return_word = word[(length//3 + 1) : (2*length//3)]
24    else:19    else:
n25        returnWord = result(word, length // 3, 2*length//3 + 1, returnWord)n20        return_word = word[(length//3) : (2*length//3+1)]
26    return returnWord21    return return_word
27    22    
28def end(word):23def end(word):
29    length = len(word)24    length = len(word)
n30    returnWord = "" n25    return_word = "" 
31    if length % 3 == 0:26    if length % 3 == 0:
n32        returnWord = result(word, 2* length // 3, length, returnWord)n27        return_word = word[(2*length//3) : length]
33    elif length % 3 == 2:28    elif length % 3 == 2:
n34        returnWord = result(word, 2* length // 3, length, returnWord)n29        return_word = word[(2*length//3) : length]
35    else:30    else:
n36        returnWord = result(word, 2* length // 3 + 1, length, returnWord)n31        return_word = word[(2*length//3 + 1) : length]
37    return returnWord32    return return_word
38    33    
39def split_sentence(sentence):34def 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:
t43        currBeg = beginning(word)t38        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 result43    return result
4944
50print(split_sentence("Kазвам се Джон Сноу"))     45print(split_sentence("Kазвам се Джон Сноу"))     
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1def result(word, start, end, returnWord):f1def 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 returnWord4    return returnWord
55
6def beginning(word):6def 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 returnWord15    return returnWord
16    16    
17def middle(word):17def 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 returnWord26    return returnWord
27    27    
28def end(word):28def 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 returnWord37    return returnWord
38    38    
n39def splitSentence(sentence):n39def 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 result48    return result
4949
t50print(splitSentence("Kазвам се Джон Сноу"))     t50print(split_sentence("Kазвам се Джон Сноу"))     
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op