[1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
[6 pts] What is displayed by the below sequence of Python statements?
k = 5
print 10 + 2 * k ** 2
print 127 % k
print 13/5 * k
60 2 10
[8 pts]
Tabulate the values taken on by the variables i, j, k, and n as the program fragment below
executes.
n = 1
for i in range(3):
for j in range(3):
k = i * j
n = n + k
i: | 0, | 1, | 2 | |||||||
j: | 0, | 1, | 2, | 0, | 1, | 2, | 0, | 1, | 2 | |
k: | 0, | 0, | 0, | 0, | 1, | 2, | 0, | 2, | 4 | |
n: | 1, | 1, | 1, | 1, | 1, | 2, | 4, | 4, | 6, | 10 |
[6 pts] The below program fragment is intended to display 6, 3, 2, 1.5, 1.2, 1, but it does not display these numbers. What numbers does it display, and how can the program be altered to work as intended?
for i in range(6):
print 6 / (i + 1)
It displays 6, 3, 2, 1, 1, 1. [This is because the division
has an integer on each side, so any remainder is ignored during
the division.] To repair it to work with floating-point
division, simply replace the print statement's 6
with 6.0:
print 6.0 / (i + 1)
An alternative solution would use the float function
to convert the denominator to floating-point:
print 6 / float(i + 1)
[10 pts] The harmonic number for an integer n is the sum of the reciprocals of the integers from 1 to n. For example, the harmonic number for 3 is 1 / 1 + 1 / 2 + 1 / 3 = 1 5 / 6. Complete the below so that it computes and displays the harmonic number corresponding to the integer given by the user. If the user enters 2, the program should display 1.5; for 3, it should display 1.83333333333.
n = input('Find harmonic number for: ')
sum = 0
for i in range(n):
sum = sum + 1.0 / (i + 1)
print sum
[8 pts]
Tabulate the values taken on by the variables i and result as the program fragment below executes.
word = 'bananas'
result = 'f'
for i in range(4):
if word[i:i + 2] != 'na':
result = result + word[i]
i: | 0, | 1, | 2, | 3 | |
result: | f, | fb, | fba, | fbaa |
[10 pts] Complete the following program so that it displays the number of double letters in the word provided by the user. For example, if the user enters bookkeeper, your program should display 3, due to its inclusion of oo, kk, and ee; for peep, it would display just 1. (Don't worry about words having three or more consecutive copies of the same letter.)
word = raw_input('Your word? ')
count = 0
for i in range(1, len(word)):
if word[i - 1] == word[i]:
count = count + 1
print count
[8 pts]
What will be in the list nums after executing the
following fragment?
nums = [4, 2, 2, 3, 5, 5]
for i in range(6):
if nums[i] == i:
nums[i] = nums[i - 1] + 1
[10 pts]
We have a variable names referring to a list with several
names in it. For instance, names might hold the
following:
Write a program fragment displaying all strings from names that
start with the letters Mac
.
for name in names:
if len(name) >= 3 and name[0:3] == 'Mac':
print name
[8 pts] Sketch what appears in the window created by the below program fragment.
from graphics import *
win = GraphWin('Mystery')
for d in range(4):
c = Point(90 - d * 10, 100)
b = Circle(c, d * 10 + 10)
b.draw(win)
[6 pts] Perform each of the following conversions.
| a. | 1101(2) | to decimal |
| b. | 100110(2) | to decimal |
| c. | 22(10) | to binary |
| a. | 1101(2) = | 13(10) |
| b. | 100110(2) = | 38(10) |
| c. | 22(10) = | 10110(2) |