printable version
Quiz 4
[1]
[2]
[3]
[4]
Problem 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.
There are many answers, but here are two:
[0-9][0-9]([0-9][0-9])?
and
[0-9]{2}|[0-9]{4}
Problem 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.
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]
Problem 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
class Interval():
def __init__(self, start):
self.start = start
self.stop = stop
def extend(self, next):
self.start = min(self.start, next)
self.stop = max(self.stop, next)
def contains(self, query):
return self.start <= query <= self.stop
Problem 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(1, len(data)):
t = data[i]
j = i - 1
# Your answer goes here
data[j + 1] = t
def insertion_sort(data):
for i in range(1, len(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