Each week you will complete problem(s) to turn in at your section. These problems will earn you up to 2 out of your 3 section participation points for the week. The other point is awarded for being present in your section and participating in the discussion.
You will not be graded on whether you have a perfect solution, but on whether you have demonstrated effort. Therefore please show some work that demonstrates how you got the answer rather than just writing the answer by itself. We will be somewhat lenient about exactly how the work is shown.
Our intention is that these problems would take you up to 30 minutes each week. If you find yourself taking significantly more than this, you may stop your work and write that you worked for 30 minutes. If you have made significant progress, we will still give you credit for your work.
Exercises: Solve the following one (1) Self-Check problem on paper and bring your sheet of paper to your section on Thursday:
class George(Sally): def method2(): print("george 2") class Fred: def method1(): print("fred 1") def method2(): print("fred 2") def __str__(): "fred" class Harold(Sally): def __str__(): return "harold" class Sally(Fred): def method1(): print("sally 1") def __str__(): return "sally"Consider the following code fragment:
elements = [Sally(), Fred(), George(), Harold()] for i in range(0, len(elements)): print(elements[i]) elements[i].method1() elements[i].method2() print()What output is produced by this code? (write the output as a series of 3-line columns in order from left to right)
Exercises: Solve the following three (3) Self-Check problems on paper and bring your sheet of paper to your section on Thursday:
What is an object? Answer the question in your own words.
class Point: x y # The constructor: def __init__( initial_x, int initial_y): X = initial_x Y = initial_y
Exercises: Solve the following one (1) Self-Check problems on paper and bring your sheet of paper to your section on Thursday:
grades
that takes a list of tuples as a parameter. Each tuple in this list contains a name and a grade. Return a dictionary that contains a mapping from each student's name to a list of their grades. Exercises: Solve the following two (2) Self-Check problems on paper and bring your sheet of paper to your section on Thursday:
list
has every function that a Set
has, and more. So why would you use a set
rather than a list
? (A few sentences will suffice.){key1: value1, key2: value2, ...}
dict = {} dict[8] = "Eight" dict[41] = "Forty-one" dict[8] = "Ocho" dict[18] = "Eighteen" dict[50] = "Fifty" dict[132] = "OneThreeTwo" dict[28] = "Twenty-eight" dict[79] = "Seventy-nine" del dict[41] del dict[28] del dict["Eight"] dict[50] = "Forty-one" dict[28] = "18" del dict[18]
Exercises: Solve the following problems on paper and bring your sheet of paper to your section on Thursday:
def main(): a = 7 b = 9 p1 = [2, 2] p2 = [0, 0] add_to_x_twice(a, p1) print(str(a) + " " + str(b) + " " + str(p1[0]) + " " + str(p2[0])) add_to_x_twice(b, p2); print(str(a) + " " + str(b) + " " + str(p1[0]) + " " + str(p2[0])) def add_to_x_twice(a, p1): a = a + a p1[0] = a print(str(a) + " " + str(p1[0])) main()
Exercises: Solve the following problem on paper and bring your sheet of paper to your section on Thursday:
def mystery(x): y = 1 z = 0 # Point A while (x > y): # Point B z = z + x - y x = x // 2 # Point C y = y * 2 # Point D # Point E return zCopy the table below onto a sheet of paper and fill it in with the words ALWAYS, NEVER or SOMETIMES.
x > y |
z > 0 |
y % 2 == 0 |
|
---|---|---|---|
Point A | |||
Point B | |||
Point C | |||
Point D | |||
Point E |
Exercises: Solve the following problem on paper and bring your sheet of paper to your section on Thursday:
def mystery(list): for i in range(1, len(list) - 1): if (list[i] > list[i - 1]): list[i + 1] = list[i - 1] + list[i + 1];Below are a list of specific lists of integers. You are to indicate what values would be stored in the list after function
mystery
executes if the given integer list is passed
as a parameter to mystery
.
[2, 4] [1, 2, 3] [2, 2, 2, 2, 2] [1, 2, 2, 2, 2] [2, 4, 6, 8]Show your work by writing the list's initial contents and then crossing out elements and writing new values as they change.
Exercises: Solve the following problem on paper and bring your sheet of paper to your section on Thursday:
8 4 2 9 7 13 5 9your function should produce the following output:
Sum of 1 = 8 Sum of 2 = 12 Sum of 3 = 14 Sum of 4 = 23 Sum of 5 = 30 Sum of 6 = 43 Sum of 7 = 48 Sum of 8 = 57 Average = 7.125Notice that the various lines of output report the sum including just the first number, then including the first two numbers, then including the first three numbers, and so on, up to the sum including all numbers. The final line of output reports the average of the sequence of numbers. Notice that this is the average of the numbers themselves, not the average of the cumulative sums.
The amount of output will vary depending upon how many numbers are in the sequence. For example, if the file contains the following values:
1 2 3 4the function should produce the following output:
Sum of 1 = 1 Sum of 2 = 3 Sum of 3 = 6 Sum of 4 = 10 Average = 2.5You are to exactly reproduce the format of these sample outputs. You may assume that the file has at least one integer to be processed.
Exercises: Solve the following problem on paper and bring your sheet of paper to your section on Thursday:
public static int mystery(int x) { int y = 1; int z = 0; // Point A while (x > y) { // Point B z = z + x - y; x = x / 2; // Point C y = y * 2; // Point D } // Point E return z; }Copy the table below onto a sheet of paper and fill it in with the words ALWAYS, NEVER or SOMETIMES.
x > y |
z > 0 |
y % 2 == 0 |
|
---|---|---|---|
Point A | |||
Point B | |||
Point C | |||
Point D | |||
Point E |
while
, Random
, boolean
()
Exercises: Solve the following problems on paper and bring your sheet of paper to your section on Thursday:
while
loop mystery.
def mystery(x): y = 0 while (x % 2 == 0): y += 1 x = x // 2 print(str(x) + " " + str(y))For each function call, make a table showing the values that
x
and y
have as you execute the while
loop for that particular call. For example, for the first two calls, the tables look like this:
mystery(19) mystery(42) x y x y -------- -------- 19 0 42 0 21 1You are to write out the tables for the other three calls.
mystery(48) mystery(40) mystery(64)
xxxxxxx xxxxxxxxxxxxx xxxxxx xxxxxxxxxxx xxxxxxxxxxxxxxxxx
Exercises: Solve the following problems on paper and bring your sheet of paper to your section on Thursday:
print_dashed
that takes a string as a parameter and outputs it with each character separated by dashes. For example, the call print_dashed("hello")
should output h-e-l-l-o
.
if/else
, input
, return
()
Exercises: Solve the following problems on paper and bring your sheet of paper to your section on Thursday:
1 + (1/2) + (1/3) + (1/4) + (1/5) + ...
You may assume that the parameter n is non-negative.print_triangle_type(5, 7, 7)
print_triangle_type(6, 6, 6)
print_triangle_type(5, 7, 8)
print_triangle_type(12, 18, 12)
The output produced should be the following:
isosceles
equilateral
scalene
isosceles
Exercises: Solve the following two (2) problems on paper and bring your sheet of paper to your section on Thursday:
def main(): x = 15 y = 2 z = 9 mystery(x, y, z) mystery(z, x, y) mystery(y, z, x) mystery(x - z, z, z) def mystery(x, y, z): print("The " + str(x) + " monkeys ate " + str(y + z) + " bananas")Make a table that shows what value is being passed to each of x and y and z for each of the four calls and then indicate the output produced by the program.
for
loops
()
Exercises: Solve the following two (2) Self-Check problems on paper and bring your sheet of paper to your section on Thursday:
14 // 7 * 2 + 30 / 5 + 1
(12 + 3) // 4 * 2
813 % 100 // 3 + 2.4
4.0 / 2 * 9 / 2
2.5 * 2 + 8 / 5.0 + 10 / 3
What is the output of the following sequence of loops?
for i in range(1, 3):
for j in range(1, 4):
for k in range(1, 5):
print("*", end='')
print("!", end='')
print()
For the first problem, please show some work rather than just writing the answer. Write out sub-expressions as you compute their values, and circle or underline operands to show precedence, as was done in lecture. You may use a calculator if you want, though one shouldn't be necessary for these problems. For example:
2 + 19 % 5 - 11 * (5 // 2)
2 + 19 % 5 - 11 * 2
2 + 4 - 11 * 2
2 + 4 - 22
6 - 22
-16
Complete the introductory survey.