Домашни > Man who speaks the ends of words > Решения > Решението на Николай Николаев

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

10 точки общо

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

 1"""
 2This is a solution to homework 1 from the Introduction to Python course 
 3at FMI by Nikolay Nikolaev.
 4"""
 5
 6def beginning(word):
 7    """Get the first part of a word."""
 8    beg_ind = int(len(word)/3)
 9    if len(word) % 3 == 2:
10        beg_ind += 1
11    return word[:beg_ind]
12
13def middle(word):
14    """Get the middle part of a word."""
15    start_ind = int(len(word)/3)
16    fin_ind = 2 * start_ind
17    if len(word) % 3 == 1:
18        fin_ind += 1
19    elif len(word) % 3 == 2:
20        start_ind += 1
21        fin_ind = 2*start_ind - 1
22    return word[start_ind:fin_ind]
23
24def end(word):
25    """Get the last part of a word."""
26    length = len(word)
27    end_ind = 2 * int(length/3)
28    if len(word) % 3 == 1:
29        end_ind += 1
30    elif len(word) % 3 == 2:
31        end_ind += 1
32    return word[end_ind:]
33
34def split_sentence(sentence):
35    """
36    Splits the words in a sentence into their three parts and returns 
37    them in a list as tuples.
38    """
39    result = []
40    for word in sentence.split():
41        result.append((beginning(word), middle(word), end(word)))
42    return result

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

OK

Дискусия
История

f1"""f1"""
n2This is a solution to homework 1 from the Introduction to Python course at FMIn2This is a solution to homework 1 from the Introduction to Python course 
3at FMI by Nikolay Nikolaev.
3"""4"""
45
5def beginning(word):6def beginning(word):
n6    """n7    """Get the first part of a word."""
7    function to get the first part of a word
8    """
9    beg_ind = int(len(word)/3)8    beg_ind = int(len(word)/3)
10    if len(word) % 3 == 2:9    if len(word) % 3 == 2:
11        beg_ind += 110        beg_ind += 1
n12    return word[0:beg_ind]n11    return word[:beg_ind]
1312
14def middle(word):13def middle(word):
n15    """n
16    function to get the middle part of a word14    """Get the middle part of a word."""
17    """
18    start_ind = int(len(word)/3)15    start_ind = int(len(word)/3)
n19    fin_ind = 2*start_indn16    fin_ind = 2 * start_ind
20    if len(word) % 3 == 1:17    if len(word) % 3 == 1:
21        fin_ind += 118        fin_ind += 1
22    elif len(word) % 3 == 2:19    elif len(word) % 3 == 2:
23        start_ind += 120        start_ind += 1
24        fin_ind = 2*start_ind - 121        fin_ind = 2*start_ind - 1
25    return word[start_ind:fin_ind]22    return word[start_ind:fin_ind]
2623
27def end(word):24def end(word):
n28    """n25    """Get the last part of a word."""
29    function to get the last part of a word
30    """
31    length = len(word)26    length = len(word)
n32    end_ind = 2*int(length/3)n27    end_ind = 2 * int(length/3)
33    if len(word) % 3 == 1:28    if len(word) % 3 == 1:
34        end_ind += 129        end_ind += 1
35    elif len(word) % 3 == 2:30    elif len(word) % 3 == 2:
36        end_ind += 131        end_ind += 1
n37    return word[end_ind:length]n32    return word[end_ind:]
3833
39def split_sentence(sentence):34def split_sentence(sentence):
40    """35    """
n41    function that splits the words in a sentence into its three parts and saves n36    Splits the words in a sentence into their three parts and returns 
42    them in a list as tuples37    them in a list as tuples.
43    """38    """
44    result = []39    result = []
t45    for word in sentence.split(sep=" "):t40    for word in sentence.split():
46        result.append((beginning(word), middle(word), end(word)))41        result.append((beginning(word), middle(word), end(word)))
47    return result42    return result
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op