CPSC150A
Scientific Computing

Activity 27

Nested Lists

Create Matrix

Write the function create_matrix(size: int) -> [[int]] 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.

Test Cases

import test

def create_matrix(size: int) -> [[int]]:
    # Put your code here

def main() -> None:
    test.equal(create_matrix(2), [[0, 0], [0, 0]])
    # Put more test cases here
    return None

main()

Sum Column

Write the function sum_column(matrix: [[int]], col_index: int) -> int, 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

import test

def sum_column(matrix: [[int]], col_index: int) -> int:
    # Put your code here

def main() -> None:
    test.equal(sum_column([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 0), 12)
    # Put more test cases here
    return None

main()

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 x 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 a 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: [[int]]) -> bool. 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

import test

def is_magic_square(a_square: [[int]]) -> bool:
    # Put your code here

def main() -> None:
    test.equal(is_magic_square([[8, 1, 6], [3, 5, 7], [4, 9, 2]]), True)
    test.equal(is_magic_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]]), True)
    # Put more test cases here
    return None

main()

Hint

  • You can use the sum_column function that you previously wrote to compute the sum of each column.
  • Write additional functions to compute the sum of a row, the major diagonal (the values from the upper left to the lower right), and the minor diagonal (the values from the upper right to the lower left).
  • The is_magic_square function must compare every row, column, and diagonal to the magic number. But to do this, it must first compute the magic number. Since the sum of all rows, columns, and diagonals must be the same, pick the magic number to be any one of them. For example, pick it to be the sum of the first column.
  • Finally, sum every row and check if it's equal to the magic number. Sum every column, check if it's equal to the magic number. Sum each diagonal, and check if it's equal to the magic number. If any of them are not equal to the magic number, 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.