December 5th, 2013, 05:07 PM

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 14, 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.
December 5th, 2013, 06:18 PM

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'))
December 5th, 2013, 06:28 PM

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[i1]
for i in range(4)
[code]
Code tags[/code] are essential for python code and Makefiles!
December 5th, 2013, 08:04 PM

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
December 5th, 2013, 08:42 PM

Code:
import itertools
# a "oneliner" 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
[code]
Code tags[/code] are essential for python code and Makefiles!
December 6th, 2013, 09:12 AM

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.