My Python 3 function golf(...) should take a list of lists of lists of strings representing a solid cube and return whether there are any places in which two equal strings are directly next to each other on the x, y or z axis (not diagonally).
If there are no adjacent duplicates, True shall be returned, else False.

The input list and all of its sublists have the same length as they represent a cube, and their length is greater than 1 but lower than or equal to 5. The only possible values for the Strings inside the nested lists are "X" and "Z".
The maximum code length should be less than 200 bytes.

This is an example test case:

golf([[["X", "Z"],
       ["Z", "X"]],
      [["Z", "X"],
       ["X", "Z"]]]) == True
golf([[["X", "Z"],
       ["Z", "X"]],
      [["X", "Z"],
       ["Z", "X"]]]) == False

My current solution has 234 bytes:

def golf(c):
 r=range(len(c));R=[(a,b)for a in r for b in r];j="".join
 return all("XX"not in l and"ZZ"not in l for l in[j(c[x][y][z]for x in r)for y,z in R]+[j(c[x][y][z]for y in r)for x,z in R]+[j(c[x][y][z]for z in r)for x,y in R])

How can I golf this code any further to save another 35 bytes?

  • \$\begingroup\$ Are you sure this is a valid Python 3 code? E.g. [a,b should throw SyntaxError. \$\endgroup\$
    – vaultah
    Commented Apr 14, 2016 at 19:14
  • \$\begingroup\$ @vaultah: That's a list comprehension. \$\endgroup\$ Commented Apr 14, 2016 at 19:19
  • \$\begingroup\$ @El'endiaStarman: yes. I just said that [a,b for a, b in ...] doesn't work. \$\endgroup\$
    – vaultah
    Commented Apr 14, 2016 at 19:21
  • \$\begingroup\$ @vaultah and @FryAmTheEggman, I opened up a Python shell and it looks like you guys are right. Changing a,b to (a,b) fixes that syntax error, but the function still doesn't work (I get a TypeError complaining that indices must be slices or integers, not tuples.) \$\endgroup\$ Commented Apr 14, 2016 at 19:25
  • 1
    \$\begingroup\$ Also, as a golfing suggestion: golf is a 4 byte function name. Try g. \$\endgroup\$
    – Riker
    Commented Apr 19, 2016 at 18:24

3 Answers 3


Python 3, 108 107 bytes

Since the cube only contains the strings "X" and "Z", there are only two valid cube patterns. The one that starts with XZXZX... and the one that starts with ZXZXZ....

My solutions generates these 2 cubes and checks if the inputted cube is one of them.

def golf(l):L=len(l);r=range(L);return l in[[[list("XZ"*L)[(i+j+k)%2:][:L]for j in r]for k in r]for i in r]

i iterates over the possible cubes. Since the dimension is at least 2, we can reuse r instead of writing for i in(0,1).

  • \$\begingroup\$ With length 2, don't the cube patterns start with XZZX or ZXXZ? \$\endgroup\$
    – Sparr
    Commented May 2, 2016 at 7:54
  • \$\begingroup\$ @Sparr Yes. With "The one that starts with XZXZX..." I meant only the first row of the top layer. In the case of length 2 it starts with "XY". \$\endgroup\$
    – Jakube
    Commented May 2, 2016 at 8:11

Python 3, 60 bytes

golf=g=lambda l:all(g(a)*g(b)*(a!=b)for a,b in zip(l,l[1:]))

The function recursively decides whether an N-dimensional array is alternating by checking if:

  • Any two adjacent elements are unequal
  • Each element is alternating

This successfully bottoms out for the strings 'X' and 'Z' because their length is 1, so the all is taken of an empty list.


Python 3 with NumPy, 125 bytes

import numpy
def golf(a):a=numpy.array(a);return(a[:-1]!=a[1:]).all()&(a[:,:-1]!=a[:,1:]).all()&(a[:,:,:-1]!=a[:,:,1:]).all()

Python 3, 146 128 bytes

e=lambda x,y:x!=y
z=lambda f:lambda*l:all(map(f,*l))
u=lambda f,g:lambda a:z(f)(a,a[1:])&z(g)(a)
  • \$\begingroup\$ Great, thank you! The test environment unfortunately did not contain NumPy, but the other solution works like a charm. \$\endgroup\$ Commented Apr 14, 2016 at 21:04
  • \$\begingroup\$ @ByteCommander if this is on your computer, opening terminal/cmd type pip install numpy. Then try again after it finishes. \$\endgroup\$
    – Riker
    Commented Apr 19, 2016 at 14:49
  • \$\begingroup\$ @EasterlyIrk No, it was an online challenge. I have no way to alter the environment. \$\endgroup\$ Commented Apr 19, 2016 at 15:19
  • \$\begingroup\$ @ByteCommander you should get python anyway. \$\endgroup\$
    – Riker
    Commented Apr 19, 2016 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.