Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Function to generate permutations of outcomes
- Repetition of outcomes not allowed
- """
- def gen_permutations(outcomes, length):
- """
- Iterative function that generates set of permutations of
- outcomes of length num_trials
- No repeated outcomes allowed
- """
- answer_set = set([()])
- old_sequence = []
- for dummy_idx in range(length):
- temp_set = set()
- for partial_sequence in answer_set:
- for item in outcomes:
- if item in partial_sequence: ----> added if statement
- old_sequence.append(item)
- #print "This is ", old_sequence
- else:
- new_sequence = list(partial_sequence)
- new_sequence.append(item)
- temp_set.add(tuple(new_sequence))
- answer_set = temp_set
- return answer_set
- # return ans
- def run_example():
- # example for digits
- # outcome = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- # outcome = ["Red", "Green", "Blue"]
- # outcome = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
- outcome = set(["a", "b", "c", "d", "e", "f"])
- length = 4
- permtutations = gen_permutations(outcome, length)
- print "Computed", len(permtutations), "permutations of length", str(length)
- print "Permutations were", permtutations
- run_example()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement