# FUNCIONES def sa(a,b): if len(a) > 1: a[0],a[1] = a[1],a[0] return a,b def sb(a,b): if len(b) > 1: b[0],b[1] = b[1],b[0] return a,b def ss(a,b): sa(a,b) sb(a,b) return a,b def pa(a,b): if len(b) > 0: a.insert(0, b[0]) b.pop(0) return a,b def pb(a,b): if len(a) > 0: b.insert(0, a[0]) a.pop(0) return a,b def ra(a,b): if len(a) > 1: a.append(a.pop(0)) return a,b def rb(a,b): if len(b) > 1: b.append(b.pop(0)) return a,b def rr(a,b): ra(a,b) rb(a,b) return a,b def rra(a,b): if len(a) > 1: a.insert(0, a.pop()) return a,b def rrb(a,b): if len(b) > 1: b.insert(0, b.pop()) return a,b def rrr(a,b): rra(a,b) rrb(a,b) return a,b !pip install openpyxl import openpyxl # importamos la librería openpyxl wb = openpyxl.Workbook() # para crear por primera vez un libro ws = wb.active # estas dos lineas crean el libro, aún sin nombre wb.save('arbol.xlsx') # grabamos el fichero por primera vez # Cuando ya sebemos que queremos trabajar con el archivo arbol.xlsx wb = openpyxl.load_workbook('arbol.xlsx') a_string = "1432" #"531642" a_original = list(int(x) for x in a_string) # pila A en forma de lista ws = wb["Sheet"] # accediendo a la hoja Sheet ws['C5'] = "Origen" ws['D5'] = a_string ws['D4'] = "A" ws['E4'] = "B" fila = 0 catalogo = ["sa","sb","pa","pb","ra","rb","rra","rrb","ss","rr","rrr"] d = {} # diccionario. clave= "324 | 1" valor= "pb ra" for nivel in range(2,5): for cont, instruccion in enumerate(catalogo): print(f"cont {cont}: instrucción {instruccion}\t", end="") a = a_original[:] b = [] print(a,b) if instruccion == "sa": sa(a,b) elif instruccion == "sb": sb(a,b) elif instruccion == "ss": ss(a,b) elif instruccion == "pa": pa(a,b) elif instruccion == "pb": pb(a,b) elif instruccion == "ra": ra(a,b) elif instruccion == "rb": rb(a,b) elif instruccion == "rr": rr(a,b) elif instruccion == "rra": rra(a,b) elif instruccion == "rrb": rrb(a,b) elif instruccion == "rrr": rrr(a,b) print("lista a:", a, "a_original:", a_original) if a != a_original or b != []: ws.cell(row=fila+6, column=3).value = instruccion ws.cell(row=fila+6, column=4).value = ''.join(str(x) for x in a) # pila A ws.cell(row=fila+6, column=5).value = ''.join(str(x) for x in b) # pila B fila += 1 wb.save('arbol.xlsx') wb.close() a = [5, 3, 1, 6, 4, 2] resultado = ''.join(str(x) for x in a) print(type(resultado)) resultado a_string = "531642" list(int(x) for x in a_string) a = [1,6,4,2] b = [3,5] a_string = ','.join(str(x) for x in a) b_string = ','.join(str(x) for x in b) ab_string = a_string + " | " + b_string d = {ab_string : "pb pb"} d palabra = "abcd" for j in range(len(palabra)): print(palabra[j]) for k in range(len(palabra)): print(palabra[j] + palabra[k]) import itertools it = itertools.permutations(['a','b','c','d'],2) li = list(it) print(li) print() for i in range(len(li)): #print("Variación", i+1,":", *li[i]) #print(f"Variación {i+1:2d}: {li[i]}") print(f"Variación {i+1:2d}: ", *li[i]) import itertools def perm(seq, n): for p in itertools.product(seq, repeat=n): print(p) perm(["sa","sb","pa","pb","ra","rb","rra","rrb","ss","rr","rrr"], 3) import itertools d = {} # diccionario. clave= "324 | 1" valor= "pb ra" a_string = "14352" #"531642" a_original = list(int(x) for x in a_string) # pila A en forma de lista b_original = [] def clave(p): a = a_original b = b_original for instruccion in p: if instruccion == "sa": sa(a,b) elif instruccion == "sb": sb(a,b) elif instruccion == "ss": ss(a,b) elif instruccion == "pa": pa(a,b) elif instruccion == "pb": pb(a,b) elif instruccion == "ra": ra(a,b) elif instruccion == "rb": rb(a,b) elif instruccion == "rr": rr(a,b) elif instruccion == "rra": rra(a,b) elif instruccion == "rrb": rrb(a,b) elif instruccion == "rrr": rrr(a,b) if a != None and a != []: if a == [1,2,3,4,5] and len(b)==0: print("Ordenado:", a, b) #else: # print(a, b) def permuta(seq, n): for p in itertools.product(seq, repeat=n): c = clave(p) #print(c) valor = ' '.join(p) #print(valor) catalogo = ["sa","sb","pa","pb","ra","rb","rra","rrb","ss","rr","rrr"] permuta(catalogo, 5)