Week 10: Exercises

1. Matrix From Input

Write a function read_matrix() that will read a matrix from standard input. Each line will contain one row of the matrix, and the function should read standard input until it ends. For example, if the input is

6 2 4
7 5 3
1 8 9

then the function will return the list [[6, 2, 4], [7, 5, 3], [1, 8, 9]]. Write the function in a single line using a list comprehension.

2. Largest Matrix Value

Write a function largest_val that takes a matrix and returns the largest value in the matrix, along with its coordinates:

>>> largest_val([[1, 2, 3], [4, 5, 10], [6, 7, 8]])
(10, (1, 2))

Write the program in a single line using a list comprehension.

3. Largest Product

Solve Project Euler problem #11 (Largest product in a grid). Read the grid from standard input.