1. 程式人生 > >Keys and Locks 求教

Keys and Locks 求教

Keys and Locks題目 思路:去除純 0 做 3 個旋轉的 copy [90, 180, 270] , 對應關係

	for y in range(row_1):
        for x in range(column_1):
            lock_4[column_1-1-x][y]=lock_3[row_1 -1- y][column_1-1-x]=lock_2[x][row_1-1-y]=lock_1[y][x]
            

問題: 列表的值與我賦值不同 如下:

print(lock_1,'\n', lock_2,'\n', lock_3,'\n', lock_4,'\n',row_1,column_1,'\n')

[['#', '#', '0', '0', '0'], ['#', '#', '#', '#', '#'], ['0', '0', '0', '#', '#']] 
 [['#', '#', '0'], ['#', '#', '0'], ['#', '#', '0'], ['#', '#', '0'], ['#', '#', '0']] 
 [['#', '#', '0', '0', '0'], ['#', '#', '#', '#', '#'], ['0', '0', '0', '#', '#']] 
 [['#', '#', '0'], ['#', '#', '0'], ['#', '#', '0'], ['#', '#', '0'], ['#', '#', '0']] 

賦值過程沒問題 如下:

for y in range(row_1):
    for x in range(column_1):
        lock_2[x][row_1-1-y]=lock_1[y][x]
        print(x, row_1-1-y, lock_2[x][row_1-1-y])

0 2 #
1 2 #
2 2 0
3 2 0
4 2 0
0 1 #
1 1 #
2 1 #
3 1 #
4 1 #
0 0 0
1 0 0
2 0 0
3 0 #
4 0 #        

程式碼:

def keys_and_locks(lock, some_key):

    lock=lock.split()
    some_key=some_key.split()

    #去掉行列 中的純 '0'
    # 去行
    lock_length=len(lock[0])
    ''' for x in range(lock_length):
        if '#'not in lock[x]:
            lock.pop(x)'''
    while '0'* lock_length in lock:
        lock.remove('0'* lock_length)
    # 去列
    lock_1=['']*len(lock[0])
    for x in lock:
        for y in range(len(x)):
            lock_1[y]+=x[y]

    lock_length=len(lock_1[0])
    '''for x in range(lock_length):
        if '#'not in lock_1[x]:
            lock_1.pop(x)'''
    while '0'* lock_length in lock_1:
        lock_1.remove('0'* lock_length)

    row_1=len(lock_1)
    column_1=len(lock_1[0])

    #得到列表
    for x in range(row_1):
        lock_1[x]=list(lock_1[x])

    # 3個旋轉 90, 180, 270 的copy
    lock_2=[[0]*row_1]*column_1
    lock_3=lock_1.copy()
    lock_4=lock_2.copy()

    for y in range(row_1):
        for x in range(column_1):
            lock_4[column_1-1-x][y]=lock_3[row_1 -1- y][column_1-1-x]=lock_2[x][row_1-1-y]=lock_1[y][x]

    for y in range(row_1):
        for x in range(column_1):
            
            lock_4[column_1-1-x][y]=lock_1[y][x]
    for y in range(row_1):
        for x in range(column_1):
            lock_3[row_1 -1- y][column_1-1-x]=lock_1[y][x]
    for y in range(row_1):
        for x in range(column_1):
            lock_2[x][row_1-1-y]=lock_1[y][x]
    
    # 對 key 去掉純 '0'
    # 去行
some_key_length=len(some_key)
'''for x in range(some_key_length):
    if '#'not in some_key[x]:
        some_key.pop(x)'''
while '0'* some_key_length in some_key:
    some_key.remove('0'* some_key_length)

# 去列
some_key_1=['']*len(some_key[0])
for x in some_key:
    for y in range(len(x)):
        some_key_1[y]+=x[y]

some_key_length=len(some_key_1)
'''for x in range(some_key_length):
    if '#'not in some_key_1[x]:
        some_key_1.pop(x)'''
while '0'* some_key_length in some_key_1:
    some_key_1.remove('0'* some_key_length)

row_1=len(some_key_1)
column_1=len(some_key_1[0])

#得到列表
for x in range(row_1):
    some_key_1[x]=list(some_key_1[x])

return some_key_1 in [lock_1, lock_2, lock_3, lock_4]

if __name__ == '__main__':
    print("Example:")
    print(keys_and_locks('''
0##0
0##0
00#0
00##
00##''',
'''
00000
000##
#####
##000
00000'''))

    #These "asserts" using only for self-checking and not necessary for auto-testing
    assert keys_and_locks('''
0##0
0##0
00#0
00##
00##''',
'''
00000
000##
#####
##000
00000''') == True

    assert keys_and_locks('''
###0
00#0''',
'''
00000
00000
#0000
###00
0#000
0#000''') == False

    assert keys_and_locks('''
0##0
0#00
0000''',
'''
##000
#0000
00000
00000
00000''') == True

    assert keys_and_locks('''
###0
0#00
0000''',
'''
##00
##00''') == False

    print("Coding complete? Click 'Check' to earn cool rewards!")