Skip to content

Commit 3bb9cfc

Browse files
authored
Add type hints to Dancing Links classes and methods
1 parent 165b4e5 commit 3bb9cfc

1 file changed

Lines changed: 8 additions & 8 deletions

File tree

‎other/dancing_links.py‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@
2121
class DLXNode:
2222
"""Represents a node in the Dancing Links structure."""
2323

24-
def __init__(self):
24+
def __init__(self) -> None:
2525
self.left = self.right = self.up = self.down = self
2626
self.column = None
2727

2828

2929
class ColumnNode(DLXNode):
3030
"""Represents a column header node, keeping track of its column size."""
3131

32-
def __init__(self, name):
32+
def __init__(self, name: str) -> None:
3333
super().__init__()
3434
self.name = name
3535
self.size = 0
@@ -38,7 +38,7 @@ def __init__(self, name):
3838
class DancingLinks:
3939
"""Dancing Links structure for solving the Exact Cover problem."""
4040

41-
def __init__(self, universe, subsets):
41+
def __init__(self, universe: list[int], subsets: list[list[int]]) -> None:
4242
self.header = ColumnNode("header")
4343
self.columns = {}
4444
self.solution = []
@@ -78,7 +78,7 @@ def __init__(self, universe, subsets):
7878
first_node.left.right = node
7979
first_node.left = node
8080

81-
def _cover(self, col):
81+
def _cover(self, col: ColumnNode) -> None:
8282
"""Covers a column (removes it from the matrix)."""
8383
col.right.left = col.left
8484
col.left.right = col.right
@@ -92,7 +92,7 @@ def _cover(self, col):
9292
node = node.right
9393
row = row.down
9494

95-
def _uncover(self, col):
95+
def _uncover(self, col: ColumnNode):
9696
"""Uncovers a column (reverses _cover)."""
9797
row = col.up
9898
while row != col:
@@ -106,7 +106,7 @@ def _uncover(self, col):
106106
col.right.left = col
107107
col.left.right = col
108108

109-
def _choose_column(self):
109+
def _choose_column(self) -> ColumnNode:
110110
"""Select the column with the smallest size (heuristic)."""
111111
min_size = float("inf")
112112
chosen = None
@@ -118,7 +118,7 @@ def _choose_column(self):
118118
col = col.right
119119
return chosen
120120

121-
def _search(self):
121+
def _search(self) -> None:
122122
"""Recursive Algorithm X search."""
123123
if self.header.right == self.header:
124124
# All columns covered -> valid solution
@@ -153,7 +153,7 @@ def _search(self):
153153

154154
self._uncover(col)
155155

156-
def solve(self):
156+
def solve(self) -> list:
157157
"""Find all exact cover solutions."""
158158
self._search()
159159
return self.solutions

0 commit comments

Comments
 (0)