Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 71 additions & 57 deletions GenerateMatrixPermutations/GenerateMatrixPermuations.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,71 @@
"""
GenerateMatrixPermuations.py

This script can be used to generate all the different permuations of a 2x2 matrix
with all values within a range of MIN and MAX

Feburary 2013
Alex Aylwin

"""

#flattenMatrix - not used in this script, but this function
#flattens a matrix into an array
def flattenMatrix(matrix):
flatArray = []
size = len(matrix)
for i in range(len):
for j in range(len):
flatArray.append(matrix[i][j])
return flatArray

#souffleArray - this function 'rises' an array into a square matrix,
#dimensions size x size
def souffleArray(array, size):
matrix = []
for i in range(size):
row = []
for j in range(size):
row.append(array[(i*size)+j])
matrix.append(row)
return matrix

#fill 4 - this function is the controller function, that creates the
#matricies and calls an operator function on each of them. This other
#function should have side effects that capture the results of whatever
#calculation needs to be performed

#To change the size of the matrix (ie, not 2x2) add for loops to match
#the length of the ARRAY that would come out of flattening the matrix,
#ie a 4x4 matrix should have 16 nested for loops.
def fill4(min, max):
array = []
size = 2
for n1 in range(min, max+1):
for n2 in range(min, max+1):
for n3 in range(min, max+1):
for n4 in range(min, max+1):
array = [n1,n2,n3,n4]
matrix = souffleArray(array, size)
doFunctionOnMatrix(matrix)

#This is the calculation function that actually does some work on the matricies
def doFunctionOnMatrix(matrix):
print(matrix)

#Kick off the calculation, with a range from 0 - 3, inclusive
fill4(0,3)

# Python function to print permutations of a given list

def permutation(lst):



# If lst is empty then there are no permutations

if len(lst) == 0:

return []



# If there is only one element in lst then, only

# one permuatation is possible

if len(lst) == 1:

return [lst]



# Find the permutations for lst if there are

# more than 1 characters



l = [] # empty list that will store current permutation



# Iterate the input(lst) and calculate the permutation

for i in range(len(lst)):

m = lst[i]



# Extract lst[i] or m from the list. remLst is

# remaining list

remLst = lst[:i] + lst[i+1:]



# Generating all permutations where m is first

# element

for p in permutation(remLst):

l.append([m] + p)

return l




# Driver program to test above function

data = list('123')

for p in permutation(data):

print p