| Total Complexity | 12 |
| Total Lines | 68 |
| Duplicated Lines | 29.41 % |
| Changes | 0 | ||
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 itertools import combinations |
||
| 2 | |||
| 3 | |||
| 4 | def GroupDots(values): |
||
| 5 | values = [list(eval(i)) for i in values] |
||
| 6 | Changed = True |
||
| 7 | while Changed: |
||
| 8 | Changed = False |
||
| 9 | for i in combinations(values, 2): |
||
| 10 | if [j for j in i[0] if j in i[1]]: |
||
| 11 | noDups = [] |
||
| 12 | [noDups.append(k) for k in i[0] + i[1] if not noDups.count(k)] |
||
| 13 | values.remove(i[0]) |
||
| 14 | values.remove(i[1]) |
||
| 15 | values.append(noDups) |
||
| 16 | Changed = True |
||
| 17 | break |
||
| 18 | return values |
||
| 19 | |||
| 20 | |||
| 21 | def checkio(cakes): |
||
| 22 | LinesDict = {} |
||
| 23 | for i in combinations(cakes, 2): |
||
| 24 | if i[0][0] == i[1][0]: |
||
| 25 | LinesDict[str(i)] = float('inf') |
||
|
|
|||
| 26 | else: |
||
| 27 | if i[0][0] < i[1][0]: |
||
| 28 | LinesDict[str(i)] = (i[1][1] - i[0][1]) / (i[1][0] - i[0][0]) |
||
| 29 | else: |
||
| 30 | LinesDict[str(i)] = (i[0][1] - i[1][1]) / (i[0][0] - i[1][0]) |
||
| 31 | |||
| 32 | SlopesDict = {} |
||
| 33 | for key, value in sorted(LinesDict.items()): |
||
| 34 | if value not in SlopesDict: |
||
| 35 | SlopesDict[value] = [] |
||
| 36 | SlopesDict[value].append(key) |
||
| 37 | keys_to_del = [i for i in SlopesDict if len(SlopesDict[i]) < 3] |
||
| 38 | for i in keys_to_del: |
||
| 39 | del SlopesDict[i] |
||
| 40 | |||
| 41 | for key, value in SlopesDict.items(): |
||
| 42 | SlopesDict[key] = [i for i in GroupDots(value) if len(i) > 2] |
||
| 43 | return sum([len(SlopesDict[i]) for i in SlopesDict]) |
||
| 44 | |||
| 45 | |||
| 46 | # These "asserts" using only for self-checking and not necessary for |
||
| 47 | # auto-testing |
||
| 48 | View Code Duplication | if __name__ == '__main__': |
|
| 49 | assert checkio([[3, 3], [5, 5], [8, 8], [2, 8], [8, 2]]) == 2 |
||
| 50 | assert ( |
||
| 51 | checkio( |
||
| 52 | [ |
||
| 53 | [2, 2], |
||
| 54 | [2, 5], |
||
| 55 | [2, 8], |
||
| 56 | [5, 2], |
||
| 57 | [7, 2], |
||
| 58 | [8, 2], |
||
| 59 | [9, 2], |
||
| 60 | [4, 5], |
||
| 61 | [4, 8], |
||
| 62 | [7, 5], |
||
| 63 | [5, 8], |
||
| 64 | [9, 8], |
||
| 65 | ] |
||
| 66 | ) |
||
| 67 | == 6 |
||
| 68 | ) |
||
| 69 |