I have the following routine divided into 3 steps:

1)

```
p = dict()
P= Permutations(3)
for i in (1 .. 3):
for u in (1 .. 3):
p[i, u] = [1 if P[m][i-1] == u else 0 for m in (0 .. 5)]
```

2)

```
for i in (1 .. 3):
for u in (1 .. 3):
for j in (1..3):
for v in (1..3):
if i != j and u != v:
p[i,j, u,v] = [1 if P[m][i-1] == u and P[m][j-1] == v else 0 for m in (0 .. 5)]
```

3)

```
A1 = matrix([p[i,j] for i in (1..3) for j in (1..3)])
A2 = matrix([p[i,j,u,v] for i in (1..3) for j in (1..3) for u in (1..3) for v in (1..3)
if i != j and u != v])
C1 = A1.stack(A2)
E1 = A1.right_kernel()
E2 = C.right_kernel()
d1 = E1.dimension()
d2 = E2.dimension()
```

I'm interested in dimensions d1 and d2. If you go to permutation of 4, I will need to adjust all '**for**' to (1..4)
and one more step similar to step 2 to construct one more matrix like

```
A3 = matrix([p[i,j,k, u,v,w] for i in (1..4) for j in (1..4) for u in (1..4) for v in (1..4) for k in (1..4) for w in (1..4) if i != j and i != k and j != k and u != v and u != w and v != w])
```

And also

```
C2 = C1.stack(A3)
E3 = C2.right_kernel()
d3 = E3.dimension()
```

So... I was wondering if there is some "easy" way to put this code in some kind of "generic" program. I mean, I could repeat the whole idea for permutations of 4 or 5 and so on, but it is not very practical. Any suggestions would be great.