Last active
July 25, 2017 22:18
-
-
Save back-seat-driver/65f70bd8b876af31d8076d6add1ad92e to your computer and use it in GitHub Desktop.
Permutations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Tree decision method with != operator. | |
''' | |
def unique_variables(LIST): | |
to_iter = len(LIST) | |
total_items = [] | |
for i in range(to_iter): | |
insert = LIST[i] | |
total_items.append(insert) | |
unique_items = [] | |
for i in range(len(total_items)): | |
count = 0 | |
for x in range(len(unique_items)): | |
if total_items[i]==unique_items[x]: | |
count+=1 | |
if count==0: | |
unique_items.append(total_items[i]) | |
return(unique_items) | |
def int_to_str(LIST): | |
to_return=[] | |
for i in range(len(LIST)): | |
to_return.append(str(LIST[i])) | |
return(to_return) | |
def list_expand(LIST): | |
LIST = list(int_to_str(LIST)) | |
to_count = unique_variables(LIST) | |
to_return=[] | |
for i in range(len(to_count)): | |
to_subtract = LIST.count(to_count[i]) | |
for x in range(len(LIST)): | |
if LIST[x]==to_count[i]: | |
LIST[x]=LIST[x]*to_subtract | |
to_subtract-=1 | |
return(LIST) | |
def list_compress(NESTED_LIST): | |
to_return=[] | |
for i in range(len(NESTED_LIST)): | |
insert=[] | |
for x in range(len(NESTED_LIST[i])): | |
insert.append(int(NESTED_LIST[i][x][-1])) | |
to_return.append(insert) | |
return(to_return) | |
def factorial(n): | |
c=1 | |
for i in range(1,n+1): | |
c*=i | |
return(c) | |
def set_start(LIST): | |
to_return=[] | |
for i in range(len(LIST)): | |
insert=[] | |
for x in range(len(LIST)): | |
if LIST[i]!=LIST[x]: | |
insert.append(LIST[x]) | |
to_return.append(insert) | |
return(to_return) | |
def set_builder(NESTED_LIST): | |
to_return=[] | |
for i in range(len(NESTED_LIST)): | |
to_return.append(set_start(NESTED_LIST[i])) | |
return(to_return) | |
def set_chain(NESTED_LIST): | |
to_return=[] | |
for i in range(len(NESTED_LIST)): | |
to_return+=NESTED_LIST[i] | |
return(to_return) | |
def set_expand(SET): | |
to_return=[] | |
for i in range(len(SET)): | |
to_return+=[SET[i]]*factorial(len(SET)-1) | |
return(to_return) | |
def set_rotation(SET): | |
set_0 = range(0,len(SET)-1,2) | |
set_1 = range(1,len(SET),2) | |
to_return=[] | |
for i in range(len(set_0)): | |
to_return+=[SET[set_1[i]],SET[set_0[i]]] | |
return(to_return) | |
def recursive_chain(SET): | |
sub_set_lengths=[] | |
for i in range(len(SET)): | |
sub_set_lengths.append(len(SET[i])) | |
sub_set_lengths = sorted(list(set(sub_set_lengths)),reverse=True) | |
to_return=[] | |
for i in range(len(sub_set_lengths)): | |
insert=[] | |
for x in range(len(SET)): | |
if sub_set_lengths[i]==len(SET[x]): | |
insert+=SET[x] | |
to_return.append(insert) | |
return(to_return) | |
def recursive_return(to_return): | |
to_return = [to_return] | |
initialize = set_start(to_return[-1]) | |
while len(to_return[-1])!=2: | |
to_return+=initialize | |
to_chain = list(set_builder(list(initialize))) | |
to_pass = list(set_chain(list(to_chain))) | |
initialize = list(to_pass) | |
for i in range(len(to_return)): | |
if len(to_return[i])!=2: | |
to_return[i]=set_expand(to_return[i]) | |
to_return = recursive_chain(to_return) | |
to_return+=[set_rotation(to_return[-1])] | |
return(to_return) | |
def PERMUTATIONS(SET): | |
to_return=[] | |
to_iterate = recursive_return(list_expand(SET)) | |
while to_iterate[-1]!=[]: | |
insert=[] | |
for i in range(len(SET)): | |
insert.append(to_iterate[i][0]) | |
to_return.append(insert) | |
for i in range(len(SET)): | |
to_iterate[i].pop(0) | |
to_return = list_compress(to_return) | |
return(to_return) | |
def PERMUTATIONS_LISTS(SET): | |
to_return=[] | |
to_iterate = recursive_return(SET) | |
while to_iterate[-1]!=[]: | |
insert=[] | |
for i in range(len(SET)): | |
insert.append(to_iterate[i][0]) | |
to_return.append(insert) | |
for i in range(len(SET)): | |
to_iterate[i].pop(0) | |
return(to_return) | |
x=PERMUTATIONS([1,2,3,4,5]) | |
a_set=[None,2,3,4,5] | |
to_build=[] | |
index=0 | |
while len(to_build) != factorial(5): | |
insert=[] | |
while len(insert) != factorial(4): | |
insert.append(a_set[index]) | |
to_build+=insert | |
index+=1 | |
''' | |
Source code from itertools, can't run print trace. | |
''' | |
''' | |
def permutations(iterable, r=None): | |
pool = tuple(iterable) | |
n = len(pool) | |
r = n if r is None else r | |
if r > n: | |
return | |
indices = list(range(n)) | |
cycles = list(range(n, n-r, -1)) | |
yield tuple(pool[i] for i in indices[:r]) | |
while n: | |
for i in reversed(range(r)): | |
cycles[i] -= 1 | |
if cycles[i] == 0: | |
indices[i:] = indices[i+1:] + indices[i:i+1] | |
cycles[i] = n - i | |
else: | |
j = cycles[i] | |
indices[i], indices[-j] = indices[-j], indices[i] | |
yield tuple(pool[i] for i in indices[:r]) | |
break | |
else: | |
return | |
''' | |
from PERMUTATIONS_SOURCE import PERMUTATIONS | |
from GREY_CODE import G_C | |
from ITERTOOLS_CHECK import list_sort | |
def PERM(SET,n): | |
if len(SET)==n: | |
return(PERMUTATIONS(SET)) | |
n_bit_grey = G_C(len(SET)) | |
index=[] | |
for i in range(len(n_bit_grey)): | |
if n_bit_grey[i].count(1)==n: | |
index.append(n_bit_grey[i]) | |
to_perm=[] | |
for i in range(len(index)): | |
insert=[] | |
for x in range(len(index[i])): | |
if index[i][x]==1: | |
insert.append(SET[x]) | |
to_perm.append(insert) | |
to_perm = list_sort(to_perm) | |
to_return=[] | |
for i in range(len(to_perm)): | |
to_return+=PERMUTATIONS(to_perm[i]) | |
to_return=list_sort(to_return) | |
return(to_return) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment