7
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?
Are you sure this is a valid Python 3 code? E.g.
[a,b
should throwSyntaxError
. – vaultah – 2016-04-14T19:14:52.727@vaultah: That's a list comprehension. – El'endia Starman – 2016-04-14T19:19:54.707
@El'endiaStarman: yes. I just said that
[a,b for a, b in ...]
doesn't work. – vaultah – 2016-04-14T19:21:16.433@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 aTypeError
complaining that indices must be slices or integers, not tuples.) – El'endia Starman – 2016-04-14T19:25:42.643Sorry, I pasted the wrong version. Now I updated the question with a fixed and working code and I already removed some more spaces as suggested by @FryAmTheEggman – Byte Commander – 2016-04-14T19:42:19.030
You can define
j=''.join
once and than usej(c[x]...)
. Should save a few bytes. – Jakube – 2016-04-14T20:06:48.033@Jakube Thanks, that saved 8 bytes. 36 more to go. – Byte Commander – 2016-04-14T20:52:49.200
you don't need a space in
) for
– Cyoce – 2016-04-16T19:06:10.0601Also, as a golfing suggestion:
golf
is a 4 byte function name. Tryg
. – Rɪᴋᴇʀ – 2016-04-19T18:24:29.560