# Quiz 4b#

## Question 1#

In Python, `[1, 2, 3]` is of type list. What is the name of the type of `(1, 2, 3)`?

Tuple
Triple
Pair
Array
Set \

Tuple \

## Question 2#

Which of the following types of data are immutable in Python?

Lists
Booleans
Tuples
Numbers
Strings \

Booleans
Tuples
Numbers
Strings \

## Question 3#

Which of the following functions must include a `global point` declaration in order to change the global variable `point`?

```point = [0, 0]

def function1():
point[0]  = 1
point[1]  = 2

def function2():
point = [50, 50]
```

`function1`
`function2` \

`function2` \

## Question 4#

Consider the following program.

```x = range(5)
#???
#???
```

We can replace the question marks with what two statements to make both variables have the value `[0, 1, 10, 3, 4]`?

```  y = x
y = [0, 1, 10, 3, 4]

```

\

```  x = y
x[2] = 10

```

\

```  y = x
y[-3] = 10

```

\

```  y[-3] = 10
x = y

```

\

```  y = x
x[2] = 10

```

\

```  y = x
x = [0, 1, 10, 3, 4]

```

\

```  y = x
y[-3] = 10

```

and

```  y = x
x[2] = 10

```

Note that the indexing operations like `y[-3]` won’t work in Python3, because `range` is a different object than a list compared to the `range` in Python2 which produces a list.

## Question 5#

In our program, the variable `position` represents a 2D position on the canvas. We want to be able to change the position by some amount in variable `delta`. Why is the following code snippet incorrect?

```position = [50, 50]
delta = [1, -2]
#…
position = position + delta
```

Note that the ellipses represent that we might have code in between what is shown, but such code is irrelevant and omitted.

The `+` operator on lists does not mean addition of the numbers in a list.
One of the elements of `delta` is negative.
Lists do not support the `+` operator.
The numbers in `position` cannot be changed. \

The `+` operator on lists does not mean addition of the numbers in a list. \

## Question 6#

Consider the following program.

```a = ["green", "blue", "white", "black"]
b = a
c = list(a)
d = c

a[3] = "red"
c[2] = a[1]
b = a[1 : 3]
b[1] = c[2]
```

At the end of this code, to how many list objects do the variables refer?

If you run the code and print the variables’ values, you can begin to answer this question. After all, if two variables print differently, they certainly can’t refer to the same object. However, if two variables print the same, you still need to determine whether they refer to the same object. One way is to step through the code while drawing reference diagrams. Another is to mutate one and see if others also mutate.

4 — The four variables each refer to different lists.
2
1 — The four variables each refer to the same list.
3 \

3

`b = a[1:3]` creates a new object. You can also check this by using the `is` operator.

```a = ["green", "blue", "white", "black"]
b = a
c = list(a)
d = c

a[3] = "red"
c[2] = a[1]
b = a[1 : 3]
b[1] = c[2]

print(a is b)  # False
print(b is c)  # False
print(c is d)  # True
```

## Question 7#

Convert the following specification into code. Do the point and rectangle ever overlap?

A point starts at `[10, 20]`. It repeatedly changes position by `[3, 0.7]` — e.g., under button or timer control. Meanwhile, a rectangle stays in place. Its corners are at `[50, 50]` (upper left), `[180, 50]` (upper right), `[180, 140]` (lower right), and `[50, 140]` (lower left).

To check for overlap, i.e., collision, just run your code and check visually. You do not need to implement a point-rectangle collision test. However, we encourage you to think about how you would implement such a test.

Yes
No \

Yes. Including crash test:

```try:
import simplegui
except ModuleNotFoundError:
import simplequi as simplegui

RECT = [(50, 50), (180, 50), (180, 140), (50, 140)]

p = [10, 20]
vel = [3, .7]
crashed = False

def draw(canvas):
global crashed
p[0] += vel[0]
p[1] += vel[1]

canvas.draw_point(p, 'orange')
canvas.draw_polygon(RECT, 12, 'yellow')

if crashed:
canvas.draw_text('Crash!', [50, 50], 40, 'Red')

if not crashed and \
p[0] >= min(RECT[0][0], RECT[1][0], RECT[2][0], RECT[3][0]) and \
p[0] <= max(RECT[0][0], RECT[1][0], RECT[2][0], RECT[3][0]) and \
p[1] >= min(RECT[0][1], RECT[1][1], RECT[2][1], RECT[3][1]) and \
p[1] <= max(RECT[0][1], RECT[1][1], RECT[2][1], RECT[3][1]):
print('crash!')
crashed = True
vel[0] = 0
vel[1] = 0

frame = simplegui.create_frame("Crash test", 300, 200)
frame.set_draw_handler(draw)
frame.start()
```

## Question 8#

Assume we are using acceleration control for a spaceship in a game. That is, we regularly have the following updates:

• The position is incremented by the time interval multiplied by the velocity. This happens on each draw event.

• The velocity is incremented by the time interval multiplied by the acceleration. This happens on each draw event.

• The acceleration is periodically incremented by some fixed vector (the same vector for each step). This could happen on keyboard or timer events.

Assume that, initially, the ship is stationary and subject to no acceleration. What sort of trajectory will the spaceship fly in?

Either figure this out mathematically, or implement it in CodeSkulptor and see what happens.

A non-linear, smooth curve
Spiral
A straight line
Unpredictable \

Mathematically:

```The changes correspond to their derivatives:
p'(t) = v
v'(t) = a
a'(t) = c
=>
a = c*t
v(t) = c*t^2/2
p(t) = c*t^3/6
```

You may think: Due to `t^3` it will be a A non-linear, smooth curve. But it is not. Our motion direction is specified by `c` which always points to the same direction. `t^3` in `p(t)` means that our speed is increasing quadratically.

to be a spiral it the change of acceleration needs to change in negated direction.

Read here for another explanation.

Using Code:

```try:
import simplegui
except ModuleNotFoundError:
import simplequi as simplegui

WIDTH = 1000
HEIGHT = 1000
ACC_INCR = [.00001, .00005]

acc = [0, 0]
vel = [0, 0]
pos = [WIDTH//2, HEIGHT//2]
last_positions = [list(pos)]

def draw(c: simplegui.Canvas):
acc[0] += ACC_INCR[0]
acc[1] += ACC_INCR[1]

vel[0] += acc[0]
vel[1] += acc[1]

last_positions.append(list(pos))
pos[0] += vel[0]
pos[1] += vel[1]

c.draw_circle(pos, 2, 10, 'yellow')
c.draw_polyline(last_positions, 4, 'yellow')
print(pos)

f = simplegui.create_frame('spaceship acceleration', WIDTH, HEIGHT)
f.set_draw_handler(draw)
f.start()
```

## Question 9#

Write a Python program that initializes a global variable to 5. The keydown event handler updates this global variable by doubling it, while the keyup event handler updates it by decrementing it by 3.

What is the value of the global variable after 12 separate key presses, i.e., pressing and releasing one key at a time, and repeating this 12 times in total?

To test your code, the global variable’s value should be 35 after 4 key presses.

5 10 7 14 11 22 19 38 35

```x, y = 10, 7
for _ in range(4-1):
x, y = y*2, y*2-3
print(x, y)
# 38 35
x, y = 10, 7
for _ in range(12-1):
x, y = y*2, y*2-3
print(x, y)
# 8198 8195
```

8195