def enumerate_paths(n, k):
"""
John want to go up a flight of stairs that has N steps. He can take
up to K steps each time. This function enumerate all different ways
he can go up this flight of stairs.
"""
paths = []
to_analyze = [(0,)]
while to_analyze:
path = to_analyze.pop()
last_step = path[-1]
if last_step >= n:
# John has reach the top
paths.append(path)
continue
for i in range(1, k + 1):
# possible paths from this point
extended_path = path + (last_step + i,)
to_analyze.append(extended_path)
return paths
输出结果如下
>>> enumerate_paths(3, 2)
[(0, 2, 4), (0, 2, 3), (0, 1, 3), (0, 1, 2, 4), (0, 1, 2, 3)]
你可能会发现结果令人困惑,所以这里有一个解释。例如,
(0, 1, 2, 4)
我试着融入
multiprocessing
import multiprocessing
def enumerate_paths_worker(n, k, queue):
paths = []
while not queue.empty():
path = queue.get()
last_step = path[-1]
if last_step >= n:
# John has reach the top
paths.append(path)
continue
for i in range(1, k + 1):
# possible paths from this point
extended_path = path + (last_step + i,)
queue.put(extended_path)
return paths
def enumerate_paths(n, k):
pool = multiprocessing.Pool()
manager = multiprocessing.Manager()
queue = manager.Queue()
path_init = (0,)
queue.put(path_init)
apply_result = pool.apply_async(enumerate_paths_worker, (n, k, queue))
return apply_result.get()
Python列表
to_analysis
就像一个任务队列,队列中的每一项都可以单独处理,所以我认为这个函数有潜力通过使用多线程/处理进行优化。另外,请注意物品的顺序并不重要。实际上,在优化它时,可以返回Python集、Numpy数组或Pandas数据帧,只要它们表示相同的路径集。
:对于这样的任务,使用科学软件包(如Numpy、Pandas或Scipy)可以获得多少性能?