### Thread: Help With Special Permutations

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2013
Posts
3
Rep Power
0

#### Help With Special Permutations

Basically what I want to do is be able to generate a list of every possible value for a four digit number in which each digit can be any integer from 1-4, but adjacent digits (including the first and last digit) cannot be the same number.

In other terms:

for integers w x y z

w, x, y, z have a range of [1, 4]

w != z or x
x != w or y
y != x or z
z != y or w

But w can be equal to y and x can be equal to z

I already tried to figure out an algorithm for this myself, but to no avail.
2. No Profile Picture
Contributing User
Devshed Novice (500 - 999 posts)

Join Date
May 2009
Posts
521
Rep Power
34
w != z or x
x != w or y ## you already have x != w on the previous line, x != y is on the next line
y != x or z
z != y or w ## z != w is on the first line, z != y is on the previous line

In any case, after you test for "can not be the same" number, it if passes then you want permutations
Code:
```from itertools import permutations
print list(permutations('1234'))```
3. The problem is so small that you can generate all 256 possibilities then delete those you dislike rather than figuring out a clever algorithm to generate only the numbers you need.
Code:
```import itertools
C=list(itertools.product(*itertools.repeat('abcd',4)))
C[:3] == [('a', 'a', 'a', 'a'), ('a', 'a', 'a', 'b'), ('a', 'a', 'a', 'c')]```
and then strain away those items having
item[i]==item[i-1]
for i in range(4)
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2013
Posts
3
Rep Power
0
That makes a lot more sense actually, thanks for pointing out my overcomplication of things. But I'm pretty new to Python, so I can't seem to get this method working. I guess I'm doing something wrong with the way I'm assigning lists, but I'm not sure what it is. Also I know there's probably a more concise way to express my code below

Code:
```import itertools
C=list(itertools.product(*itertools.repeat('1234', 4)))

i=0
flist=[]
for i in range(0,len(C)):
w = C[i][0]
x = C[i][1]
y = C[i][2]
z = C[i][3]
if((w != x) and (x != y) and (y != z) and (z != w)):
flist[i]=C[i]
i+=1

print flist```
Last edited by RosenBridge; December 5th, 2013 at 08:06 PM. Reason: Spelling
5. Code:
```import itertools

# a "one-liner" with list comprehenssion

flist0 = [int(w+x+y+z)for(w,x,y,z)in itertools.product(*itertools.repeat('1234',4))if(w!=x)and(x!=y)and(y!=z)and(z!=w)]

# let's work with your idea.

import itertools

C=list(itertools.product(*itertools.repeat('1234', 4)))

# i=0  # has no useful impact.  Removed.

flist1=[]

for item in C:              # collections iterate over contained objects
(w, x, y, z) = item     # tuple assignment
if (w != x) and (x != y) and (y != z) and (z != w):
flist1.append(int(''.join(item)))   # append method of lists

# comparison.  The same!

assert flist0 == flist1```
Last edited by b49P23TIvg; December 6th, 2013 at 09:46 AM. Reason: generalization
6. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2013
Posts
3
Rep Power
0
At one point I was trying to do the exact same thing as your second example using list.append, I just wasn't quite doing it the right way. Thanks a lot for that. I've still got a long way to go with my Python knowledge it seems.