We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
def update_location(i,j,location):
if i == len(location) - 1 and j == len(location) - 1:
# if i and j == last row and column in location then do nothing
pass
elif j == len(location) - 1: # if j == last index, update next row index 0
if location[i + 1][0][1] == matrix[i][j + 1]: # [i+1][0] == next row index 0
location[i + 1][0][0] = 1
elif location[i + 1][0][1] == matrix[i][-1 - j]:
location[i + 1][0][0] = 2
elif location[i + 1][0][1] == matrix[-1 - i][j + 1]:
location[i + 1][0][0] = 3
elif location[i + 1][0][1] == matrix[-1 - i][-1 - j]:
location[i + 1][0][0] = 4
else: # update next j+1
print('******************')
print('max = '+ str(location[i][j + 1][1]))
print(matrix[i][j + 1],matrix[i][-1 - (j+1)],matrix[-1 - i][j + 1],matrix[-1 - i][-1 - (j+1)])
if location[i][j + 1][1] == matrix[i][j + 1]:
print('update to 1')
# if j exceed len(location), go to next i and start j = 0 again
location[i][j + 1][0] = 1
elif location[i][j + 1][1] == matrix[i][-1 - (j+1)]: #need to use -(j+1)
print('update to 2')
location[i][j + 1][0] = 2
elif location[i][j + 1][1] == matrix[-1 - i][j + 1]:
print('choose to 3')
location[i][j + 1][0] = 3
elif location[i][j + 1][1] == matrix[-1 - i][-1 - (j+1)]:
print('update to 4')
location[i][j + 1][0] = 4
return location
#6x6 wrong at 1,1 and 2,1 26 should be 29 and 20 ->23
#step24 num 20 doing Q4 then update wrong location
# before step 24 doing Q4
#step25 num 26 doing Q4 then update wrong location
# before step 25 doing Q4
#เข้าเงื่อนไข else แต่ max ไม่ตรงกับ ค่าใน matrix เลยดักไม่ได้
# if max in q1 -> no move
# if max in q2 -> cur box down, max box down, max box left , max box up
# if max in q3 -> cur box right, max box right, max box up, max box left
# if max in q4 -> cur box right, max box up, max box left
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Flipping the Matrix
You are viewing a single comment's thread. Return to all comments →