What will this program print?
a = 2 b = 3 def foo(): a = b + 1 a = a + 1 return a def bar(): global b b = a + 3 return b def baz(): return a + b def thud(): a = b + 1 b = a + 1 return a print(foo()) print(bar()) print(baz()) print(thud())
Write a function append() that takes a variable number of arguments. Each argument should be a sequence, such as a list. The function should return a sequence formed by concatenating all of the sequences. For example:
>>> append([1, 2], [5, 6], [8, 9]) [1, 2, 5, 6, 8, 9] >>> append() []
Write a function my_max() that takes one or more arguments. If there is just one argument, it should be a sequence, and my_max() should return the maximum value in the sequence. Otherwise, each argument should be a value, and my_max() should return the maximum of these values. For example:
>>> my_max([2, 4, 6]) 6 >>> my_max(2, 4, 6) 6
Recall that a[:] makes a copy of the
list a. It's the same as a[0:len(a)], or as
a.copy().
What will this program print? Why?
def bar(a):
for i in range(len(a)):
a[i] += 1
a = a[:]
def foo(a):
for b in a + a[:]:
bar(b)
bar(b[:])
m = [[1, 2], [3, 4]]
foo(m)
print(m)Write a class Rectangle representing a rectangle in 2 dimensions. The class should support these operations:
Rectangle(p, q) – make a rectangle with corners at Points p and q
r.area() - return the area of a rectangle
r.perimeter() - return the perimeter of a rectangle
r.contains(p) – return true if point p is inside the rectangle r
r.intersects(s) – return true if rectangle r intersects rectangle s. (Be warned: this is a bit tricky. As a hint, you might first write a helper function that determines whether two line segments intersect.)
Write a class Circle representing a circle in 2 dimensions. The class should support these operations:
Circle(p, r) – make a circle whose center is at p, with radius r
c.area() - return the area of a circle
c.circumference() – return the circumference of a circle
c.contains(p) – return true if point p is inside the circle c
Write a
class Vector that represents a vector in n-dimensional
space. (We partially implemented this class in the lecture.)
Provide an initializer that takes a list of numbers, yielding a vector with those components.
v.add(w) should add two vectors.
v.mul(w) should multiply a scalar times a vector, yielding a new vector.
v.dot(w) method should compute the dot product of two vectors, yielding a scalar.
v.length() should compute the length of a vector.
v.angle(w) should computes the angle between two vectors in radians.
Write a class Date representing a month and day. The class should support these operations:
Date(m, d) – make a Date representing the given month (1 ≤ m ≤ 12) and day (1 ≤ d ≤ 31)
d.next() - return the Date after d
d.prev() - return the Date before d
d.add(n) – add n days to the given Date, wrapping past Dec 31 to Jan 1 if necessary