Write
a class Time
that represents a time of day with 1-second
resolution, e.g. 11:32:07.
Include an
initializer that takes three integers (hours, minutes,
seconds) and returns a Time
. Seconds should default to
0 if not provided.
The
'+' operator should add a number of seconds to a Time
,
yielding a new Time
object (wrapping past midnight if
necessary).
The '-' operator should subtract two Time
objects, yielding a (possibly negative) number of seconds.
A
Time
object's
string representation should look like this: "11:32:07".
Write a class Polynomial representing a polynomial of a single variable. The class should support these operations:
Polynomial(c) – make a Polynomial with the given coefficients. For example, Polynomial(3, 2, 1) represents the polnomial 3x2 + 2x + 1.
p.degree() - return the degree of this Polynomial
The '+' operator should add two Polynomials.
The '*' operator should multiply two Polynomials.
p.eval(x) – return the value of the Polynomial at the given value of x
A Polynomial should have a string representation such as "3x^2 + 2x + 1".
Write a function same_dict(d1, d2) that returns true if two dictionaries have the same key-value pairs. You may not use the == method to compare the dictionaries directly (though you may use == on other types in your solution). For example:
>>> same_dict({ 10 : 20, 30 : 40 }, { 30 : 40, 10 : 20 }) True
If the two dictionaries have a total of N elements, what will be your function's expected big-O running time?
Write a function that takes a list of pairs such
as [(
2
,
3
),
(
4
,
1
),
(
6
,
5
)]
and returns a list in which the pairs have been swapped, e.g.
[(
3
,
2
),
(
1
,
4
),
(
5
,
6
)]
.
Use a list comprehension.
Write a function flatten() that takes a matrix and returns a list containing all the values from all rows:
>>> flatten([[5, 2], [6, 10], [8, 3]]) [5, 2, 6, 10, 8, 3]
Use a list comprehension.
Write a function deep_flatten
that
takes a list that may contain sublists nested to any level. The
function should return a list containing all the values from all the
sublists:
>>> deep_flatten([ [[5, 2], 6], [8, 3], [[[[10]]]] ]) [5, 2, 6, 8, 3, 10]
Write a program that reads any number of lines from standard input, and prints out the sum of all numbers on all lines. For example, if the input is
2 6 4 3 3 2
then the program will print 20. Write the program in a single line using a list comprehension.
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.
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.
Solve Project Euler problem #9 (Special Pythagorean triplet). Use a list comprehension.
Solve Project Euler problem #11 (Largest product in a grid). Read the grid from standard input.