permutations

# 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)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s