#NEWLINE# def rubik( n ) : #NEWLINE# #INDENT# "Return permutations for an nxn Rubik's cube.\n\n    Permutations returned are for rotation of each of the slice\n    from the face up to the last face for each of the 3 sides (in this order):\n    front, right and bottom. Hence, the first n - 1 permutations are for the\n    slices from the front.\n    " #NEWLINE# if ( n < 2 ) : #NEWLINE# #INDENT# raise ValueError ( 'dimension of cube must be > 1' ) #NEWLINE# #UNINDENT# def getr( f , i ) : #NEWLINE# #INDENT# return faces [ f ] . col ( ( n - i ) ) #NEWLINE# #UNINDENT# def getl( f , i ) : #NEWLINE# #INDENT# return faces [ f ] . col ( ( i - 1 ) ) #NEWLINE# #UNINDENT# def getu( f , i ) : #NEWLINE# #INDENT# return faces [ f ] . row ( ( i - 1 ) ) #NEWLINE# #UNINDENT# def getd( f , i ) : #NEWLINE# #INDENT# return faces [ f ] . row ( ( n - i ) ) #NEWLINE# #UNINDENT# def setr( f , i , s ) : #NEWLINE# #INDENT# faces [ f ] [ : , ( n - i ) ] = Matrix ( n , 1 , s ) #NEWLINE# #UNINDENT# def setl( f , i , s ) : #NEWLINE# #INDENT# faces [ f ] [ : , ( i - 1 ) ] = Matrix ( n , 1 , s ) #NEWLINE# #UNINDENT# def setu( f , i , s ) : #NEWLINE# #INDENT# faces [ f ] [ ( i - 1 ) , : ] = Matrix ( 1 , n , s ) #NEWLINE# #UNINDENT# def setd( f , i , s ) : #NEWLINE# #INDENT# faces [ f ] [ ( n - i ) , : ] = Matrix ( 1 , n , s ) #NEWLINE# #UNINDENT# def cw( F , r = 1 ) : #NEWLINE# #INDENT# for _ in range ( r ) : #NEWLINE# #INDENT# face = faces [ F ] #NEWLINE# rv = [ ] #NEWLINE# for c in range ( n ) : #NEWLINE# #INDENT# for r in range ( ( n - 1 ) , ( - 1 ) , ( - 1 ) ) : #NEWLINE# #INDENT# rv . append ( face [ ( r , c ) ] ) #NEWLINE# #UNINDENT# #UNINDENT# faces [ F ] = Matrix ( n , n , rv ) #NEWLINE# #UNINDENT# #UNINDENT# def ccw( F ) : #NEWLINE# #INDENT# cw ( F , 3 ) #NEWLINE# #UNINDENT# def fcw( i , r = 1 ) : #NEWLINE# #INDENT# for _ in range ( r ) : #NEWLINE# #INDENT# if ( i == 0 ) : #NEWLINE# #INDENT# cw ( F ) #NEWLINE# #UNINDENT# i += 1 #NEWLINE# temp = getr ( L , i ) #NEWLINE# setr ( L , i , list ( getu ( D , i ) ) ) #NEWLINE# setu ( D , i , list ( reversed ( getl ( R , i ) ) ) ) #NEWLINE# setl ( R , i , list ( getd ( U , i ) ) ) #NEWLINE# setd ( U , i , list ( reversed ( temp ) ) ) #NEWLINE# i -= 1 #NEWLINE# #UNINDENT# #UNINDENT# def fccw( i ) : #NEWLINE# #INDENT# fcw ( i , 3 ) #NEWLINE# #UNINDENT# def FCW( r = 1 ) : #NEWLINE# #INDENT# for _ in range ( r ) : #NEWLINE# #INDENT# cw ( F ) #NEWLINE# ccw ( B ) #NEWLINE# cw ( U ) #NEWLINE# t = faces [ U ] #NEWLINE# cw ( L ) #NEWLINE# faces [ U ] = faces [ L ] #NEWLINE# cw ( D ) #NEWLINE# faces [ L ] = faces [ D ] #NEWLINE# cw ( R ) #NEWLINE# faces [ D ] = faces [ R ] #NEWLINE# faces [ R ] = t #NEWLINE# #UNINDENT# #UNINDENT# def FCCW( ) : #NEWLINE# #INDENT# FCW ( 3 ) #NEWLINE# #UNINDENT# def UCW( r = 1 ) : #NEWLINE# #INDENT# for _ in range ( r ) : #NEWLINE# #INDENT# cw ( U ) #NEWLINE# ccw ( D ) #NEWLINE# t = faces [ F ] #NEWLINE# faces [ F ] = faces [ R ] #NEWLINE# faces [ R ] = faces [ B ] #NEWLINE# faces [ B ] = faces [ L ] #NEWLINE# faces [ L ] = t #NEWLINE# #UNINDENT# #UNINDENT# def UCCW( ) : #NEWLINE# #INDENT# UCW ( 3 ) #NEWLINE# #UNINDENT# ( U , F , R , B , L , D ) = names = symbols ( 'U, F, R, B, L, D' ) #NEWLINE# faces = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# count = 0 #NEWLINE# for fi in range ( 6 ) : #NEWLINE# #INDENT# f = [ ] #NEWLINE# for a in range ( ( n ** 2 ) ) : #NEWLINE# #INDENT# f . append ( count ) #NEWLINE# count += 1 #NEWLINE# #UNINDENT# faces [ names [ fi ] ] = Matrix ( n , n , f ) #NEWLINE# #UNINDENT# def perm( show = 0 ) : #NEWLINE# #INDENT# p = [ ] #NEWLINE# for f in names : #NEWLINE# #INDENT# p . extend ( faces [ f ] ) #NEWLINE# #UNINDENT# if show : #NEWLINE# #INDENT# return p #NEWLINE# #UNINDENT# g . append ( Permutation ( p ) ) #NEWLINE# #UNINDENT# g = [ ] #NEWLINE# I = list ( range ( ( 6 * ( n ** 2 ) ) ) ) #NEWLINE# for i in range ( ( n - 1 ) ) : #NEWLINE# #INDENT# fcw ( i ) #NEWLINE# perm ( ) #NEWLINE# fccw ( i ) #NEWLINE# #UNINDENT# assert ( perm ( 1 ) == UCW ) #NEWLINE# UCW ( ) #NEWLINE# for i in range ( ( n - 1 ) ) : #NEWLINE# #INDENT# fcw ( i ) #NEWLINE# UCCW ( ) #NEWLINE# perm ( ) #NEWLINE# UCW ( ) #NEWLINE# fccw ( i ) #NEWLINE# #UNINDENT# UCCW ( ) #NEWLINE# assert ( perm ( 1 ) == I ) #NEWLINE# FCW ( ) #NEWLINE# UCCW ( ) #NEWLINE# FCCW ( ) #NEWLINE# for i in range ( ( n - 1 ) ) : #NEWLINE# #INDENT# fcw ( i ) #NEWLINE# FCW ( ) #NEWLINE# UCW ( ) #NEWLINE# FCCW ( ) #NEWLINE# perm ( ) #NEWLINE# FCW ( ) #NEWLINE# UCCW ( ) #NEWLINE# FCCW ( ) #NEWLINE# fccw ( i ) #NEWLINE# #UNINDENT# FCW ( ) #NEWLINE# UCW ( ) #NEWLINE# FCCW ( ) #NEWLINE# assert ( perm ( 1 ) == I ) #NEWLINE# return g