Practice Exercises for Lists#
1#
Create a list that contains first 6 prime numbers in ascending order. (This list should be created manually.) Print out the 2nd, 4th, and 6th numbers in this list. Prime list template — Prime list solution — Prime list (Checker)
# Prime number lists
###################################################
# Student should enter code below
prime_numbers = [2, 3, 5, 7, 11, 13]
# 1 is neither a prime or composite number
print(prime_numbers[1], prime_numbers[3], prime_numbers[5])
###################################################
# Expected output
#3 7 13
2#
Given the list a
in the template, make a new
reference b
to a
.
Update the first entry in b
to be zero. What
happened to the first entry in a
? Explain
your answer (in a comment). List reference
template
— List reference
solution
— List reference
(Checker)
# List reference problem
###################################################
# Student should enter code below
a = [5, 3, 1, -1, -3, 5]
b = a
b[0] = 0
print(a)
print(b)
###################################################
# Explanation
# The names `a` and `b` point to the same list. So we mutated the same list.
3#
Given the list a
in the template, make a new
copy b
of the list a
using the function list
. Update the first
entry in b
to be zero. What happened to the
first entry in a
? Explain your answer (in a
comment). List copy
template
— List copy
solution
— List copy
(Checker)
# List reference problem
###################################################
# Student should enter code below
a = [5, 3, 1, -1, -3, 5]
b = list(a)
b[0] = 0
# or
# b = a.copy()
print(a)
print(b)
###################################################
# Explanation
# the list that `a` points to did not change, because we created a copy of this
# list and assigned to the name `b`.
4#
Write a function add_vector(v, w)
that takes
two 2D vectors v
and
w
(represented as lists) and returns a new
2D vector (represented as a list) that is the sum of the two
vectors. Remember that vector addition is performed independently on
each corresponding element of the lists. Hint: returning
v + w
does not work. Vector addition
template
— Vector addition
solution
— Vector addition
(Checker)
# Vector addition function
###################################################
# Student should enter code below
def add_vector(a, b):
return [a[0] + b[0], a[1] + b[1]]
###################################################
# Test
print(add_vector([4, 3], [0, 0]))
print(add_vector([1, 2], [3, 4]))
print(add_vector([2, 3], [-6, -3]))
###################################################
# Output
#[4, 3]
#[4, 6]
#[-4, 0]
5#
Challenge: The program template below is a program designed to run two independent timers with their own start and stop buttons. In particular, each timer should be controlled by its own buttons independent of the other timer’s buttons. The current version has an error that causes both timers to work in unison. Find and fix this error. Debugging test template — Debugging test solution — Debugging test (Checker)
# Mystery bug
# This program should implement two independent timers
# each having their own start and stop buttons.
# Find and correct the error in the code below.
import simplegui
# Initialize two counters.
counter1 = [0, 0]
counter2 = counter1 # <============
# assigning a list to another variable does not create a new list but uses the
# same list which hinders independent timers
# Define event handlers.
def start1():
timer1.start()
def stop1():
timer1.stop()
def start2():
timer2.start()
def stop2():
timer2.stop()
def tick1():
global counter
if counter1[1] == 9:
counter1[0] += 1
counter1[1] = 0
else:
counter1[1] += 1
def tick2():
global counter
if counter2[1] == 9:
counter2[0] += 1
counter2[1] = 0
else:
counter2[1] += 1
# Define draw handler.
def draw(canvas):
canvas.draw_text("Timer 1: " + str(counter1[0] % 10) + "." + str(counter1[1]), [50, 100], 24, "White")
canvas.draw_text("Timer 2: " + str(counter2[0] % 10) + "." + str(counter2[1]), [50, 200], 24, "White")
# Register event handlers.
frame = simplegui.create_frame("Mystery bug", 300, 300)
frame.add_button("Start timer1", start1, 200)
frame.add_button("Stop timer1", stop1, 200)
frame.add_button("Start timer2", start2, 200)
frame.add_button("Stop timer2", stop2, 200)
frame.set_draw_handler(draw)
timer1 = simplegui.create_timer(100, tick1)
timer2 = simplegui.create_timer(100, tick2)
# Start frame.
frame.start()
Compare this with c
and d
in this example