Processing math: 100%

CPSC120A
Fundamentals of Computer Science

Lab 30

Two-dimensional Lists

Create Matrix

Write the function create_matrix(size) that creates a square matrix. The parameter size is a positive integer. The function should return a new two-dimensional list of 0's with size rows and size columns. Note, the returned matrix should not have any aliasing.

Test Cases

matrix = create_matrix(3)
matrix[0][0] = 1
print("Input:   ", 3)
print("Actual:  ", matrix)
print("Expected: [[1, 0, 0], [0, 0, 0], [0, 0, 0]]")
print()
matrix = create_matrix(4)
matrix[3][3] = 1
print("Input:   ", 4)
print("Actual:  ", matrix)
print("Expected: [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 1]]")
print()
    

Sum Column

Write the function sum_column(matrix, col_index), that computes the sum of a column in a matrix. The parameter matrix is a rectangular, two-dimensional list of integers. The parameter col_index is the index of a column in matrix. The function should return the sum of all of the integers in the column col_index in the 2D list matrix.

Test Cases

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print("Input:   ", matrix)
print("Actual:  ", sum_column(matrix, 0))
print("Expected: 12")
print()
matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
print("Input:   ", matrix)
print("Actual:  ", sum_column(matrix, 3))
print("Expected: 24")
print()
matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
print("Input:   ", matrix)
print("Actual:  ", sum_column(matrix, 3))
print("Expected: 40")
print()
    

Magic Square

One interesting application of multidimensional lists are magic squares. Magic squares are a mathematical structure that has been studied for centuries. A magic square is an n×n 2-dimensional list such that the sum of each row, the sum of each column, and the sum of each diagonal are exactly the same. Constructing an magic square is a little bit complicated, but determining if a specified square is magic is not too complicated.

Details

Write the function is_magic_square(a_square). The parameter square is a square, two-dimensional list of integers. The function should return True if square is a magic square, and False otherwise.

Test Cases

square = [[8, 1, 6], [3, 5, 7], [4, 9, 2]]
print("Input:   ", square)
print("Actual:  ", is_magic_square(square))
print("Expected: True")
print()
square = [[8, 1, 10], [3, 5, 7], [4, 9, 2]]
print("Input:   ", square)
print("Actual:  ", is_magic_square(square))
print("Expected: False")
print()
square = [[17, 24, 1, 8, 15], [23, 5, 7, 14, 16], [4, 6, 13, 20, 22], [10, 12, 19, 21, 3], [11, 18, 25, 2, 9]]
print("Input:   ", square)
print("Actual:  ", is_magic_square(square))
print("Expected: True")
print()
    

Hint

  • You can use the built in sum function to compute the sum of a particular row of the square.
  • Write the function sum_column(a_square, column_number), that computes the sum of all of the values in the specified column of the square.
  • Also write the functions sum_major_diagonal(a_square) and sum_minor_diagonal(a_square). That sum the elements of the two diagonals.
  • The check_magic_square function will need to compute the sum of one of the rows in the potentially magic square. Then, sum every row and check if it's equal to the magic sum. Sum every column, check if it's equal to the magic sum. Sum the two diagonals, and check if it's equal to the magic sum. If any of them are not equal to the magic sum, then you don't have a magic square. Use the above functions to make writing this function easier.

Challenge

One additional restriction, for an official magic square, is that every element in the magic square has to be unique. For example, [[1, 1], [1, 1]] is not a magic square. Add an additional check in your program to verify you have true magic squares.

Challenge

There are some relatively straight forward algorithms for generating a square which is guaranteed to be magic. Read the Wikipedia article for magic squares, and write a function called generate_magic_square(size) to generate a magic square of the specified size.