-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.py
65 lines (52 loc) · 1.95 KB
/
graph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""Sudoku graph."""
# Local imports
from config import COLS, ROWS
from utils import get_match_groups
class Graph():
"""Class to manage Graph Model."""
def __init__(self, values):
"""Create a new graph.
Parameters:
values (list): values of a new graph.
"""
self.nodes = []
for row in range(0, ROWS):
for col in range(0, COLS):
value = int(values[row][col])
node = Node(value, row, col)
self.nodes.append(node) # Create each graph node
for node in self.nodes:
groups = get_match_groups(node)
for group in groups:
for item in group:
row, col = (int(x) for x in item.split())
if node.row == row and node.col == col:
continue # Continue if the item is the node itself
target = self.find_node(row, col)
node.add_adjacent(target) # Add adjacents to graph node
def find_node(self, row, col):
"""Find a node by row and col."""
for node in self.nodes:
if node.row == row and node.col == col:
return node
return None
class Node():
"""Class to manage Node Model."""
def __init__(self, value, row, col):
"""Create a new node.
Parameters:
value (integer): value of a new node.
row (integer): row that new node belongs to.
col (integer): column that new node belongs to.
"""
self.value = value
self.row = row
self.col = col
self.adjacents = []
def add_adjacent(self, node):
"""Add an ajacent to node."""
for adjacent in self.adjacents:
if node == adjacent: # Discards if the node already exists
return
if isinstance(node, Node): # Add only if the node is a Node instance
self.adjacents.append(node)