mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-09 02:48:02 +00:00
Added three state variable visit_state which will be useful for more advanced challenges such as topological sort.
This commit is contained in:
@@ -1,11 +1,18 @@
|
|||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
from enum import Enum # Python 2 users: Run pip install enum34
|
||||||
|
|
||||||
|
|
||||||
|
class State(Enum):
|
||||||
|
unvisited = 1
|
||||||
|
visited = 2
|
||||||
|
visiting = 3
|
||||||
|
|
||||||
|
|
||||||
class Node:
|
class Node:
|
||||||
|
|
||||||
def __init__(self, id):
|
def __init__(self, id):
|
||||||
self.id = id
|
self.id = id
|
||||||
self.visited = False
|
self.visit_state = State.unvisited
|
||||||
self.adjacent = OrderedDict() # key = node, val = weight
|
self.adjacent = OrderedDict() # key = node, val = weight
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
|||||||
@@ -135,13 +135,20 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"%%writefile graph.py\n",
|
"%%writefile graph.py\n",
|
||||||
"from collections import OrderedDict\n",
|
"from collections import OrderedDict\n",
|
||||||
|
"from enum import Enum # Python 2 users: Run pip install enum34\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"class State(Enum):\n",
|
||||||
|
" unvisited = 1\n",
|
||||||
|
" visited = 2\n",
|
||||||
|
" visiting = 3\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class Node:\n",
|
"class Node:\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def __init__(self, id):\n",
|
" def __init__(self, id):\n",
|
||||||
" self.id = id\n",
|
" self.id = id\n",
|
||||||
" self.visited = False\n",
|
" self.visit_state = State.unvisited\n",
|
||||||
" self.adjacent = OrderedDict() # key = node, val = weight\n",
|
" self.adjacent = OrderedDict() # key = node, val = weight\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def __str__(self):\n",
|
" def __str__(self):\n",
|
||||||
|
|||||||
Reference in New Issue
Block a user