|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Auto-fix script for TheAlgorithms/Python PR ruff errors. |
| 4 | +Run from repo root: python fix_ruff_errors.py |
| 5 | +""" |
| 6 | + |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | + |
| 10 | +def fix_floyd_warshall(): |
| 11 | + p = Path("graphs/floyd_warshall.py") |
| 12 | + text = p.read_text() |
| 13 | + text = text.replace( |
| 14 | + 'current = next_node[current][end] # type: ignore', |
| 15 | + 'current = next_node[current][end] # type: ignore[assignment]' |
| 16 | + ) |
| 17 | + p.write_text(text) |
| 18 | + print("fixed graphs/floyd_warshall.py") |
| 19 | + |
| 20 | + |
| 21 | +def fix_ford_fulkerson(): |
| 22 | + p = Path("graphs/ford_fulkerson.py") |
| 23 | + text = p.read_text() |
| 24 | + text = text.replace( |
| 25 | + 'u = parent[s] # type: ignore', |
| 26 | + 'u = parent[s] # type: ignore[assignment]' |
| 27 | + ) |
| 28 | + p.write_text(text) |
| 29 | + print("fixed graphs/ford_fulkerson.py") |
| 30 | + |
| 31 | + |
| 32 | +def fix_heavy_light_decomposition(): |
| 33 | + p = Path("graphs/heavy_light_decomposition.py") |
| 34 | + text = p.read_text() |
| 35 | + old = """ # Same chain now |
| 36 | + l, r = self.pos[u], self.pos[v] |
| 37 | + if l > r: |
| 38 | + l, r = r, l |
| 39 | + segment = self.base_array[l : r + 1]""" |
| 40 | + new = """ # Same chain now |
| 41 | + left_idx, right_idx = self.pos[u], self.pos[v] |
| 42 | + if left_idx > right_idx: |
| 43 | + left_idx, right_idx = right_idx, left_idx |
| 44 | + segment = self.base_array[left_idx : right_idx + 1]""" |
| 45 | + text = text.replace(old, new) |
| 46 | + p.write_text(text) |
| 47 | + print("fixed graphs/heavy_light_decomposition.py") |
| 48 | + |
| 49 | + |
| 50 | +def fix_hopcroft_karp(): |
| 51 | + p = Path("graphs/hopcroft_karp.py") |
| 52 | + text = p.read_text() |
| 53 | + |
| 54 | + text = text.replace( |
| 55 | + 'self.dist[u] = float("inf") # type: ignore', |
| 56 | + 'self.dist[u] = float("inf") # type: ignore[assignment]', |
| 57 | + 1 |
| 58 | + ) |
| 59 | + text = text.replace( |
| 60 | + 'if pair_v is not None and self.dist[pair_v] == float("inf"): # type: ignore', |
| 61 | + 'if pair_v is not None and self.dist[pair_v] == float("inf"): # type: ignore[operator]' |
| 62 | + ) |
| 63 | + text = text.replace( |
| 64 | + 'self.dist[u] = float("inf") # type: ignore', |
| 65 | + 'self.dist[u] = float("inf") # type: ignore[assignment]' |
| 66 | + ) |
| 67 | + |
| 68 | + old = """ if self.pair_u[u] is None: |
| 69 | + if self.dfs(u): |
| 70 | + matching += 1""" |
| 71 | + new = """ if self.pair_u[u] is None and self.dfs(u): |
| 72 | + matching += 1""" |
| 73 | + text = text.replace(old, new) |
| 74 | + |
| 75 | + old = """ print( |
| 76 | + f"Hopcroft-Karp: {n}x{m}, {edges} edges, matching={result}, time={elapsed:.3f}s" |
| 77 | + )""" |
| 78 | + new = """ print( |
| 79 | + f"Hopcroft-Karp: {n}x{m}, {edges} edges, " |
| 80 | + f"matching={result}, time={elapsed:.3f}s" |
| 81 | + )""" |
| 82 | + text = text.replace(old, new) |
| 83 | + |
| 84 | + p.write_text(text) |
| 85 | + print("fixed graphs/hopcroft_karp.py") |
| 86 | + |
| 87 | + |
| 88 | +def fix_max_bipartite_independent_set(): |
| 89 | + p = Path("graphs/max_bipartite_independent_set.py") |
| 90 | + text = p.read_text() |
| 91 | + |
| 92 | + text = text.replace( |
| 93 | + '# Minimum vertex cover = (U - Z) \u222a (V \u2229 Z)', |
| 94 | + '# Minimum vertex cover = (U - Z) union (V intersect Z)' |
| 95 | + ) |
| 96 | + text = text.replace( |
| 97 | + '# Maximum independent set = Z \u222a (V - Z) = complement of min vertex cover', |
| 98 | + '# Maximum independent set = Z union (V - Z) = complement of min vertex cover' |
| 99 | + ) |
| 100 | + text = text.replace( |
| 101 | + 'dist[u] = float("inf") # type: ignore', |
| 102 | + 'dist[u] = float("inf") # type: ignore[assignment]', |
| 103 | + 1 |
| 104 | + ) |
| 105 | + text = text.replace( |
| 106 | + 'if pu is not None and dist[pu] == float("inf"): # type: ignore', |
| 107 | + 'if pu is not None and dist[pu] == float("inf"): # type: ignore[operator]' |
| 108 | + ) |
| 109 | + text = text.replace( |
| 110 | + 'dist[u] = float("inf") # type: ignore', |
| 111 | + 'dist[u] = float("inf") # type: ignore[assignment]' |
| 112 | + ) |
| 113 | + |
| 114 | + p.write_text(text) |
| 115 | + print("fixed graphs/max_bipartite_independent_set.py") |
| 116 | + |
| 117 | + |
| 118 | +def fix_push_relabel(): |
| 119 | + p = Path("graphs/push_relabel.py") |
| 120 | + text = p.read_text() |
| 121 | + |
| 122 | + old = 'v for v in range(n) if v != source and v != sink and self.excess[v] > 0' |
| 123 | + new = 'v for v in range(n) if v not in (source, sink) and self.excess[v] > 0' |
| 124 | + text = text.replace(old, new) |
| 125 | + |
| 126 | + old = """ if ( |
| 127 | + v != source |
| 128 | + and v != sink |
| 129 | + and self.excess[v] == self.excess[u] + 1 |
| 130 | + ):""" |
| 131 | + new = """ if ( |
| 132 | + v not in (source, sink) |
| 133 | + and self.excess[v] == self.excess[u] + 1 |
| 134 | + ):""" |
| 135 | + text = text.replace(old, new) |
| 136 | + |
| 137 | + p.write_text(text) |
| 138 | + print("fixed graphs/push_relabel.py") |
| 139 | + |
| 140 | + |
| 141 | +def fix_test_graph_algorithms(): |
| 142 | + p = Path("graphs/tests/test_graph_algorithms.py") |
| 143 | + text = p.read_text() |
| 144 | + |
| 145 | + text = text.replace( |
| 146 | + 'dist, next_node = floyd_warshall(graph)', |
| 147 | + '_dist, next_node = floyd_warshall(graph)' |
| 148 | + ) |
| 149 | + text = text.replace( |
| 150 | + 'result = hk.max_matching()', |
| 151 | + 'hk.max_matching()' |
| 152 | + ) |
| 153 | + |
| 154 | + p.write_text(text) |
| 155 | + print("fixed graphs/tests/test_graph_algorithms.py") |
| 156 | + |
| 157 | + |
| 158 | +if __name__ == "__main__": |
| 159 | + print("Applying auto-fixes for ruff errors...\n") |
| 160 | + fix_floyd_warshall() |
| 161 | + fix_ford_fulkerson() |
| 162 | + fix_heavy_light_decomposition() |
| 163 | + fix_hopcroft_karp() |
| 164 | + fix_max_bipartite_independent_set() |
| 165 | + fix_push_relabel() |
| 166 | + fix_test_graph_algorithms() |
| 167 | + print("\nAll fixes applied! Run 'ruff check graphs/' to verify.") |
0 commit comments