striped_words.checkio()   C
last analyzed

Complexity

Conditions 11

Size

Total Lines 18
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 16
nop 1
dl 0
loc 18
rs 5.4
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like striped_words.checkio() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
VOWELS = "AEIOUY"
2
CONSONANTS = "BCDFGHJKLMNPQRSTVWXZ"
3
4
5
def split_text(text):
6
    result = []
7
    temp_text = ''
8
    for i in text:
9
        if i.isalpha() or i.isdigit():
10
            temp_text += i.upper()
11
        else:
12
            result.append(temp_text)
13
            temp_text = ''
14
    if temp_text:
15
        result.append(temp_text)
16
    return [i for i in result if i]
17
18
19
def checkio(text):
20
    counter = 0
21
    for j in split_text(text):
22
        not_striped = True
23
        if len(j) == 1:
24
            not_striped = False
25
        for i in zip(j, j[1:]):
26
            if (i[0] in VOWELS + CONSONANTS) and (i[1] in VOWELS + CONSONANTS):
27
                if (i[0] in CONSONANTS and i[1] in CONSONANTS) or (
28
                    i[1] in VOWELS and i[0] in VOWELS
29
                ):
30
                    not_striped = False
31
                    break
32
            else:
33
                not_striped = False
34
        if not_striped:
35
            counter += 1
36
    return counter
37
38
39
# These "asserts" using only for self-checking and not necessary for
40
# auto-testing
41
if __name__ == '__main__':
42
    assert checkio(u"My name is ...") == 3, "All words are striped"
43
    assert checkio(u"Hello world") == 0, "No one"
44
    assert checkio(u"A quantity of striped words.") == 1, "Only of"
45
    assert checkio(u"Dog,cat,mouse,bird.Human.") == 3, "Dog, cat and human"
46