Artificial intelligent assistant

Listing digits without repetition > In how many ways can we list the digits $$\\{1,1,2,2,3,4,5\\}$$ so that identical values do not appear next to each other? Is it possible to count this directly? Do we need inclusion-exclusion? Without any restrictions, there are $\dfrac{7!}{2!*2!}$ possible lists. Let $A_1$ be a set containing lists where the value $1$ is repeated. Define $A_2$ similarly. $$\dfrac{7!}{2!*2!} - |A_1 \cup A_2|$$ counts what we need. $$|A_1 \cup A_2| = |A_1| + |A_2| - |A_1 \cap A_2|$$ $|A_1| = \dfrac{6!}{2!} = |A_2|$ and $|A_1 \cap A_2| = 5!$ so the answer is $$\dfrac{7!}{2!*2!} - \left(\dfrac{6!}{2!} + \dfrac{6!}{2!} - 5!\right) = 660$$ Is this correct? Is there a faster way?

Yes, it is correct.

Here's a Python code to check that the answer is indeed $660$.


from itertools import permutations
def check_same_neigh(x):
for i in range(len(x)-1):
if x[i] == x[i+1]:
return False
return True
perms = [''.join(p) for p in permutations('1122345') if check_same_neigh(''.join(p))]
print(len(set(perms)))

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy d57c83c54035e96c1cf50fad7e534b1d