printable version
Test 2 Review A
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
[10]
[11]
[12]
Problem R2a.1.
Both functions and methods allow names to be associated with
more complex computations. For example, with strings,
you can use the len
function and the count
method.
What distinguishes them?
A function is initiated using just its name, and any
information it needs is supplied in the parameters, but a method
must be invoked by preceding the method name with the value
it's operating on
. Thus, if we have a string variable
called name
, you'd invoke the count
method by
writing name.count('e')
, but you'd invoke the
len
function by writing len(name)
.
Problem R2a.2.
What does the below fragment display if next
is
a list of integers containing
[3, 4, 5, −1, 2, 6, −2].
loc = 1
while loc >= 0:
print(loc)
loc = next[loc]
Problem R2a.3.
Write a regular expression that accepts only strings of letters
that begin with a capital letter followed by one or more
lower-case letters.
[A-Z][a-z]+
Problem R2a.4.
Write a regular expression for strings containing two integers
separated by either a hyphen or a slash. Examples include
450-1377 and 12/25.
[0-9]+[-/][0-9]+
Problem R2a.5.
Write a regular expression that describes strings containing
the lower-case letter e followed by a digit, with
possibly some intervening spaces. Examples include
3e8
,
the 100K
,
and e2
, but not e-5
.
.*e *[0-9].*
(There is a space between the e
and the asterisk.)
Problem R2a.6.
Write a regular expression that describes all words of lower-case
letters that begin with w and contain an r. Examples
include weird, word, wrong, and wear, but
not awkward, war-weary, or www.hendrix.edu.
(The latter two are bad because they contain characters that are not
lower-case letters — a hyphen and a period.)
w[a-z]*r[a-z]*
Problem R2a.7.
Write a regular expression for each of the following
concepts.
a. |
a string of alphabetic letters starting with the letter c
|
b. |
a string of digits that doesn't start with 0
|
a. |
c[a-zA-Z]*
|
b. |
[1-9][0-9]*
|
Problem R2a.8.
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(1, 4):
for j in range(1, 4):
k = i * j
n = n + k
i : | | 1, | | | 2, | | | 3 | | |
j : | | 1, | 2, | 3, | 1, | 2, | 3, | 1, | 2, | 3 |
k : | | 1, | 2, | 3, | 2, | 4, | 6, | 3, | 6, | 9 |
n : | 1, | 2, | 4, | 7, | 9, | 13, | 19, | 22, | 28, | 37 |
Problem R2a.9.
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(1, 4):
for j in range(1, 4):
k = i * j
n = n + k
i : | | 1, | | | 2, | | | 3 | | |
j : | | 1, | 2, | 3, | 1, | 2, | 3, | 1, | 2, | 3 |
k : | | 1, | 2, | 3, | 2, | 4, | 6, | 3, | 6, | 9 |
n : | 1, | | | 4, | | | 10, | | | 19 |
Problem R2a.10.
Tabulate the values taken on by the variables
i
, j
, k
, and res
as the
program fragment below executes.
base = 'barber'
res = 'a'
for i in range(len(base)):
k = -1
for j in range(i):
if base[j] == base[i]:
k = j
if k < 0:
res = res + base[i]
print(res)
i |
0, |
1, |
|
2, |
|
|
3, |
|
|
|
4, |
|
|
|
|
5 |
|
|
|
|
|
j |
|
|
0, |
|
0, |
1, |
|
0, |
1, |
2, |
|
0, |
1, |
2, |
3, |
|
0, |
1, |
2, |
3, |
4 |
k |
−1, |
−1, |
|
−1, |
|
|
−1, |
0, |
|
|
−1, |
|
|
|
|
−1, |
|
|
2 |
|
|
res |
ab, |
|
aba, |
|
|
abar, |
|
|
|
|
|
|
|
|
abare |
|
|
|
|
The program displays abare
.
Problem R2a.11.
What is displayed by the following program?
for i in range(5):
template = '{1:4.2f} {0:3d}'
output = template.format(i, i * 0.05)
print(output)
0.00 0
0.05 1
0.10 2
0.15 3
0.20 4
Problem R2a.12.
What value for template
would result in the following
strings being produced?
template.format(1000, 31.6228, 'sqrt') |
→ “sqrt(1000) = 31.623 ” |
template.format(9000, 9.1050, 'log') |
→ “log (9000) = 9.105 ” |
“{2:4s}({0:4d}) = {1:6.3f}
”