[1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
[10 pts]
Tabulate how the variables i
, x
, and y
change
while executing the below program fragment.
data = [2, 3, 5, 7, 11, 13]
y = 0
for i in range(5):
x = data[i] % 4
if x >= y:
y = y + x
i : |
0 | 1 | 2 | 3 | 4 | ||||||||
x : |
2 | 3 | 1 | 3 | 3 | ||||||||
y : |
0 | 2 | 5 |
[9 pts]
For three string methods other than split
,
give a meaningful example of how to invoke that method
and what that method returns.
For example, for split
, you could write,
“If s
is ‘hello world’ then s.split()
returns the list
[‘hello’, ‘world’].”
If s
is “MacDonald
”, s.count('a')
returns 2 (the number of times a appears in s
).
If s
is “i is {0}
”, s.format(34)
returns “i is 34
”.
If s
is “and
”, s.join(['x', 'y', 'z'])
returns “xandyandz
”.
If s
is “MacDonald
”, s.lower()
returns “macdonald
”.
If s
is “ Old MacDonald
”, s.strip()
returns “Old MacDonald
” (with spaces on both
ends removed).
If s
is “MacDonald
”, s.upper()
returns “MACDONALD
”.
[6 pts] Write a regular expression that describes only those strings of 0's and 1's that start with a 1 and end with at least two 0's. Examples include 100, 101000, and 1101000, but not 001000 or 101010.
1[01]*00
[8 pts]
Complete the while
statement below so that the loop continues
until a
and b
are both odd integers.
(Recall that you can test whether a
is odd by checking whether
a % 2
is 1.)
a = 0
b = 0
while
a = b
b = int(input())
while a % 2 != 1 or b % 2 != 1:
[9 pts] Perform each of the following conversions. Please show your work.
a. | 1001(2) | to decimal |
b. | 1101010(2) | to decimal |
c. | 50(10) | to binary |
a. | 1001(2) = | 9(10) |
b. | 1101010(2) = | 106(10) |
c. | 50(10) = | 110010(2) |
[6 pts] The below table shows the ASCII values corresponding to some characters, while others are left blank. Fill in the blanks.
1 | 0110001 |
3 | _______ |
e | 1100101 |
h | _______ |
1 | 0110001 |
3 | 0110011 |
e | 1100101 |
h | 1101000 |
[8 pts] What is the output of the following program?
def f(x):
a = 2 + g(x)
b = 1 + g(a)
print(a * b)
return b
def g(y):
a = y + 2
print(a)
return a * 2
f(2)
4
12
250
[8 pts]
Below is the definition of a function area_triangle
,
which takes three parameters representing points (each represented
as a list containing the x-coordinate in the first
slot and the y-coordinate in the second) and returns
the area of the triangle between those three points.
Now complete the function area_pentagon
, which takes five
points as parameters (each point represented as with
the triangle), given in clockwise order, and should return the total area of
the pentagon between those points.
(The diagram at right gives a hint.)
import math
def area_triangle(p0, p1, p2): # do not change this function!
a = math.hypot(p0[0] - p1[0], p0[1] - p1[1])
b = math.hypot(p1[0] - p2[0], p1[1] - p2[1])
c = math.hypot(p2[0] - p0[0], p2[1] - p0[1])
p = (a + b + c) / 2
return math.sqrt(p * (p - a) * (p - b) * (p - c))
def area_pentagon(p0, p1, p2, p3, p4):
def area_pentagon(p0, p1, p2, p3, p4):
t0 = area_triangle(p0, p1, p2)
t1 = area_triangle(p0, p2, p3)
t2 = area_triangle(p0, p3, p4)
return t0 + t1 + t2
[8 pts] As we saw, secure sites will never store passwords; they instead use a cryptographic hash function. Describe how a site can verify a password typed later matches the account password, when the account password is not stored anywhere. (Don't worry about salt.)
When the user creates the account, the system will perform a cryptographic hash on the password, storing the result of that has in a database. When the user later attempts to log in, the system will perform the same cryptographic hash on that attempted password, and it will verify that the result of this hash matches stored result from the account creation. If they match, we accept the attempt; but if it doesn't match, then the attempted password is certainly incorrect.
[10 pts]
Complete the function add_b_grades
below, which takes a parameter
grades
that is a dictionary mapping names to integer values.
The function should return the sum of all values associated with
names starting with the letter b.
For example, given the dictionary
{ 'beth': 94, 'ben': 88, 'eva': 99 }
as a parameter,
add_b_grades
should return 182 (from 94 + 88).
def add_b_grades(grades):
def add_b_grades(grades):
total = 0
for name in grades:
if len(name) > 0 and name[0] == 'b':
total += grades[name]
return total