Errors in the "Python Patterns - Implementing Graphs" essay

Hello there, I would like to report some issues in the "Python Patterns - Implementing Graphs" Python essay, which can be found here: https://www.python.org/doc/essays/graphs/ . The first issue I would like to report is a Py2/Py3 incompatibility issue. The first 3 functions in the essay; find_path(), find_all_paths() and find_shortest_path(); all use the line if not graph.has_key(start) . In these functions, graph is a dictionary. However, the has_keys() method has been removed from Python 3, so these functions do not work in Python 3. To update the functions for Python 3 compatibility, if not graph.has_key(start) should be replaced with if start not in graph . The second issue I would like to report is within the second version of find_shortest_path(), i.e. Eryk Kopczyński's version. Some background first: graph is a dictionary representing nodes connected to each other. The nodes in graph are currently represented by single letter strings, e.g. 'a', 'b', 'c'. etc. I would like to point your attention to the line q = deque(start) . Here, start is a node in the graph. This line is intended to initialise the deque with a starting node. It works perfectly with one-letter character nodes, correctly setting q to deque(['a']) . However, the line does not work with nodes that are strings longer than one character. Let start be the string 'YOU' instead. Instead of q being initialised as deque(['YOU']) , q is initialised as deque(['Y', 'O', 'U']) instead. This is incorrect and breaks the algorithm. To fix this issue, q = deque(start) should be replaced with q = deque([start]) . I hope this information helps you fix the issues in the Python graph essay. Please follow up with me if you have any questions. Thanks, Fadil Nohur
participants (1)
-
Fadil Nohur