@author: aino """ def left_to_choose(hand, chosen): hand = list(hand) chosen = list(chosen) for item in chosen: hand.remove(item) return hand def gen_all_holds(hand): all_holds = set([()]) for dummy_length in range(len(hand)): temp_set = set() for partial_seq in all_holds: for item in left_to_choose(hand, partial_seq): new_seq = list(partial_seq) new_seq.append(item) temp_set.add(tuple(sorted(new_seq))) all_holds.update(temp_set) return all_holds import poc_holds_testsuite poc_holds_testsuite.run_suite(gen_all_holds)
# -*- coding: utf-8 -*- """ Created on Sun Sep 20 16:42:09 2015 @author: aino """ def left_to_choose(hand, chosen): hand = list(hand) chosen = list(chosen) for item in chosen: hand.remove(item) return hand def gen_all_holds(hand): all_holds = set([()]) for dummy_length in range(len(hand)): temp_set = set() for partial_seq in all_holds: for item in left_to_choose(hand, partial_seq): new_seq = list(partial_seq) new_seq.append(item) temp_set.add(tuple(sorted(new_seq))) all_holds.update(temp_set) return all_holds import poc_holds_testsuite poc_holds_testsuite.run_suite(gen_all_holds)
ans = temp return ans def gen_all_holds(hand): """ Generate all possible choices of dice from hand to hold. hand: full yahtzee hand Returns a set of tuples, where each tuple is dice to hold """ ans = set([()]) length = len(hand) if length == 0: return ans if length <= 2: for dummy in range(length + 1): prefix = hand[:dummy] suffix = hand[dummy:] for die in suffix: hold = prefix + (die,) ans.add(hold) else: sub_holds = gen_all_holds(hand[1:]) ans = set([(hand[0],) + hold for hold in sub_holds]) ans = ans.union(sub_holds) return ans import poc_holds_testsuite as test test.run_suite(gen_all_holds)