Given a 2D array that is n
by m
, return the transpose of the matrix.
Return None
if a transpose does not exist
[[5, 4],
[4, 0],
[7, 10].
[-1, 8]]
[[5, 4, 7, -1],
[4, 0, 10, 8]]
[[int]]
: matrix
[[int]]
: Transpose of the list
In [1]:
def transpose_matrix(matrix):
"""
Given a matrix, return the transpose.
"""
pass
In [2]:
def test_transpose_matrix():
assert transpose_matrix([[1,2,3]]) == [[1], [2], [3]]
assert transpose_matrix([[5, 4], [4, 0], [7, 10], [-1, 8]]) == [[5, 4, 7, -1], [4, 0, 10, 8]]
assert transpose_matrix([[1, 3, 5], [2, 4, 6]]) == [[1, 2], [3, 4], [5, 6]]
Create a program to indicate whether or not the column vectors in a matrix are orthogonal, where the column vectors of a matrix are the vectors <a1,a2,a3>
,<b1,b2,b3>
,<c1,c2,c3>
contained in the matrix:
|a1,b1,c1|
|a2,b2,c2|
|a3,b3,c3|
In [3]:
def orthogonal_vectors(matrix):
'''
Compute whether or not all of the column vectors of a matrix are orthogonal to one another
'''
pass
In [4]:
def test_orthogonal_vectors():
assert not orthogonal_vectors([[1,2,3],[-2,1,-4],[-3,4,1]])
assert orthogonal_vectors([[1,2,4],[-1,-1,7],[3,-1,1]])
assert orthogonal_vectors([[0,25,-1],[0,4,10],[0,5,-3]])
Given a complteted Tic Tac Toe game as 2D matrix, return the winner of the game.
Example 1:
Input:
game
: [Output:
O wins!
Example 2:
Input:
game
: [Output:
X wins!
Example 3:
game
: [Draw!
list
: game
str
:
In [5]:
def winner_of_tic_tac_toe(game):
"""
Print winner of tic tac toe game
"""
pass
In [6]:
def test_winner_of_tic_tac_toe():
assert winner_of_tic_tac_toe([['X','O','X'],['O','X','O'],['X','O','X']]) == "X wins!"
assert winner_of_tic_tac_toe([['X','X','O'],['O','X','O'],['X','O','O']]) == "O wins!"
Given a matrix of m x n diemensions (m rows, n columns), return all elements of the matrix in spiral order.
Example 1:
Input:
arr
: [Output:
[1,2,3,6,9,8,7,4,5]
Example 2:
arr
: [ [1,2,3,4,5,6,7,8,9,2,4,9]
list
: arr
list
:
In [7]:
def spiral_matrix(arr):
"""
return matrix in spiral order
"""
pass
In [8]:
def test_spiral_matrix():
assert spiral_matrix([[1, 2, 3, 4],[2, 4, 9, 5],[9, 8, 7, 6]]) == [1, 2, 3, 4, 5, 6, 7, 8, 9, 2, 4, 9]
assert spiral_matrix([[ 1, 2, 3 ],[ 4, 5, 6 ],[ 7, 8, 9 ]]) == [1, 2, 3, 6, 9, 8, 7, 4, 5]
assert spiral_matrix([[1,2,3],[11,12,13],[21,22,23]]) == [1, 2, 3, 13, 23, 22, 21, 11, 12]
Given a list of lists, return a new list with all the elements of the old lists in sequential order
The contents of the lists may be any valid Python object.
The lists themselves will always be a list, and will never be None
.
Example 1:
Input:
arr
: [[1,2],[3,4]]
Output:
[1,2,3,4]
Example 2:
arr
: [[1,'a','bbbbb'],[2],[3,4,5,[3,4]]]
[1,'a','bbbbb',2,3,4,5,[3,4]]
list
: arr
list
:
In [9]:
def flatten_list(arr):
"""return a flattened list"""
pass
In [1]:
def test_flatten_list():
assert flatten_list([[1,2],[3,4]]) == [1,2,3,4]
assert flatten_list([[1,'a','bbbbb'],[2],[3,4,5,[3,4]]]) == [1,'a','bbbbb',2,3,4,5,[3,4]]
assert flatten_list([['a', 'b', 'c'], ['d']]) == ['a', 'b', 'c', 'd']
Given a 2D matrix, return a new matrix of the same dimensions as the old whose contents are the sorted equivalents of the old matrix.
The matrix will be rectangular, i.e. the sublists will all be of the same length. The dimensions of the matrix are strictly positive.
Example:
Input:
arr
: [[5,2],[37,4]]
Output:
[[2,4],[5,37]]
arr
: [[5,4,20], [9,-1,19], [91,-19,1]]
[[-19, -1, 1], [4, 5, 9], [19, 20, 91]]
## Parametersmat
: arr
list
:
In [11]:
def sort_matrix(mat):
"""sort a matrix"""
pass
In [12]:
def test_sort_matrix():
assert sort_matrix([[5,2],[37,4]]) == [[2,4],[5,37]]
assert sort_matrix([[0,1,2],[4,3,5]]) == [[0,1,2],[3,4,5]]
assert sort_matrix([[0,0,0,0,0,14],[-2,8,20,0,0,0]]) == [[-2,0,0,0,0,0],[0,0,0,8,14,20]]
assert sort_matrix([[5,4,20], [9,-1,19], [91,-19,1]]) == [[-19, -1, 1], [4, 5, 9], [19, 20, 91]]
Given a square matrix (i.e. a chess board) containing a character 'R' (the rook) and a character 'P' (the other piece),
return True
if the rook can take the piece, and False
otherwise.
Every input matrix will be square and contain 'R' and 'P' exactly once each.
Example 1:
Input:
arr
: [Output:
True
Example 2:
arr
: [False
list
: board
bool
:
In [13]:
def check_rook(board):
"""
Checks if the rook can take the other piece, given a square matrix and 2 characters within that matrix.
"""
pass
In [14]:
def test_check_rook():
assert check_rook([['', 'P'], ['', 'R']]) is True
assert check_rook([['', 'P'], ['R', '']]) is False
assert check_rook([['', '', 'P'], ['', '', ''], ['', '', 'R']]) is True
Given a 2D matrix, rotate the matrix 90 degrees clockwise, then return the result
The 2D matrix will always input. You do not have the consider the empty 2D Matrix [[]]
Example 1:
Input:
arr
: [Output:
arr
: [Example 2:
arr
: [arr
: [list
: matrix
list
:
In [2]:
def rotate_matrix(matrix):
"""Given a matrix, rotate the matrix"""
pass
In [16]:
def test_rotate_matrix():
assert rotate_matrix([[1, 2, 3], [4, 5, 6]]) == [[4, 1], [5, 2], [6, 3]]
assert rotate_matrix([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]) == [[13, 9, 5, 1], [14, 10, 6, 2], [15, 11, 7, 3], [16, 12, 8, 4]]
Given a 2D matrix, find the difference between the maximum and minimum products that can be formed by moving through the columns of the matrix. Valid moves are down-left, downwards, and down-right.
We will only test for arrays that are 2x3
That is, we will test arrays with similar structure to the examples and nothing else.
Example 1:
Input:
arr
: [Output:
30
Example 2:
arr
: [ 63
[[int]]
: arr
int
:
In [3]:
def product_range(arr):
"""
return difference between maximum and minimum product of a valid path
"""
pass
In [4]:
def test_product_range():
# Tests reserved for students
assert product_range([[1,3,2],[-4,5,6]])==30
assert product_range([[2,3,-4],[4,9,5]])==63
assert product_range([[2,-2,6],[5,-10,1]])==80
In [5]:
def test_all():
# Add your test case here. This should be at the very end
test_transpose_matrix()
test_orthogonal_vectors()
test_winner_of_tic_tac_toe()
test_spiral_matrix()
test_flatten_list()
test_sort_matrix()
test_check_rook()
test_rotate_matrix()
test_product_range()
In [20]:
test_all()