printable version
Exam 2 Review B
[1]
[2]
[3]
[4]
[5]
[6]
[7]
Problem X2rB.1.
Complete the below Python fragment so that it computes and displays
the length of the longest line in the text file.
essay = open('words.txt')
max_length = 0
for line in essay:
if len(line) > max_length:
max_length = len(line)
print max_length
Problem X2rB.2.
Assuming scores.txt is a file of integers, with one
integer on each line, complete the below Python fragment so that it
computes and displays the average of the numbers in the
file.
data = open('scores.txt')
line_count = 0
total = 0
for line in data:
line_count += 1
total += float(line.rstrip())
print 'Average is', total / line_count
Problem X2rB.3.
What is displayed by the following Python fragment?
words = 'The quick brown fox jumps over the lazy dog'
map = {}
for word in words.split():
map[len(word)] = word
for k in range(3, 6):
print k, map[k]
Problem X2rB.4.
Suppose we have a dictionary scores
mapping names (each a string) to test scores (each an integer).
Write a program fragment that displays the name of each student
whose score is 100.
for name in scores:
if scores[name] == 100:
print name
Problem X2rB.5.
Explain as best you can how the IP address 209.65.57.4 comes
to be allocated to ozark.hendrix.edu.
An international agency called ICANN allocates blocks of
addresses to Internet service providers, such as AT&T.
[Actually, ICANN gives blocks to regional Internet
registries,
of which there is essentially one for each
continent, and these registries allocate within these blocks to
ISPs.] They might have given AT&T all addresses that start
with 209.65, for example. AT&T
then divvies this range of addresses among their customers, such as
Hendrix College. Thus, Hendrix College ends up receiving all
addresses whose first 22 bits match with 209.65.56.0 —
that is, 209.65.56.0 through 209.65.59.255. Hendrix
splits its address space some, and they gave the computer science
network all addresses whose first 26 bits match with
209.65.57.0. Then the network administrator decided to
assign one of those — 209.65.57.4 — to
its computer named ozark.
Problem X2rB.6.
In the context of networking, why do messages typically
include port numbers
rather than simply the computers' IP addresses?
A port number, allocated uniquely on the computer for each
program running, allows a sender to identify the particular program
on the destination computer that should receive the message.
Problem X2rB.7.
What does a name server do in the Domain Name System
(DNS)?
A name server receives requests including the name of a computer
(such as ozark.hendrix.edu) and responds with the
computer's IP address (such as 209.65.57.4).