CSci 150: Foundations of computer science
Home Syllabus Readings Projects Tests

Final Review B: Questions

RFb.1.

Suppose we have the Node class as defined earlier.

class Node:
    def __init__(selfvaluenext):
        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.

RFb.2.

Suppose we have the Node class as defined earlier.

class Node:
    def __init__(selfvaluenext):
        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.

RFb.3.

Suppose we have the Node class as defined earlier.

class Node:
    def __init__(selfvaluenext):
        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.

RFb.4.

Suppose we have the Node class as defined earlier.

class Node:
    def __init__(selfvaluenext):
        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.

RFb.5.

Write the following instance methods as they should appear within the WordList class of the preceding problem.

add(indexval)

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.

RFb.6.

Write the following instance method as it should appear within the WordList class of the preceding problem.

add_after(atadd)

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.

RFb.7.

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()
RFb.8.

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):
RFb.9.

Define the function of a router in the Internet.

RFb.10.

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).

RFb.11.

In the context of networking, why do messages typically include “port numbers” rather than simply the computers' IP addresses?

RFb.12.

What does a name server do in the Internet's Domain Name System (DNS)?

RFb.13.

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?

Final Review B: Solutions

RFb.1.
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.')
RFb.2.
cur_loc = head
while cur_loc is not None:
    if cur_loc.next is None:
        print(cur_loc.data)
    cur_loc = cur_loc.next
RFb.3.
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
RFb.4.
class WordList:
    def __init__(self):
        self.head = None

    def get(selfindex):
        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
RFb.5.
class WordList:
    def __init__(self):
        self.head = None

    def add(selfindexval):
        if index == 0:
            new_head = Node(valself.head)
            self.head = new_head
        else:
            cur_loc = self.head
            for i in range(index - 1):
                cur_loc = cur_loc.next
            after = Node(valcur_loc.next)
            cur_loc.next = after

    def index_of(selfval):
        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
RFb.6.
class WordList:
    def __init__(self):
        self.head = None

    def add_after(selfat_valuenew_value):
        cur_loc = self.head
        while cur_loc is not None:
            if cur_loc.data == at_value:
                after = Node(new_valuecur_loc.next)
                cur_loc.next = after
                return
            cur_loc = cur_loc.next
RFb.7.
try:
    num = float(user)
    print(num ** 0.5)
except ValueError:
    print('bad user')
RFb.8.
def compute_length(vals):
    pos = 0
    try:
        while True:
            cur_val = vals[pos]
            pos += 1
    except IndexError:
        return i
RFb.9.

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.

RFb.10.

The Network layer provides best-effort delivery of an individual, short message between two computers that may exist in different networks within the Internet.

RFb.11.

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.

RFb.12.

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).

RFb.13.

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]