Skip to content

Instantly share code, notes, and snippets.

@phobologic
Created May 22, 2013 19:25
Show Gist options
  • Save phobologic/5630193 to your computer and use it in GitHub Desktop.
Save phobologic/5630193 to your computer and use it in GitHub Desktop.
>>> def breadth_first(sequence):
... import copy
... tmp_seq = list(reversed(copy.deepcopy(sequence)))
... while tmp_seq:
... for i in xrange(len(tmp_seq) -1, -1, -1):
... try:
... v = tmp_seq[i].pop(0)
... print v
... except IndexError:
... del(tmp_seq[i])
...
>>>
>>>
>>> x = [[random.randint(0, 100) for i in range(random.randint(1, 5))] for z in range(10)]
>>> x
[[18, 56], [36, 94, 94, 55], [91, 39], [74, 65], [10], [72, 8, 83, 95, 41], [13, 100], [92, 86, 52, 34], [40, 64, 59], [56, 62]]
>>> breadth_first(x)
18
36
91
74
10
72
13
92
40
56
56
94
39
65
8
100
86
64
62
94
83
52
59
55
95
34
41
>>> x
[[18, 56], [36, 94, 94, 55], [91, 39], [74, 65], [10], [72, 8, 83, 95, 41], [13, 100], [92, 86, 52, 34], [40, 64, 59], [56, 62]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment