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.
This is a way better problem than misery nim. Yes, you can know before hand all magic squares (as you can you know the xor solutions), but, here, you can even deduce all the magic squares or build them all during the interview, which is probably what is expected. However, building them somewhat efficiently instead of creating all 9! and then filtering
Python best solution
If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below:
my solutions
defmagic_squares(s):# Time complexity: O(1)# Space complexity (ignoring input): O(1)# Hard coded or not, both are O(1), since it's maximum of 9! to create all possible# squares, which is a costant (and that is the inefficient way)magic_squares_hard_coded=[[[2,7,6],[9,5,1],[4,3,8]],[[4,9,2],[3,5,7],[8,1,6]],[[8,3,4],[1,5,9],[6,7,2]],[[6,1,8],[7,5,3],[2,9,4]],[[6,7,2],[1,5,9],[8,3,4]],[[2,9,4],[7,5,3],[6,1,8]],[[4,3,8],[9,5,1],[2,7,6]],[[8,1,6],[3,5,7],[4,9,2]],]magic_squares=magic_squares_creating_efficiently()minimum_cost=99forsquareinmagic_squares:cost=0foriinrange(0,3):forjinrange(0,3):cost+=abs(s[i][j]-square[i][j])minimum_cost=min(minimum_cost,cost)returnminimum_costdefmagic_squares_creating_efficiently():# create magic squaresblank_square=[]forrowinrange(0,3):row=[]forcolumninrange(0,3):row.append(None)blank_square.append(row)possible_squares=[]forrowinrange(0,3):forcolumninrange(0,3):blank_square[row][column]=9possible_squares.append([[cellforcellinrow]forrowinblank_square])blank_square[row][column]=Noneold_squares=[]# start by big numbers to filter magic numbers fasterfornumberinrange(8,0,-1):old_squares=possible_squarespossible_squares=[]forpossible_squareinold_squares:forrowinrange(0,3):forcolumninrange(0,3):ifpossible_square[row][column]isNone:# check row validitysum_row=numberforjinrange(0,3):ifpossible_square[row][j]isnotNone:sum_row+=possible_square[row][j]ifsum_row<16:# check column validitysum_column=numberforiinrange(0,3):ifpossible_square[i][column]isnotNone:sum_column+=possible_square[i][column]ifsum_column<16:possible_square[row][column]=numberpossible_squares.append([[cellforcellinrow]forrowinpossible_square])possible_square[row][column]=Nonemagic_squares=[]forsquareinpossible_squares:square_is_magic=True# check sum columnsforcolumninrange(0,3):ifsquare[0][column]+square[1][column]+square[2][column]!=15:square_is_magic=False# check sum rowsforrowinrange(0,3):ifsquare[row][0]+square[row][1]+square[row][2]!=15:square_is_magic=False# check sum diagonalsifsquare[0][0]+square[1][1]+square[2][2]!=15:square_is_magic=Falseifsquare[0][2]+square[1][1]+square[2][0]!=15:square_is_magic=Falseifsquare_is_magic:magic_squares.append(square)returnmagic_squares
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Forming a Magic Square
You are viewing a single comment's thread. Return to all comments →
This is a way better problem than misery nim. Yes, you can know before hand all magic squares (as you can you know the xor solutions), but, here, you can even deduce all the magic squares or build them all during the interview, which is probably what is expected. However, building them somewhat efficiently instead of creating all 9! and then filtering
Python best solution
If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below: my solutions