safe_coasts.finish_map()   F
last analyzed

Complexity

Conditions 14

Size

Total Lines 46
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 35
nop 1
dl 0
loc 46
rs 3.6
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like safe_coasts.finish_map() 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
def finish_map(regional_map):
2
    # extend original map
3
    extended_map = ['S' + i + 'S' for i in regional_map]
4
    extended_map = (
5
        ['S' * len(extended_map[0])] + extended_map + ['S' * len(extended_map[0])]
6
    )
7
8
    # mark squares
9
    changed = True
10
    while changed:
11
        changed = False
12
        for row in range(1, len(extended_map) - 1):
13
            for col in range(1, len(extended_map[0]) - 1):
14
                if extended_map[row][col] == '.':
15
                    surronded_squares = (
16
                        extended_map[row - 1][col - 1 : col + 2]
17
                        + extended_map[row][col - 1 : col + 2]
18
                        + extended_map[row + 1][col - 1 : col + 2]
19
                    )
20
                    if 'X' not in surronded_squares:
21
                        if (
22
                            'D' in surronded_squares[1]
23
                            or 'D' in surronded_squares[3]
24
                            or 'D' in surronded_squares[5]
25
                            or 'D' in surronded_squares[7]
26
                        ):
27
                            extended_map[row] = (
28
                                extended_map[row][:col]
29
                                + 'D'
30
                                + extended_map[row][col + 1 :]
31
                            )
32
                            changed = True
33
                            break
34
            if changed:
35
                break
36
37
    # fill left . to S
38
    for row in range(len(extended_map)):
39
        for col in range(len(extended_map[0])):
40
            if extended_map[row][col] == '.':
41
                extended_map[row] = (
42
                    extended_map[row][:col] + 'S' + extended_map[row][col + 1 :]
43
                )
44
45
    extended_map = extended_map[1:-1]
46
    return [i[1:-1] for i in extended_map]
47
48
49
if __name__ == '__main__':
50
    # These "asserts" using only for self-checking and not necessary for
51
    # auto-testing
52
    assert isinstance(finish_map(("D..", "...", "...")), (list, tuple)), "List or tuple"
53
    assert list(
54
        finish_map(
55
            (
56
                "D..XX.....",
57
                "...X......",
58
                ".......X..",
59
                ".......X..",
60
                "...X...X..",
61
                "...XXXXX..",
62
                "X.........",
63
                "..X.......",
64
                "..........",
65
                "D...X....D",
66
            )
67
        )
68
    ) == [
69
        "DDSXXSDDDD",
70
        "DDSXSSSSSD",
71
        "DDSSSSSXSD",
72
        "DDSSSSSXSD",
73
        "DDSXSSSXSD",
74
        "SSSXXXXXSD",
75
        "XSSSSSSSSD",
76
        "SSXSDDDDDD",
77
        "DSSSSSDDDD",
78
        "DDDSXSDDDD",
79
    ], "Example"
80
81
    assert list(
82
        finish_map(
83
            (
84
                "........",
85
                "........",
86
                "X.X..X.X",
87
                "........",
88
                "...D....",
89
                "........",
90
                "X.X..X.X",
91
                "........",
92
                "........",
93
            )
94
        )
95
    ) == [
96
        "SSSSSSSS",
97
        "SSSSSSSS",
98
        "XSXSSXSX",
99
        "SSSSSSSS",
100
        "DDDDDDDD",
101
        "SSSSSSSS",
102
        'XSXSSXSX',
103
        "SSSSSSSS",
104
        "SSSSSSSS",
105
    ], "Walls"
106