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

test_colder_warmer   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 47
Duplicated Lines 36.17 %

Importance

Changes 0
Metric Value
eloc 34
dl 17
loc 47
rs 10
c 0
b 0
f 0
wmc 11

1 Method

Rating   Name   Duplication   Size   Complexity  
A Tests.test_Basics() 0 4 2

1 Function

Rating   Name   Duplication   Size   Complexity  
C check_solution() 17 17 9

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
import unittest
2
from math import hypot
3
4
from colder_warmer import checkio
5
6
7
MAX_STEP = 12
8
9
10 View Code Duplication
def check_solution(func, goal, start):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
11
    prev_steps = [start]
12
    for step in range(MAX_STEP):
13
        row, col = func([s[:] for s in prev_steps])
14
        if [row, col] == goal:
15
            return True
16
        if 10 <= row or 0 > row or 10 <= col or 0 > col:
17
            print("You gave wrong coordinates.")
18
            return False
19
        prev_distance = hypot(prev_steps[-1][0] - goal[0], prev_steps[-1][1] - goal[1])
20
        distance = hypot(row - goal[0], col - goal[1])
21
        alteration = (
22
            0 if prev_distance == distance else (1 if prev_distance > distance else -1)
23
        )
24
        prev_steps.append([row, col, alteration])
25
    print("Too many steps")
26
    return False
27
28
29
class Tests(unittest.TestCase):
30
    TESTS = {
31
        "1st": {"input": [[5, 5, 0]], "goal": [7, 7]},
32
        "2nd": {"input": [[0, 0, 0]], "goal": [5, 6]},
33
        "3rd": {"input": [[0, 0, 0]], "goal": [9, 9]},
34
        "4th": {"input": [[7, 7, 0]], "goal": [2, 4]},
35
        "5th": {"input": [[0, 9, 0]], "goal": [9, 0]},
36
    }
37
38
    def test_Basics(self):
39
        for i in self.TESTS:
40
            assert check_solution(
41
                checkio, self.TESTS[i]['goal'], self.TESTS[i]['input'][0]
42
            )
43
44
45
if __name__ == "__main__":  # pragma: no cover
46
    unittest.main()
47