Skip to content

Instantly share code, notes, and snippets.

@timothy-allan
Created May 10, 2020 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timothy-allan/226191e630777cc21d31f6c7fedbe90c to your computer and use it in GitHub Desktop.
Save timothy-allan/226191e630777cc21d31f6c7fedbe90c to your computer and use it in GitHub Desktop.
from itertools import permutations
# create a list of numValues pixel lists
numValues = 9
singleResult = [ [1] * i + [0] * (numValues - i) for i in range(1, numValues) ]
print(singleResult)
def generate_permutations(list_to_permute):
permutation_list = list(permutations(list_to_permute)) # 'permutations' is a fuction of iterTools
permutation_list = list(dict.fromkeys(permutation_list)) # use 'dict' trick to remove duplicates
# convert each configuration to rows for Processing (eg - 3 rows, 3 columns)
num_of_rows = 3
split_permutation = []
for config in permutation_list:
temp_config = [ config [i:i + num_of_rows] for i in range(0, len(config), num_of_rows) ]
split_permutation.append(temp_config)
permutation_list = list(split_permutation)
return(permutation_list)
def convert_for_Processing(list_to_convert):
# ************** Convert Python list to Processing Array ***************
permutation_string = str(list_to_convert) # convert to string
# replace [ ] with { } for Processing Array
permutation_string = permutation_string.replace( "[", "{")
permutation_string = permutation_string.replace( "]", "}")
# replace ( ) with { } for Processing Array
permutation_string = permutation_string.replace( "(", "{")
permutation_string = permutation_string.replace( ")", "}")
# add newLine(s) for readability
permutation_string = permutation_string.replace( "}}," , "}},\n")
permutation_string = permutation_string.replace( "}}}," , "}}},\n\n")
return permutation_string
def write_to_file(permutation_string_to_write):
# write to txt file:
f = open ("_txt_files/write_to_file_pixelConfig__multiPermutations.txt", "w")
f.write (permutation_string_to_write)
f.close()
# read from txt file to check:
f = open ("_txt_files/write_to_file_pixelConfig__multiPermutations.txt", "r")
#print(f.read())
return permutation_string_to_write
#-------- MAIN --------#
all_permutations = generate_ALL_permutations(singleResult)
all_permutations = convert_for_Processing(all_permutations)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment