# Week 12 practice problems, by chaynes@indiana.edu def find(lst, value): """ Return the index of the first occurrence of value in lst, or -1 if value is not in lst. >>> find(['a', 'b', 'c'], 'b') 1 >>> find(['a', 'b', 'c'], 'd') -1 >>> """ #. #. #. #. def dot_product(vector1, vector2): """Return the dot product of the given vectors (represented as equal-length lists of numbers). The dot product is the sum of the corresponding-element products. >>> assert dot_product([1, 2], [3, 4]) == 1*3 + 2*4 >>> """ #. #. #. #. def diagonal(matrix): """Return a list of the diagonal elements of matrix (those elements with equal row and column indices). The matrix must be square (equal length and width). >>> diagonal([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) [1, 5, 9] >>> """ #. #. #. #. def identity_matrix(size): """Return an identity matrix of the given size. That is, a matrix with length and width equal to size and containing all zero values except for ones on the diagonal. >>> identity_matrix(3) [[1, 0, 0], [0, 1, 0], [0, 0, 1]] >>> """ #. #. #. #. #. #. def make_matrix(lst, width): """Return a table, represented as a list of row lists, each of the given width. The elements of the table are taken from the list in row order. (The list length must be a multiple of width.) >>> make_matrix([0, 1, 2, 3, 4, 5], 3) [[0, 1, 2], [3, 4, 5]] >>> """ #. #. #. #. def sum_rows(table): """Return a list whose values are the sums of the rows of table (a list of row lists of numbers. >>> sum_rows([[1, 2, 3], [4, 5, 6]]) [6, 15] >>> """ #. #. #. #. #. #. #. #. def get_column(table, column_index): """Return a list of the values in the column indicated by column_index of the table, which is a list of row lists. >>> get_column([[1, 2, 3], [4, 5, 6]], 1) [2, 5] >>> """ #. #. #. #. #. def sum_columns(table): """Return a list of values that are the sums of the columns of table, a list of rows, each of which is a list of numbers. The table contains at least one value. >>> sum_columns([[1, 2, 3], [4, 5, 6]]) [5, 7, 9] >>> sum_columns([[3]]) [3] >>> """ #. #. #. #. #. #. #. #. #. def flatten_table(table): """Return a list of the values in table (a list of row lists) using row order; that is the values in the first row, followed by those in the second row, and so on. >>> flatten_table([[1, 2, 3], [4, 5, 6]]) [1, 2, 3, 4, 5, 6] >>> """ #. #. #. #. #. def add_column(table, column): """Add column (a list of values) to table (a list of row lists), assuming the length of column is the number of rows in table. >>> t = [[1, 2], [3, 4]] >>> add_column(t, ['a', 'b']) >>> t [[1, 2, 'a'], [3, 4, 'b']] >>> """ #. #. def add_tables(table1, table2): """Return a new table whose values are the sum of the corresponding values in table1 and table2 (assuming the dimentions of table1 and table2 are the same, and non-empty). >>> t1 = [[1, 2], [3, 4]] >>> t2 = [[5, 6], [7, 8]] >>> add_tables(t1, t2) [[6, 8], [10, 12]] >>> """ #. #. #. #. #. #. #. #. #. #. def transpose(table): """Return the transpose of table; that is, a new table in which the values in the i_th row are those in the i_th column of the given table (in the same order of course). The table may be empty. Hint: use get_column. >>> transpose([[1, 2, 3], [4, 5, 6]]) [[1, 4], [2, 5], [3, 6]] >>> """ #. #. #. #. #. #.