Implementation

For reference, the source code is here.

The read_table function reads a text file that contains a square table of integers and converts it into a multidimensional array. The functions get_row_products, get_col_products, get_diagonal_products, and get_antidiagonal_products send the numbers from each row, column, diagonal, and “antidiagonal”1 to the get_products function. The get_products function computes the products of each set of four adjacent numbers and returns the list of products for each row, column, etc. to the calling function. The calling function collects all the products in a list and returns the list. A list of all products is sorted in ascending order, and the last (largest) product is printed.

Functions

read_table(filename)

Given a file that contains a square table of integers, returns the table as a multidimensional array. If the table isn’t square (that is, the number of rows does not equal the number of columns), or if the table contains anything other than integers, the program prints an error message and exits.

Parameters

filename (str) – The name of the file that contains the table.

Returns

The multidimensional array.

Return type

numpy.ndarray

get_products(numbers)

Given a list of numbers, gets the products of every set of four adjacent numbers. Returns the list of products.

Parameters

numbers (list[str]) – A list of numbers.

Returns

The list of products of every set of four adjacent numbers in the list.

Return type

list[str]

get_row_products(table)

Given a multidimensional array, turns each row into a list and gives it to the get_products function. Adds the returned list of products to a list. Returns the complete list of row products.

Parameters

table (numpy.ndarray) – A multidimensional array.

Returns

The list of products from every row of the table.

Return type

list[str]

get_col_products(table)

Given a multidimensional array, turns each column into a list and gives it to the get_products function. Adds the returned list of products to a list. Returns the complete list of column products.

Parameters

table (numpy.ndarray) – A multidimensional array.

Returns

The list of products from every column of the table.

Return type

list[str]

get_diagonal_products(table)

Given a multidimensional array, turns each diagonal (from top left to bottom right) into a list and gives it to the get_products function. Adds the returned list of products to a list. Returns the complete list of diagonal products.

Parameters

table (numpy.ndarray) – A multidimensional array.

Returns

The list of products from every diagonal of the table.

Return type

list[str]

antidiagonal(table, k)

Gets the kth “antidiagonal” for the given table.

Parameters
  • table (numpy.ndarray) – A multidimensional array.

  • k (int) – The index of the antidiagonal band to be extracted (similar to that for NumPy’s diagonal method).

Returns

The list of products from every antidiagonal of the table.

Return type

list[int]

get_antidiagonal_products(table)

Given a multidimensional array, turns each antidiagonal (from bottom left to top right) into a list and gives it to the get_products function. Adds the returned list of products to a list. Returns the complete list of antidiagonal products.

Parameters

table (numpy.ndarray) – A multidimensional array.

Returns

The list of products from every antidiagonal of the table.

Return type

list[str]

Footnotes

1

Here we define the antidiagonal of a table as the band running from bottom left to top right, at a right angle to each of the diagonal bands.