import math heights=[0,1,3,6,10,15,21,28,36,45,55,66,78,91,105,120] def build_row(rlst, k, tri): row = [] for parent in rlst: if parent == rlst[0]: lc = parent - k if (lc in rlst) or (lc in row): return [] rc = k else: if row == []: rc = k lc = parent - k else: lc = row[-1] rc = parent - lc if (lc in tri) or (rc in tri) or (lc < 1) or (rc < 1): return [] if (rc in rlst) or (rc in row) or (rc == lc): continue if parent == rlst[0]: row.append(lc) row.append(rc) if len(row) == len(rlst) + 1: return row else: return [] def get_rows(lst,tri): if len(lst) == 1: tri = lst ct = lst[0] if len(lst) == 1: if lst[0] % 2 == 0: ct = int(lst[0] / 2) else: ct = int((lst[0] / 2) + 1) for k in range(ct): if len(lst)+1 == heights.index(len(tri)): a = -1*heights.index(len(tri)) tri = tri[:a] row = build_row(lst, k, tri) if row != []: tri = tri + row get_rows(row,tri) else: if tri not in triangles[tri[0]-1]: if triangles[tri[0]-1] != []: if len(triangles[tri[0]-1][-1]) == 1: if len(lst) > 1: del(triangles[tri[0]-1][-1]) if len(triangles[tri[0]-1]) > 2: if triangles[tri[0]-1][-1][:len(tri)] == tri[:heights.index(len(tri))]: if len(tri[:heights.index(len(tri))]) > 2: del(triangles[tri[0]-1][-1]) if triangles[tri[0]-1][-1] == tri[:len(triangles[tri[0]-1][-1])]: del(triangles[tri[0]-1][-1]) if tri == triangles[tri[0]-1][-1][:len(tri)]: continue triangles[tri[0]-1].append(tri) seq = [] tri = [] triangles = [] for m in range(1,17): triangles.append([]) get_rows([m],tri) seq.append(len(triangles[m-1])) print(seq) for m in range(len(triangles)): for t in triangles[m]: print(t)