[1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13]
Suppose we have the Node
class as defined earlier.
class Node:
def __init__(self, value, next):
self.data = value
self.next = next
Assuming the variable head
references the first Node
in
a list where each node contains a reference to a word,
write a program fragment that counts how many of the words
in the list have exactly four letters.
count = 0
cur_loc = head
while cur_loc is not None:
if len(cur_loc.data) == 4:
count += 1
cur_loc = cur_loc.next
print('There are', count, 'four-letter words.')
Suppose we have the Node
class as defined earlier.
class Node:
def __init__(self, value, next):
self.data = value
self.next = next
Assuming the variable head
references the first Node
in
a list where each node contains a reference to a string,
write a program fragment that displays the final string in the
list.
cur_loc = head
while cur_loc is not None:
if cur_loc.next is None:
print(cur_loc.data)
cur_loc = cur_loc.next
Suppose we have the Node
class as defined earlier.
class Node:
def __init__(self, value, next):
self.data = value
self.next = next
Assuming the variable head
references the first Node
in
a list, write a fragment that removes the final Node
in
the list. You can assume that the list has at least two
nodes.
cur_loc = head
while cur_loc is not None:
if cur_loc.next.next is None:
cur_loc.next = None
cur_loc = cur_loc.next
Suppose we have the Node
class as defined earlier.
class Node:
def __init__(self, value, next):
self.data = value
self.next = next
We are implementing a class
WordList
that uses this to store a sequence of words.
class WordList:
def __init__(self):
self.head = None
# ...
Write the following instance methods as they should appear within the
WordList
class.
get(index)
Returns the string at index
(numbered from
0). The method may assume that index
is
at least 0 and less than the length of the list.
remove_first()
Removes the first value in this list, and returns the value removed (not the node removed). This method may assume that prior to invocation the list contains at least one node.
class WordList:
def __init__(self):
self.head = None
def get(self, index):
cur_loc = self.head
for i in range(index):
cur_loc = cur_loc.next
return cur_loc.data
def remove_first(self):
old_head = self.head
self.head = old_head.next
return old_head.data
Write the following instance methods as they should appear within the
WordList
class of the preceding problem.
add(index, val)
Inserts val
into position index
of
this list. (Don't worry about what happens if index
is
invalid.)
index_of(val)
Returns the index of the first occurrence of val
in
this list, or −1 if it doesn't occur in the list at
all.
class WordList:
def __init__(self):
self.head = None
def add(self, index, val):
if index == 0:
new_head = Node(val, self.head)
self.head = new_head
else:
cur_loc = self.head
for i in range(index - 1):
cur_loc = cur_loc.next
after = Node(val, cur_loc.next)
cur_loc.next = after
def index_of(self, val):
index = 0
cur_loc = self.head
while cur_loc is not None:
if cur_loc.data == val:
return index
index += 1
cur_loc = cur_loc.next
return -1
Write the following instance method as it should appear within the
WordList
class of the preceding problem.
add_after(at, add)
Locates the first node in our list containing
the word at_value
and inserts a new node
referencing new_value
just after it.
The method may assume that at
indeed appears in
the list.
class WordList:
def __init__(self):
self.head = None
def add_after(self, at_value, new_value):
cur_loc = self.head
while cur_loc is not None:
if cur_loc.data == at_value:
after = Node(new_value, cur_loc.next)
cur_loc.next = after
return
cur_loc = cur_loc.next
Recall that the float
function raises a
ValueError
exception if the string it is given as a
parameter does not represent a legitimate number (like
“positive” or “3+4”).
Complete the following program so that it displays the square root of the number typed by the user or “bad user” if the user's input doesn't correspond to an integer.
user = input()
try:
num = float(user)
print(num ** 0.5)
except ValueError:
print('bad user')
Without using the len
function,
complete the following function so that it returns the length of
the parameter list.
You'll want to take advantage of
IndexError
, which is raised when you attempt to access a
list at an invalid index.
def compute_length(vals):
def compute_length(vals):
pos = 0
try:
while True:
cur_val = vals[pos]
pos += 1
except IndexError:
return i
Define the function of a router in the Internet.
A router is a computing device that is attached to two or more separate networks. Any time it receives a message, it forwards the message to the appropriate computer on the other network as necessary.
The Internet networking stack has four layers: Application, Transport, Network, and Physical. Define what is accomplished by the Network layer (as implemented by IP, the Internet Protocol).
The Network layer provides best-effort delivery of an individual, short message between two computers that may exist in different networks within the Internet.
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.
What does a name server do in the Internet's 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).
Suppose you tell your Web browser to retrieve the page http://ozark.hendrix.edu/~burch/cs/150/. Using HTTP, what message might the Web browser send to ozark.hendrix.edu to retrieve the requested Web page?
Following is a minimal example of the text the browser might send to the Web server:
GET /~burch/cs/150 HTTP/1.1 Host: ozark.hendrix.edu [an empty line signals the end of the request]