Create a function check() in Python 2.7 for a NxN rubik\'s cube that takes in a
ID: 3716212 • Letter: C
Question
Create a function check() in Python 2.7 for a NxN rubik's cube that takes in a list (cube) and checks if the cube is in the following states:
NxNxN cubes:
Spots: an NxNxN cube is in the "spots" state if each face consists of exactly two colors where the (N-2)^2 middle elements are one color and the edge elements are the other color.
Crosses: an NxNxN cube is in the "crosses" state if each face consists of exactly two colors where the corner elements are one color and the remaining elements are the other color.
example of input:
Cube is a list NxNx6 long that consists of numbers 1-6 that represent the colors of the rubik's cube
3X3 cube in 'crosses' state
cube = [1,2,1,2,2,2,1,2,1,
2,3,2,3,3,3,2,3,2,
4,5,4,5,5,5,4,5,4,
5,6,5,6,6,6,5,6,5,
3,1,3,1,1,1,3,1,3,
6,4,6,4,4,4,6,4,6]
3X3 cube in 'spots' state
# cube = [1,1,1,1,2,1,1,1,1,
3,3,3,3,1,3,3,3,3,
4,4,4,4,5,4,4,4,4,
6,6,6,6,4,6,6,6,6,
2,2,2,2,3,2,2,2,2,
5,5,5,5,6,5,5,5,5]
3X3 cube in 'complete' state:
cube = [1,1,1,1,1,1,1,1,1,
2,2,2,2,2,2,2,2,2,
3,3,3,3,3,3,3,3,3,
4,4,4,4,4,4,4,4,4,
5,5,5,5,5,5,5,5,5,
6,6,6,6,6,6,6,6,6]
Explanation / Answer
Here you can find the check function which internally uses three other functions to check for 3 other states
def isComplete(faces):
for face in faces:
val = face[0][0]
for row in face:
for color in row:
if color!=val:
return False
return True
def isSpots(faces):
n = len(faces[0])
for face in faces:
val = face[1][1]
for i in xrange(1,n-1):
for j in xrange(1,n-1):
if face[i][j]!=val:
return False
val = face[0][0]
for i in xrange(n):
if face[0][i]!=val:
return False
if face[n-1][i]!=val:
return False
if face[i][0]!=val:
return False
if face[i][n-1]!=val:
return False
return True
def isCrosses(faces):
n = len(faces[0])
for face in faces:
val = face[0][0]
if val!=face[0][n-1] or val!=face[n-1][0] or val!=face[n-1][n-1]:
return False
val = face[0][1]
for i in xrange(n):
for j in xrange(n):
if (i==0 and (j==0 or j==n-1)) or (i==n-1 and (j==0 or j==n-1)):
continue
if val!=face[i][j]:
return False
return True
def printCube(faces):
for face in faces:
for row in face:
print row
print ""
def check(cube):
n = int(round((len(cube)/6) ** (1./2)))
faces = []
for i in xrange(6):
face = []
j = i*n*n
for k in xrange(n):
temp = cube[j:j+n]
face.append(temp)
j += n
faces.append(face)
# printCube(faces)
if isComplete(faces):
return {'State':'Complete'}
elif isSpots(faces):
return {'State':'Spots'}
elif isCrosses(faces):
return {'State':'Crosses'}
else:
return {'State':'None'}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.