Домашни > Man who speaks the ends of words > Решения > Решението на Михаела Илиева

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

10 точки общо

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

 1#This is the refactored code, after the course's team's advice
 2def beginning(word):
 3    syllable_length = len(word)//3
 4    if (len(word)%3 == 0 or len(word)%3 == 1):
 5        return word[:syllable_length]
 6    elif len(word)%3 == 2:
 7        return word[:syllable_length+1]
 8
 9
10def middle(word):
11    syllable_length = len(word)//3
12    if (len(word)%3 == 0 or len(word)%3 == 1):
13        return word[syllable_length:len(word)-syllable_length]
14    elif len(word)%3 == 2:
15        return word[syllable_length+1:len(word)-syllable_length-1]  
16
17
18def end(word):
19    syllable_length = len(word)//3
20    if (len(word)%3 == 0 or len(word)%3 == 1):
21        return word[len(word)-syllable_length:]
22    elif len(word)%3 == 2:
23        return word[len(word)-syllable_length-1:] 
24
25
26def split_sentence(sentence):
27    final_list_of_split_words = []
28    words = sentence.split()
29    for word in words:
30        current_tuple = (beginning(word),middle(word),end(word))
31        final_list_of_split_words.append(current_tuple)
32    return final_list_of_split_words

............
----------------------------------------------------------------------
Ran 12 tests in 0.000s

OK

Дискусия
Виктор Бечев
17.10.2023 09:38

Поставяй блоковете код в коментари в: \`\`\` *код* \`\`\` За да излиза като код. :)
Михаела Илиева
17.10.2023 00:52

Здравейте, прикачвам тук променения код, защото едва ли ще има време за обратна връзка преди крайния срок. Благодаря за съвета за слайсинга, кодът стана значително по-приятен за гледане :D def beginning(word): syllable_length = len(word)//3 if (len(word)%3 == 0 or len(word)%3 == 1): return word[0:syllable_length] elif len(word)%3 == 2: return word[0:syllable_length+1] def middle(word): syllable_length = len(word)//3 if (len(word)%3 == 0 or len(word)%3 == 1): return word[syllable_length:len(word)-syllable_length] elif len(word)%3 == 2: return word[syllable_length+1:len(word)-syllable_length-1] def end(word): syllable_length = len(word)//3 if (len(word)%3 == 0 or len(word)%3 == 1): return word[len(word)-syllable_length:] elif len(word)%3 == 2: return word[len(word)-syllable_length-1:] def split_sentence(sentence): final_list_of_split_words = [] words = sentence.split() for word in words: current_tuple = (beginning(word),middle(word),end(word)) final_list_of_split_words.append(current_tuple) return final_list_of_split_words Edit: Не знам защо се форматира така странно
История

n1#In the process of solving the homework, I rewrote the code in fewer linesn1#This is the refactored code, after the course's team's advice
2#For the first three functions it is comented
3#For the fourth one, the shorter version is the working one
4#The original one is comented 
5#I apologise for the inconvenience
6def beginning(word):2def beginning(word):
n7    begin = ""n
8    counter = 0
9    syllable_length = len(word)//33    syllable_length = len(word)//3
n10    if len(word)%3 == 0:n4    if (len(word)%3 == 0 or len(word)%3 == 1):
11        for letter in word:5        return word[:syllable_length]
12            counter += 1
13            if counter <= syllable_length:
14                begin += letter
15    elif len(word)%3 == 2:6    elif len(word)%3 == 2:
n16        for letter in word:n7        return word[:syllable_length+1]
17            counter += 1
18            if counter <= syllable_length+1:
19                begin += letter
20    elif len(word)%3 == 1:
21        for letter in word:
22            counter += 1
23            if counter <= syllable_length:
24                begin += letter
25    return begin
26    # for letter in word:
27    #     counter+=1
28    #     if (((len(word)%3 == 0 or len(word)%3 == 1) and 
29    #     counter <= syllable_length) or 
30    #     (len(word)%3 == 2 and counter <= syllable_length+1)):
31    #         begin += letter
32    # return begin
338
349
35def middle(word):10def middle(word):
n36    mid = ""n
37    counter = 0
38    syllable_length = len(word)//311    syllable_length = len(word)//3
n39    if len(word)%3 == 0:n12    if (len(word)%3 == 0 or len(word)%3 == 1):
40        for letter in word:13        return word[syllable_length:len(word)-syllable_length]
41            counter += 1
42            if counter > syllable_length:
43                if counter <= len(word)-syllable_length:
44                    mid += letter
45    elif len(word)%3 == 2:14    elif len(word)%3 == 2:
n46        for letter in word:n15        return word[syllable_length+1:len(word)-syllable_length-1]  
47            counter += 1
48            if counter > syllable_length+1:
49                if counter <= len(word)-syllable_length-1:
50                    mid += letter
51    elif len(word)%3 == 1:
52        for letter in word:
53            counter += 1
54            if counter > syllable_length:
55                if counter <= len(word)-syllable_length:
56                    mid += letter
57    return mid
58    # for letter in word:
59    #     counter+=1
60    #     if (((len(word)%3 == 0 or len(word)%3 == 1) and 
61    #     counter > syllable_length and 
62    #     counter <= len(word)-syllable_length) or 
63    #     (len(word)%3 == 2 and 
64    #     counter > syllable_length+1 and 
65    #     counter <= len(word)-syllable_length-1)):
66    #         mid += letter
67    # return mid    
6816
6917
70def end(word):18def end(word):
n71    fin = ""n
72    counter = 0
73    syllable_length = len(word)//319    syllable_length = len(word)//3
n74    if len(word)%3 == 0:n20    if (len(word)%3 == 0 or len(word)%3 == 1):
75        for letter in word:21        return word[len(word)-syllable_length:]
76            counter += 1
77            if counter > len(word)-syllable_length:
78                fin += letter
79    elif len(word)%3 == 2:22    elif len(word)%3 == 2:
n80        for letter in word:n23        return word[len(word)-syllable_length-1:] 
81            counter += 1
82            if counter > len(word)-syllable_length-1:
83                fin += letter
84    elif len(word)%3 == 1:
85        for letter in word:
86            counter += 1
87            if counter > len(word)-syllable_length:
88                fin += letter
89    return fin
90    # for letter in word:
91    #     counter += 1
92    #     if (((len(word)%3 == 0 or len(word)%3 == 1) and
93    #     counter > len(word)-syllable_length) or
94    #     (len(word)%3 == 2 and 
95    #     counter > len(word)-syllable_length-1)):
96    #         fin += letter
97    # return fin    
9824
9925
100def split_sentence(sentence):26def split_sentence(sentence):
t101    #Before finding the split functiont
102    # current_word = ""
103    # final_list_of_split_words = []
104    # #The for loop will work only for words that have space after them
105    # for i in sentence:
106    #     if i != " ":
107    #         current_word += i
108    #     else:
109    #         current_tuple = (beginning(current_word),
110    #         middle(current_word),
111    #         end(current_word))
112    #         final_list_of_split_words.append(current_tuple)
113    #         current_word = ""
114    # #Getting the last word of the sentence
115    # index_of_last_space = sentence.rfind(" ")
116    # #in case the sentence ends with space
117    # if sentence[index_of_last_space+1:] != "":
118    #     last_tupple = (beginning(sentence[index_of_last_space+1:]),
119    #                  middle(sentence[index_of_last_space+1:]),
120    #                  end(sentence[index_of_last_space+1:]))
121    #     final_list_of_split_words.append(last_tupple)
122    # return final_list_of_split_words
123 
124    #Optimising with the split function
125    final_list_of_split_words = []27    final_list_of_split_words = []
126    words = sentence.split()28    words = sentence.split()
127    for word in words:29    for word in words:
128        current_tuple = (beginning(word),middle(word),end(word))30        current_tuple = (beginning(word),middle(word),end(word))
129        final_list_of_split_words.append(current_tuple)31        final_list_of_split_words.append(current_tuple)
130    return final_list_of_split_words32    return final_list_of_split_words
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op