Quiz 7a#
Question 1#
Let’s define a class for 2-dimensional points.
class Point2D:
def __init__(self, x = 0, y = 0):
self.x = x
self.y = y
def translate(self, deltax = 0, deltay = 0):
"""Translate the point in the x direction by deltax
and in the y direction by deltay."""
self.x += deltax
self.y += deltay
#…
Which of the following code snippets are valid usages of the
Point2D
initializer and its
translate
method? For your first attempt at this
problem, we suggest that you try to answer without using CodeSkulptor.
Point2D = (3, 9)
Point2D.translate(5, -2)
\
Point2D(3, 9)
Point2D.translate(5, -2)
\
point = Point2D(3, 9)
point.translate(5, -2)
\
point1 = Point2D(3, 9)
point2 = Point2D()
point2.translate(20, 4)
point = Point2D(3, 9)
point.translate(5, -2)
point1 = Point2D(3, 9)
point2 = Point2D()
point2.translate(20, 4)
Question 2#
Let’s continue to use the same class for 2-dimensional points.
class Point2D:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def translate(self, deltax=0, deltay=0):
"""Translate the point in the x direction by deltax
and in the y direction by deltay."""
self.x += deltax
self.y += deltay
#…
Which of the following code snippets are valid usages of the
Point2D
initializer and its
translate
method? For your first attempt at this
problem, we suggest that you try to answer without using CodeSkulptor.
points = [(2, 5), (8, 3), (0, 2)]
for point in points:
point.translate(-1, -1)
\
point0 = Point2D(2, 5)
point1 = Point2D(8, 3)
point2 = Point2D(0, 2)
points = [point0, point1, point2]
points.translate(-1, -1)
\
point0 = Point2D(2, 5)
point1 = Point2D(8, 3)
point2 = Point2D(0, 2)
points = [point0, point1, point2]
for point in points:
point.translate(-1, -1)
point0 = Point2D(2, 5)
point1 = Point2D(8, 3)
point2 = Point2D(0, 2)
points = [point0, point1, point2]
for point in points:
point.translate(-1, -1)
Question 3#
Now, let’s make this class definition for 2-dimensional points more specific.
class Point2D:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def translate(self, deltax=0, deltay=0):
"""Translate the point in the x direction by deltas
and in the y direction by deltay."""
self.x += deltax
self.y += deltay
def __str__(self):
return "<" + str(self.x) + ", " + str(self.y) + ">"
For the Point2D
class above, having only the
three methods shown, which of the following code snippets are valid
usages of the Point2D
class? For your first
attempt at this problem, we suggest that you try to answer without using
CodeSkulptor.
point = Point2D(3, 6)
tup = tuple(point)
\
point = Point2D(3, 6)
lst = list(point)
\
point = Point2D(3, 6)
s = str(point)
\
point = Point2D(3, 6)
s = str(point)
newpoint = Point(s)
point = Point2D(3, 6)
s = str(point)
Question 4#
In SimpleGUI, the function draw_image
takes an
optional sixth parameter that determines the angle of rotation of the
destination rectangle around its center. Do positive values for the
angle rotate the image clockwise or counterclockwise? Is the angle
specified in degrees or radians?
Refer to the CodeSkulptor documentation .
clockwise, radians
counterclockwise, degrees
clockwise, degrees
counterclockwise, radians \
clockwise, radians \
Question 5#
One interesting extension of Rice Rocks would be to have two ships, with each controlled by a separate player, instead of one single ship. Using the provided class definitions, what is the best way to represent the two ships in this new variant?
In the Ship
class definition, change the
variables pos
, vel
,
angle
to be lists of two values each. Then,
change each method to take an additional number argument that indicates
which ship should be used. Thus, when we call the
constructor now, we are creating both ships.
ships = Ship(#…)
Copy the Ship
class code, e.g.,
class Another_Ship:
def __init__(self, pos, vel, angle):
#…
#…
Then create two ship objects, one of each class, assigning each to a global variable.
ship1 = Ship(#…)
ship2 = Another_Ship(#…)
Add another call to the Ship
constructor,
assigning the result to another global variable.
ship1 = Ship(#…)
ship2 = Ship(#…)
In the Ship
class definition, duplicate
every method. For example, Ship.draw1(#…)
would be used to draw the first ship, while
Ship.draw2(#…)
would be used to draw the second
ship. \
Add another call to the Ship
constructor,
assigning the result to another global variable.
ship1 = Ship(#…)
ship2 = Ship(#…)
Question 6#
Which of the following browsers fully support MP3 audio files? Refer to the CodeSkulptor documentation .
Chrome
Safari
Firefox \
According to developer.mozilla.org all of them.
Question 7#
Consider a spaceship where the ship’s thrusters can accelerate the ship by 10 pixels per second for each second that the thrust key is held down. If the friction induces a deceleration that is 10% of the ship’s velocity per second, what is the maximal velocity of the ship? If you are having trouble, consider writing a short program to help understand this problem.
The ship has no maximal velocity. It can reach any velocity the player
desires if you hold the thrust key down long enough.
Around 10 pixels per second
Around 1000 pixels per second
Around 100 pixels per second \
Around 100 pixels per second \
vel = 0
ACCEL = 10
for _ in range(1000):
vel = .9 * vel + ACCEL
print(vel)
Analytic approach using recursive sequences:
The equation corresponds to: $ f(n) = 0.9 f(n-1)+10 where f(0) = 0 and f(1) = 10 $
According to WolframAlpha Recursive Sequences Widget the solution is:
$ f(n) = -100(\frac{9}{10}^n - 1) $. For larger $n$ we get 100.
Using symbolic algebra library sympy
:
from sympy import *
y = Function('y')
n = symbols('n', integer=True)
f = y(n) - .9 * y(n-1) - 10
rsolve(f, y(n), {y(0):0})
# outputs
# 100.0 - 100.0*0.9**n