1def beginning(word):
2 word_length = len(word)
3 if word_length % 3 in (0, 1):
4 return word[:word_length // 3]
5 else:
6 return word[:word_length // 3 + 1]
7
8def middle(word):
9 word_length = len(word)
10 if word_length % 3 == 0:
11 return word[word_length // 3 : 2 * (word_length // 3)]
12 elif word_length % 3 == 2:
13 return word[word_length // 3 + 1 : 2 * (word_length // 3) + 1]
14 else:
15 return word[word_length // 3 : 2 * (word_length // 3) + 1]
16
17def end(word):
18 word_length = len(word)
19 if word_length % 3 == 0:
20 return word[2 * (word_length // 3):]
21 else:
22 return word[2 * (word_length // 3) + 1:]
23
24def split_sentence(sentence):
25 splited_sentence = sentence.split()
26 result = []
27 for word in splited_sentence:
28 result.append((beginning(word), middle(word), end(word)))
29 return result
............
----------------------------------------------------------------------
Ran 12 tests in 0.000s
OK
f | 1 | def beginning(word): | f | 1 | def beginning(word): |
2 | word_length = len(word) | 2 | word_length = len(word) | ||
t | 3 | if word_length % 3 == 0 or word_length % 3 == 1: | t | 3 | if word_length % 3 in (0, 1): |
4 | return word[:word_length // 3] | 4 | return word[:word_length // 3] | ||
5 | else: | 5 | else: | ||
6 | return word[:word_length // 3 + 1] | 6 | return word[:word_length // 3 + 1] | ||
7 | 7 | ||||
8 | def middle(word): | 8 | def middle(word): | ||
9 | word_length = len(word) | 9 | word_length = len(word) | ||
10 | if word_length % 3 == 0: | 10 | if word_length % 3 == 0: | ||
11 | return word[word_length // 3 : 2 * (word_length // 3)] | 11 | return word[word_length // 3 : 2 * (word_length // 3)] | ||
12 | elif word_length % 3 == 2: | 12 | elif word_length % 3 == 2: | ||
13 | return word[word_length // 3 + 1 : 2 * (word_length // 3) + 1] | 13 | return word[word_length // 3 + 1 : 2 * (word_length // 3) + 1] | ||
14 | else: | 14 | else: | ||
15 | return word[word_length // 3 : 2 * (word_length // 3) + 1] | 15 | return word[word_length // 3 : 2 * (word_length // 3) + 1] | ||
16 | 16 | ||||
17 | def end(word): | 17 | def end(word): | ||
18 | word_length = len(word) | 18 | word_length = len(word) | ||
19 | if word_length % 3 == 0: | 19 | if word_length % 3 == 0: | ||
20 | return word[2 * (word_length // 3):] | 20 | return word[2 * (word_length // 3):] | ||
21 | else: | 21 | else: | ||
22 | return word[2 * (word_length // 3) + 1:] | 22 | return word[2 * (word_length // 3) + 1:] | ||
23 | 23 | ||||
24 | def split_sentence(sentence): | 24 | def split_sentence(sentence): | ||
25 | splited_sentence = sentence.split() | 25 | splited_sentence = sentence.split() | ||
26 | result = [] | 26 | result = [] | ||
27 | for word in splited_sentence: | 27 | for word in splited_sentence: | ||
28 | result.append((beginning(word), middle(word), end(word))) | 28 | result.append((beginning(word), middle(word), end(word))) | ||
29 | return result | 29 | return result |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | def beginning(word): | f | 1 | def beginning(word): |
n | 2 | if len(word) % 3 == 0: | n | 2 | word_length = len(word) |
3 | if word_length % 3 == 0 or word_length % 3 == 1: | ||||
3 | return word[:len(word) // 3] | 4 | return word[:word_length // 3] | ||
4 | elif len(word) % 3 == 2: | ||||
5 | return word[:len(word) // 3 + 1] | ||||
6 | else: | 5 | else: | ||
n | 7 | return word[:len(word) // 3] | n | 6 | return word[:word_length // 3 + 1] |
8 | 7 | ||||
9 | def middle(word): | 8 | def middle(word): | ||
n | n | 9 | word_length = len(word) | ||
10 | if len(word) % 3 == 0: | 10 | if word_length % 3 == 0: | ||
11 | return word[len(word) // 3 : 2 * (len(word) // 3)] | 11 | return word[word_length // 3 : 2 * (word_length // 3)] | ||
12 | elif len(word) % 3 == 2: | 12 | elif word_length % 3 == 2: | ||
13 | return word[len(word) // 3 + 1 : 2 * (len(word) // 3) + 1] | 13 | return word[word_length // 3 + 1 : 2 * (word_length // 3) + 1] | ||
14 | else: | 14 | else: | ||
n | 15 | return word[len(word) // 3 : 2 * (len(word) // 3) + 1] | n | 15 | return word[word_length // 3 : 2 * (word_length // 3) + 1] |
16 | 16 | ||||
17 | def end(word): | 17 | def end(word): | ||
n | n | 18 | word_length = len(word) | ||
18 | if len(word) % 3 == 0: | 19 | if word_length % 3 == 0: | ||
19 | return word[2 * (len(word) // 3):] | 20 | return word[2 * (word_length // 3):] | ||
20 | elif len(word) % 3 == 2: | ||||
21 | return word[2 * (len(word) // 3) + 1:] | ||||
22 | else: | 21 | else: | ||
n | 23 | return word[2 * (len(word) // 3) + 1:] | n | 22 | return word[2 * (word_length // 3) + 1:] |
24 | 23 | ||||
25 | def split_sentence(sentence): | 24 | def split_sentence(sentence): | ||
26 | splited_sentence = sentence.split() | 25 | splited_sentence = sentence.split() | ||
27 | result = [] | 26 | result = [] | ||
28 | for word in splited_sentence: | 27 | for word in splited_sentence: | ||
29 | result.append((beginning(word), middle(word), end(word))) | 28 | result.append((beginning(word), middle(word), end(word))) | ||
30 | return result | 29 | return result | ||
t | 31 | t | |||
32 | """ | ||||
33 | Question 1: Will it be more efficient and aesthetic to assign variables | ||||
34 | after the definition of the function, like word_length = len(word), or | ||||
35 | this way is ok? | ||||
36 | |||||
37 | Question 2: Do you know if there is a built-in function | ||||
38 | in Python that can track time? For example, how much | ||||
39 | time does this function need to execute? | ||||
40 | |||||
41 | Remark: I don't need you to answer as a comment on the homework, just | ||||
42 | putting it here if I forgot to ask you in class. | ||||
43 | """ |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|