diff --git a/.github/workflows/python-test.yml b/.github/workflows/python-test.yml index 4b531ac..fb2ccd3 100644 --- a/.github/workflows/python-test.yml +++ b/.github/workflows/python-test.yml @@ -30,6 +30,12 @@ jobs: python -m pip install --upgrade pip python -m pip install flake8 pytest python -m pip install -r requirements.txt + - name: Lint with flake8 + run: | + # stop the build if there are Python syntax errors or undefined names + flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics + # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide + flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics - name: Test with pytest run: | python -m pytest tests --timesensitive diff --git a/graph/tsp.py b/graph/tsp.py index 611b600..89ecc4d 100644 --- a/graph/tsp.py +++ b/graph/tsp.py @@ -255,22 +255,21 @@ def reverse_segment_if_better(tour, i, j, k): best = [(a, b) for a, b in zip([d0, d1, d2, d3, d4], ["d0", "d1", "d2", "d3", "d4"])] best.sort() _, index = best[0] - match index: - case "d1": - tour[i:j] = reversed(tour[i:j]) - return -d0 + d1 - case "d2": - tour[j:k] = reversed(tour[j:k]) - return -d0 + d2 - case "d3": - tmp = tour[j:k] + tour[i:j] - tour[i:k] = tmp - return -d0 + d3 - case "d4": - tour[i:k] = reversed(tour[i:k]) - return -d0 + d4 - case _: - return 0 + if index == "d1": + tour[i:j] = reversed(tour[i:j]) + return -d0 + d1 + elif index == "d2": + tour[j:k] = reversed(tour[j:k]) + return -d0 + d2 + elif index == "d3": + tmp = tour[j:k] + tour[i:j] + tour[i:k] = tmp + return -d0 + d3 + elif index == "d4": + tour[i:k] = reversed(tour[i:k]) + return -d0 + d4 + else: + return 0 def all_segments(n: int): """Generate all segments combinations"""