Skip to content

Instantly share code, notes, and snippets.

@DavidYKay
Created October 7, 2012 04:58
Show Gist options
  • Save DavidYKay/3847155 to your computer and use it in GitHub Desktop.
Save DavidYKay/3847155 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import unittest
import random
class TestSequenceFunctions(unittest.TestCase):
def setUp(self):
self.graph = { 'a': ['c'],
'b': ['a'],
'c': ['e'],
'd': ['a'],
'e': [
'a',
'b',
'c',
'd',
],
}
def test_traverse(self):
traversed = traverse_graph(self.graph, 'a', 'a', 1)
self.assertEqual( [ 'c', 'e', ], traversed, )
traversed = traverse_graph(self.graph, 'a', 'a', 0)
self.assertEqual( [ 'c', ], traversed, )
def traverse_graph(graph, starting_point, current, hops):
destinations = graph[current]
traversed = []
if starting_point != current:
traversed += current
if hops < 0:
return traversed
for destination in destinations:
traversed += traverse_graph(graph, starting_point, destination, hops-1)
return traversed
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment