CSci 150: Foundations of computer science I
Home Syllabus Assignments Tests

Quiz 4: Questions

Q4.1.

[6 pts] Write a regular expression for a two- or four-year digit number, allowing for leading zeroes. Examples include 03 or 1776, but not 3, 025, or 814.

Q4.2.

[6 pts] Arkansas ZIP codes range from 71600 to 72999. Write a regular expression for 5-digit numbers in this range. Examples include 72032 and 71667, but not 71101 or 74601.

Q4.3.

[10 pts] Write an Interval class for tracking a range of numbers on the number line. It should have the following constructor and methods.

Interval(first)

(Constructor) Constructs an interval consisting of just one number, first.

extend(next)

If this interval doesn't already contain next, it is extended just enough to include next.

contains(query)

Returns True if this interval contains query.

The below fragment illustrates how this might be used.

Interval i = Interval(5)
print i.contains(4)   # False
i.extend(2)
i.extend(6)
print i.contains(4)   # True
print i.contains(6)   # True
print i.contains(6.1# False
Q4.4.

[8 pts] Complete the following partial implementation of insertion sort so that it orders its parameter list into increasing order.

def insertion_sort(data):
    for i in range(1len(data)):
        t = data[i]
        j = i - 1

        # Your answer goes here

        data[j + 1] = t

Quiz 4: Solutions

Q4.1.

There are many answers, but here are two: [0-9][0-9]([0-9][0-9])? and [0-9]{2}|[0-9]{4}

Q4.2.

Here are two possible answers: 7(1[6-9]|2[0-9])[0-9]{3} and 71[6-9][0-9][0-9][0-9]|72[0-9][0-9][0-9][0-9][0-9]

Q4.3.
class Interval():
    def __init__(selfstart):
        self.start = start
        self.stop = stop

    def extend(selfnext):
        self.start = min(self.startnext)
        self.stop = max(self.stopnext)

    def contains(selfquery):
        return self.start <= query <= self.stop
Q4.4.
def insertion_sort(data):
    for i in range(1len(data)):
        t = data[i]
        j = i - 1

        while j >= 0 and data[j] > t:
            data[j + 1] = data[j]
            j = j - 1

        data[j + 1] = t