def rotate(C, D):
def swap(C, i1, i2, j1, j2, k1, k2):
C[i1:i2], C[j1:j2], C[k1:k2] = C[j1:j2], C[k1:k2], C[i1:i2]
return C
C = list(C)
if D == 'R':
C = swap(C, 0, 9, 27, 36, 45, 54)
C = swap(C, 2, 7, 29, 34, 47, 52)
C = swap(C, 8, 9, 17, 18, 26, 27)
elif D == 'r':
C = swap(C, 8, 9, 17, 18, 26, 27)
C = swap(C, 2, 7, 29, 34, 47, 52)
C = swap(C, 0, 9, 27, 36, 45, 54)
elif D == 'U':
C = swap(C, 0, 27, 9, 18, 36, 45)
C = swap(C, 2, 5, 11, 14, 29, 32)
C = swap(C, 6, 9, 8, 11, 24, 27)
elif D == 'u':
C = swap(C, 6, 9, 8, 11, 24, 27)
C = swap(C, 2, 5, 11, 14, 29, 32)
C = swap(C, 0, 27, 9, 18, 36, 45)
elif D == 'L':
C = swap(C, 0, 9, 9, 18, 18, 27)
C = swap(C, 2, 5, 5, 8, 8, 11)
C = swap(C, 6, 9, 15, 18, 21, 24)
elif D == 'l':
C = swap(C, 6, 9, 15, 18, 21, 24)
C = swap(C, 2, 5, 5, 8, 8, 11)
C = swap(C, 0, 9, 9, 18, 18, 27)
elif D == 'F':
C = swap(C, 18, 27, 45, 54, 36, 45)
C = swap(C, 20, 23, 47, 50, 38, 41)
C = swap(C, 6, 9, 24, 27, 42, 45)
elif D == 'f':
C = swap(C, 6, 9, 24, 27, 42, 45)
C = swap(C, 20, 23, 47, 50, 38, 41)
C = swap(C, 18, 27, 45, 54, 36, 45)
elif D == 'B':
C = swap(C, 0, 9, 9, 18, 18, 27)
C = swap(C, 20, 23, 23, 26, 26, 29)
C = swap(C, 33, 36, 15, 18, 51, 54)
elif D == 'b':
C = swap(C, 33, 36, 15,C, 51, 54)
C = swap(C, 20, 23, 23, 26, 26, 29)
C = swap(C, 0, 9, 9, 18, 18, 27)
return ''.join(C)
还带了一个解释
其中,
swap 函数是用来交换相邻的 6 个面,使得魔方旋转的效果。