: , . , :
def bfs(options):
queue = [([c], [*options[:i], *options[i+1:]]) for i,c in enumerate(options)]
while len(queue) > 0:
head, tail = queue[0]
print(head)
queue.extend([([*head, c], [*tail[:i], *tail[i+1:]]) for i,c in enumerate(tail)])
del queue[0]
(64 , ):
>>> bfs(['q','w','e','r'])
['q']
['w']
['e']
['r']
['q', 'w']
['q', 'e']
...
['r', 'w']
['r', 'e']
['q', 'w', 'e']
['q', 'w', 'r']
['q', 'e', 'w']
...
['r', 'q', 'e', 'w']
['r', 'w', 'q', 'e']
['r', 'w', 'e', 'q']
['r', 'e', 'q', 'w']
['r', 'e', 'w', 'q']
,
def bfs(options):
queue = [([c], [*options[:i], *options[i+1:]]) for i,c in enumerate(options)]
for head, tail in queue:
queue.extend([([*head, c], [*tail[:i], *tail[i+1:]]) for i,c in enumerate(tail)])
return [head for head, tail in queue]
.
, :
, . "" :
def bfs(options, level=0):
if level == 0:
for c in options:
print([c])
for i in range(1,len(options)):
bfs(options, i)
else:
for i,c in enumerate(options):
for j,g in enumerate(options[i+1:]):
if i+1+j+level <= len(options):
print([c,*options[i+1+j:i+1+j+level]])
*
Python3, .
:
['q']
['w']
['e']
['r']
['q', 'w']
['q', 'e']
['q', 'r']
['w', 'e']
['w', 'r']
['e', 'r']
['q', 'w', 'e']
['q', 'e', 'r']
['w', 'e', 'r']
['q', 'w', 'e', 'r']
:
def bfs(options, level=0):
for i,c in enumerate(options):
for j,g in enumerate(options[i+1:]):
if i+1+j+level <= len(options):
print([c,*options[i+1+j:i+1+j+level]])
if level == 0:
break
if level < len(options):
bfs(options, level + 1)