これが問題です。各アイテムにはインデックス値と、それが収まるスロットがあります。
items = ( #(index, [list of possible slots])
(1, ['U', '3']),
(2, ['U', 'L', 'O']),
(3, ['U', '1', 'C']),
(4, ['U', '3', 'C', '1']),
(5, ['U', '3', 'C']),
(6, ['U', '1', 'L']),
)
これらのアイテムが収まるスロットの最大のリストは何ですか? スロットは一度しかありません。
私の解決策は従うのが難しいようで、非常に非pythonic [そして最後の項目で失敗します]。問題を自分で解決する前に、「どちらが良いか」という質問をしたくなかったのです [だから今、私は物乞いの帽子をかぶっていると聞いてください]。これが私のコードです:
def find_available_spot(item, spot_list):
spots_taken = [spot for (i,spot) in spot_list]
i, l = item
for spot in l:
if spot not in spots_taken: return (i, spot)
return None
def make_room(item, spot_list, items, tried=[]):
ORDER = ['U','C','M','O','1','3','2','L']
i, l = item
p_list = sorted(l, key=ORDER.index)
spots_taken = [spot for (i, spot) in spot_list]
for p in p_list:
tried.append(p)
spot_found = find_available_spot((i,[p]),spot_list)
if spot_found: return spot_found
else:
spot_item = items[spots_taken.index(p)]
i, l = spot_item
for s in tried:
if s in l: l.remove(s)
if len(l) == 0: return None
spot_found = find_available_spot((i,l),spot_list)
if spot_found: return spot_found
spot_found = make_room((i,l), spot_list, items, tried)
if spot_found: return spot_found
return None
items = ( #(index, [list of possible slots])
(1, ['U', '3']),
(2, ['U', 'L', 'O']),
(3, ['U', '1', 'C']),
(4, ['U', '3', 'C', '1']),
(5, ['U', '3', 'C']),
(6, ['U', '1', 'L']),
)
spot_list = []
spots_taken = []
for item in items:
spot_found = find_available_spot(item, spot_list)
if spot_found:
spot_list.append(spot_found)
else:
spot_found = make_room(item,spot_list,items)
if spot_found: spot_list.append(spot_found)