Skip to content

Commit

Permalink
Python < 3.9 can't handle case statement.
Browse files Browse the repository at this point in the history
  • Loading branch information
root-11 committed Jul 3, 2023
1 parent 1ea7423 commit d276bc1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/python-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
31 changes: 15 additions & 16 deletions graph/tsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down

0 comments on commit d276bc1

Please sign in to comment.