Домашни > Man who speaks the ends of words > Решения > Решението на Костадин Русалов

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

10 точки общо

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

 1def split_in(word, parts=3):
 2    word_len = len(word)
 3    return word_len // parts, word_len % parts
 4
 5
 6def beginning(word):
 7    split, rem = split_in(word)
 8    if rem == 2:
 9        return word[:split + 1]
10    return word[:split]
11
12
13def middle(word):
14    split, rem = split_in(word)
15    if rem == 0:
16        return word[split: 2 * split]
17    elif rem == 1:
18        return word[split: 2 * split + 1]
19    else:
20        return word[split + 1: 2 * split + 1]
21
22
23def end(word):
24    split, rem = split_in(word)
25    if rem == 2:
26        return word[-split - 1:]
27    return word[-split:] if split > 0 else ''
28
29
30def split_word(word):
31    return beginning(word), middle(word), end(word)
32
33
34def split_sentence(sentence):
35    return [split_word(w) for w in sentence.split()]

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

OK

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

n1def quot_rem(num, div):n1def split_in(word, parts=3):
2    return num // div, num % div2    word_len = len(word)
3    return word_len // parts, word_len % parts
34
45
5def beginning(word):6def beginning(word):
n6    sub_len, rem = quot_rem(len(word), 3)n7    split, rem = split_in(word)
7    return word[:sub_len] if rem != 2 else word[:sub_len + 1]8    if rem == 2:
9        return word[:split + 1]
10    return word[:split]
811
912
10def middle(word):13def middle(word):
n11    sub_len, rem = quot_rem(len(word), 3)n14    split, rem = split_in(word)
12    if rem == 0:15    if rem == 0:
n13        return word[sub_len: 2 * sub_len]n16        return word[split: 2 * split]
14    elif rem == 1:17    elif rem == 1:
n15        return word[sub_len: 2 * sub_len + 1]n18        return word[split: 2 * split + 1]
16    else:19    else:
n17        return word[sub_len + 1: 2 * sub_len + 1]n20        return word[split + 1: 2 * split + 1]
1821
1922
20def end(word):23def end(word):
t21    sub_len, rem = quot_rem(len(word), 3)t24    split, rem = split_in(word)
22    return word[-sub_len:] if rem != 2 else word[-sub_len - 1:]25    if rem == 2:
26        return word[-split - 1:]
27    return word[-split:] if split > 0 else ''
2328
2429
25def split_word(word):30def split_word(word):
26    return beginning(word), middle(word), end(word)31    return beginning(word), middle(word), end(word)
2732
2833
29def split_sentence(sentence):34def split_sentence(sentence):
30    return [split_word(w) for w in sentence.split()]35    return [split_word(w) for w in sentence.split()]
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op