# This is a script that generates permutations,
# given a list of outcomes and the size of the sequences.
# A permutation is when we calculate all possible sequences where
# it's not allowed to have a repeated outcome inside each sequence.
# For example if all possible outcomes are 1 and 2 and we want to generates
# all possible sequences, given the sequence size is 2 outcomes, we'll end up
# with two possible permutations: (1, 2) , (2, 1).
# Since (1, 1) and (2, 2) both contain repeated outcome,
# they're not considered permutaions.
# The script is written in Python3.4
def calcperm(arr, size):
result = set([()])
for dummy_idx in range(size):
temp = set()
for dummy_lst in result:
for dummy_outcome in arr:
if dummy_outcome not in dummy_lst:
new_seq = list(dummy_lst)
new_seq.append(dummy_outcome)
temp.add(tuple(new_seq))
result = temp
return result
# Test Cases:
lst = [1, 2, 3, 4]
#lst = ["yellow", "magenta", "white", "blue"]
seq = 2
final = calcperm(lst, seq)
print(len(final))
print(final)

### Like this:

Like Loading...