Open this lab document and keep it open during the lab -- it will be updated live!
def printTable( table:list ) -> None:
""" Given a 2D list (a list of lists), print out the entries,
with each row on a new line. """
# for each row index of the table
for rowIndex in range(len(table)):
# make a string to accumulate our entries into
rowString:str = ""
# it's a list, so walk over each entry
for colIndex in range(len(table[rowIndex])):
rowString += str( table[rowIndex][colIndex] ) + "\t"
# print it!
print( rowString )
def printTables( tables:list ) -> None:
""" Given a 3D list (a list of 2D lists), print each 2D list. """
for table in tables:
printTable(table)
print( "---------------------" )
def enumerateQueens3() -> list:
boards:list = [] # to track all the boards
emptyBoard:list = [[False]*3]*3
# for every placement of a queen in row 0
for i in range( 3 ):
# make a copy of the empty board
board0:list = copyTable( emptyBoard )
# place the queen
board0[0][i] = True
# for every placement of a queen in row 1
for j in range( 3 ):
# make a copy of the board
board1:list = copyTable( board0 )
# place the queen
board1[1][j] = True
# for every placement of a queen in row 2
for k in range( 3 ):
# make a copy of the board
board2:list = copyTable( board1 )
# place the last queen
board2[2][k] = True
# add it to the list of boards
boards.append( board2 )
return boards
def copyTable( table:list ) -> list:
""" Make a copy of a 2D table and return it. """
# use an accumulator pattern to build up the copy row by row
copiedTable:list = [] # start with the empty list
# for each row of the given list
for row in table:
# the copy instance method makes a "shallow" copy
copiedTable.append( row.copy() )
# return the copy
return copiedTable
if __name__ == "__main__":
boards:list = enumerateQueens3()
printTables( boards )