Passed
Push — master ( d0203a...db8646 )
by Ken M.
01:09
created

disposable_teleports   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 76
Duplicated Lines 27.63 %

Importance

Changes 0
Metric Value
eloc 63
dl 21
loc 76
rs 10
c 0
b 0
f 0
wmc 21

3 Functions

Rating   Name   Duplication   Size   Complexity  
B checkio() 0 18 7
B find_path() 0 21 7
B check_solution() 21 21 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
from copy import deepcopy
2
3
4
def find_path(connection_dict, start_point):
5
    if start_point in connection_dict:
6
        if len(connection_dict) == 1:
7
            return connection_dict[start_point]
8
        else:
9
            ret = []
10
            for i in connection_dict[start_point]:
11
                new_connection_dict = deepcopy(connection_dict)
12
                new_connection_dict[start_point].remove(i)
13
                if not new_connection_dict[start_point]:
14
                    del new_connection_dict[start_point]
15
                new_connection_dict[i].remove(start_point)
16
                if not new_connection_dict[i]:
17
                    del new_connection_dict[i]
18
                for j in find_path(new_connection_dict, i):
19
                    ret.append(i + j)
20
                # add this to mean i can stop here even I have path to go
21
                ret.append(i)
22
            return ret
23
    else:
24
        return ['']
25
26
27
def checkio(teleports_string):
28
    connections = teleports_string.split(',')
29
    connection_dict = {}
30
    for i in connections:
31
        if i[0] not in connection_dict:
32
            connection_dict[i[0]] = []
33
        if i[1] not in connection_dict:
34
            connection_dict[i[1]] = []
35
        connection_dict[i[0]].append(i[1])
36
        connection_dict[i[1]].append(i[0])
37
    path = find_path(connection_dict, '1')
38
    path = map(lambda x: '1' + x, path)
39
    path = filter(
40
        lambda x: ('1' == x[-1] and set(x) == set(connection_dict.keys())), path
41
    )
42
    path = sorted(list(path), key=len, reverse=True)
43
    if path:
44
        return path[0]
45
46
47
# This part is using only for self-testing
48
if __name__ == "__main__":
49
50 View Code Duplication
    def check_solution(func, teleports_str):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
51
        route = func(teleports_str)
52
        teleports_map = [
53
            tuple(sorted([int(x), int(y)])) for x, y in teleports_str.split(",")
54
        ]
55
        if route[0] != '1' or route[-1] != '1':
56
            print("The path must start and end at 1")
57
            return False
58
        ch_route = route[0]
59
        for i in range(len(route) - 1):
60
            teleport = tuple(sorted([int(route[i]), int(route[i + 1])]))
61
            if not (teleport in teleports_map):
62
                print("No way from {0} to {1}".format(route[i], route[i + 1]))
63
                return False
64
            teleports_map.remove(teleport)
65
            ch_route += route[i + 1]
66
        for s in range(1, 9):
67
            if not str(s) in ch_route:
68
                print("You forgot about {0}".format(s))
69
                return False
70
        return True
71
72
    assert check_solution(checkio, "12,23,34,45,56,67,78,81"), "First"
73
    assert check_solution(checkio, "12,28,87,71,13,14,34,35,45,46,63,65"), "Second"
74
    assert check_solution(checkio, "12,15,16,23,24,28,83,85,86,87,71,74,56"), "Third"
75
    assert check_solution(checkio, "13,14,23,25,34,35,47,56,58,76,68"), "Fourth"
76