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?
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]
Write a regular expression that accepts only strings of letters that begin with a capital letter followed by one or more lower-case letters.
Write a regular expression for strings containing two integers separated by either a hyphen or a slash. Examples include 450-1377 and 12/25.
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
.
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.)
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 |
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
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
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)
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)
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 ” |
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
, but you'd invoke the
name.count('e')
len
function by writing
.len(name)
1 4 2 5 6
[A-Z][a-z]+
[0-9]+[-/][0-9]+
.*e *[0-9].*
(There is a space between the e
and the asterisk.)
w[a-z]*r[a-z]*
a. |
c[a-zA-Z]*
|
b. |
[1-9][0-9]*
|
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 |
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 |
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
.
0.00 0
0.05 1
0.10 2
0.15 3
0.20 4
{2:4s}({0:4d}) = {1:6.3f}
”