Code Duplication    Length = 34-35 lines in 2 locations

checkio/Blizzard/Magic Domino/magic_domino.py 1 location

@@ 72-106 (lines=35) @@
69
    #These "asserts" using only for self-checking and not necessary for auto-testing
70
    import itertools
71
72
    def check_data(size, number, user_result):
73
74
        # check types
75
        check_container_type = lambda o: any(map(lambda t: isinstance(o, t), (list, tuple)))
76
        check_cell_type = lambda i: isinstance(i, int)
77
        if not (check_container_type(user_result) and
78
                all(map(check_container_type, user_result)) and
79
                all(map(lambda row: all(map(check_cell_type, row)), user_result))):
80
            raise Exception("You should return a list/tuple of lists/tuples with integers.")
81
82
        # check sizes
83
        check_size = lambda o: len(o) == size
84
        if not (check_size(user_result) and all(map(check_size, user_result))):
85
            raise Exception("Wrong size of answer.")
86
87
        # check is it a possible numbers (from 0 to 6 inclusive)
88
        if not all(map(lambda x: 0 <= x <= 6, itertools.chain.from_iterable(user_result))):
89
            raise Exception("Wrong matrix integers (can't be domino tiles)")
90
91
        # check is it a magic square
92
        seq_sum_check = lambda seq: sum(seq) == number
93
        diagonals_indexes = zip(*map(lambda i: ((i, i), (i, size - i - 1)), range(size)))
94
        values_from_indexes = lambda inds: itertools.starmap(lambda x, y: user_result[y][x], inds)
95
        if not (all(map(seq_sum_check, user_result)) and  # rows
96
                all(map(seq_sum_check, zip(*user_result))) and  # columns
97
                all(map(seq_sum_check, map(values_from_indexes, diagonals_indexes)))):  # diagonals
98
            raise Exception("It's not a magic square.")
99
100
        # check is it domino square
101
        tiles = set()
102
        for x, y in itertools.product(range(size), range(0, size, 2)):
103
            tile = tuple(sorted((user_result[y][x], user_result[y + 1][x])))
104
            if tile in tiles:
105
                raise Exception("It's not a domino magic square.")
106
            tiles.add(tile)
107
108
    check_data(4, 5, magic_domino(4, 5))
109

checkio/Blizzard/Magic Domino/test_magic_domino.py 1 location

@@ 22-55 (lines=34) @@
19
        for n in random.sample(range(13, 24), self.extra_tests_count):
20
            self.TESTS["Extra"].append({"input": (6, n), "answer": (6, n)})
21
22
    def check_data(self, size, number, user_result):
23
        # check types
24
        check_container_type = lambda o: any(map(lambda t: isinstance(o, t), (list, tuple)))
25
        check_cell_type = lambda i: isinstance(i, int)
26
        if not (check_container_type(user_result) and
27
                all(map(check_container_type, user_result)) and
28
                all(map(lambda row: all(map(check_cell_type, row)), user_result))):
29
            raise Exception("You should return a list/tuple of lists/tuples with integers.")
30
31
        # check sizes
32
        check_size = lambda o: len(o) == size
33
        if not (check_size(user_result) and all(map(check_size, user_result))):
34
            raise Exception("Wrong size of answer.")
35
36
        # check is it a possible numbers (from 0 to 6 inclusive)
37
        if not all(map(lambda x: 0 <= x <= 6, itertools.chain.from_iterable(user_result))):
38
            raise Exception("Wrong matrix integers (can't be domino tiles)")
39
40
        # check is it a magic square
41
        seq_sum_check = lambda seq: sum(seq) == number
42
        diagonals_indexes = zip(*map(lambda i: ((i, i), (i, size - i - 1)), range(size)))
43
        values_from_indexes = lambda inds: itertools.starmap(lambda x, y: user_result[y][x], inds)
44
        if not (all(map(seq_sum_check, user_result)) and  # rows
45
                all(map(seq_sum_check, zip(*user_result))) and  # columns
46
                all(map(seq_sum_check, map(values_from_indexes, diagonals_indexes)))):  # diagonals
47
            raise Exception("It's not a magic square.")
48
49
        # check is it domino square
50
        tiles = set()
51
        for x, y in itertools.product(range(size), range(0, size, 2)):
52
            tile = tuple(sorted((user_result[y][x], user_result[y + 1][x])))
53
            if tile in tiles:
54
                raise Exception("It's not a domino magic square.")
55
            tiles.add(tile)
56
57
    # def test_Basics(self):
58
    #     for i in self.TESTS['Basics']: