Advertisement
carolsand

yahtze_func_ex

Feb 26th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | None | 0 0
  1. """
  2. Function to generate permutations of outcomes
  3. Repetition of outcomes not allowed
  4. """
  5.  
  6.  
  7. def gen_permutations(outcomes, length):
  8.     """
  9.    Iterative function that generates set of permutations of
  10.    outcomes of length num_trials
  11.    No repeated outcomes allowed
  12.    """
  13.  
  14.     answer_set = set([()])
  15.     old_sequence = []
  16.     for dummy_idx in range(length):
  17.         temp_set = set()
  18.         for partial_sequence in answer_set:
  19.             for item in outcomes:
  20.                 if item in partial_sequence:  ----> added if statement
  21.                     old_sequence.append(item)
  22.                     #print "This is ", old_sequence
  23.                 else:
  24.                     new_sequence = list(partial_sequence)
  25.                     new_sequence.append(item)
  26.                     temp_set.add(tuple(new_sequence))
  27.         answer_set = temp_set
  28.     return answer_set
  29.     # return ans
  30.  
  31.  
  32. def run_example():
  33.     # example for digits
  34.     # outcome = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  35.     # outcome = ["Red", "Green", "Blue"]
  36.     # outcome = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
  37.     outcome = set(["a", "b", "c", "d", "e", "f"])
  38.  
  39.     length = 4
  40.     permtutations = gen_permutations(outcome, length)
  41.     print "Computed", len(permtutations), "permutations of length", str(length)
  42.     print "Permutations were", permtutations
  43.  
  44.  
  45. run_example()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement