Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Introduction to Computers and Computer Science (CSL100) Minor II

October 7, 2013

Note: Maximum marks : 60. All questions carry equal marks. All notations standard (as done in
class). Answer only in the space provided. 4 pages in all

1. The following is a template of a python program for sorting an array using a selection sort
strategy. Fill in the blanks to obtain a complete python function so that the assertions and
invariants hold good at each of the point where they occur.

def selsort(a,left,right): #assert: a[left..right] is given array

i = __________________________________________
#INV1: a[i+1..right] is sorted; left <= i <= right+1
# all a[left..i] <= all a[i+1..right]
# a[i] <= all a[i+1..right]
while (i >= left):

p = ____________________________________

k = ____________________________________
#assert & INV2: p st a[p] >= all a[left..k-1],
# i <= p <= right,
# left <= k <= i+1
while (k <= i):

if ______________________________________________:

p = _________________________________________

k = _____________________________________________
#assert: p st a[p] >= all a[left..i], left <= p <= i

t = _____________________________________________

a[i] = __________________________________________

a[p] = __________________________________________

i = _____________________________________________
#assert: a[left..right] is sorted up
CSL100-MII Name: Entry: Grp: Room: 2

def selsort(a,left,right):
#assert: a[left..right] is established
i = right
#INV: a[i+1..right] is sorted; left <= i <= right+1
# all a[left..i] <= all a[i+1..right]
# a[i] <= all a[i+1..right]
while (i >= left):
p = i
k = left
#assert: p st a[p] >= all a[left..k-1],
# i <= p <= right,
# left <= k <= i+1
while (k <= i):
if (a[k] > a[p]):
p = k
k = k+1
#assert: p st a[p] >= all a[left..i], left <= p <= i
t = a[i]
a[i] = a[p]
a[p] = t
i = i-1
#assert: a[left..right] is sorted up

# try it
A = [10, 23, 21, 45, -12, -10, 67, 98]
print A
selsort (A, 0, len(A)-1)
print A
CSL100-MII Name: Entry: Grp: Room: 3

2. We mayP think of a sequence of integers a0 , a1 , . . . , an with n ≥ 0 as representing the polynomial


n
p(x) = i=0 ai xi of degree n ≥ 0. Assume the coefficients are stored in a python array Coeff
of size n + 1 such that Coeff[i] = ai for each i, 0 ≤ i ≤ n.

(a) Design an iterative algorithm polyval(Coef f, x) (using only O(n) multiplications and
O(n) additions) to compute the value of the polynomial for any integer value x.
(b) State and prove an appropriate invariant property which will allow you to prove the
correctness of your algorithm.
(c) Develop an iterative python function polyval (x) to compute the value of the polynomial
at a point x.
n
X
A glance at the polynomial in the form p(x) = ai xi shows that in this form it requires
i=0
O(n2 ) multiplications and O(n) additions. So we factor out the multiplications to get it in the
form

p(x) = (...((an x + an−1 )x + an−2 )x + ...)x + a0 = (...(((0x + an )x + an−1 )x + an−2 )x + ...)x + a0

and define a tail-recursive function which requires only O(n) multiplications.

polyval(L, x) = polyval tr(L, x, 0)

where

p if L = nil
polyval tr(L, x, p) =
polyval tr(T, x, px + h) if L = h :: T
The invariant property which governs the proof of this tail-recursive function is
k
X
0≤k ≤n∧p= an−j .xn−(k−1)−j)
j=0

We leave the proof of the correctness of this invariant1 to the reader and only apply it to a
python function.
Applying this to an array Coeff requires that we scan the array from the right end (i.e.
Coeff[n] = an and Coeff[0] = a0 .

def polyval (x):


k = n
p = 0
#INV: forall j: 0<=j<=n: Coeff[j] = aj /\ p = sum_{j:0..k} a_(n-j)*x^(n-(k-1)-j)
while (k >= 0):
p = p*x + Coeff[k]
k = k-1
return p

1 In fact an induction proof will also reveal the any bugs in this invariant
CSL100-MII Name: Entry: Grp: Room: 4

3. Consider all positive integers which are multiples of only 2, 3 and 5. It is necessary to generate
the first n > 0 such integers and store them in ascending order in an array H[0..n − 1] for
some given large value of n > 0. All this has to be done in a single pass of the array. Also the
starting point is H[0] = 1.
Complete the following python function for doing this.

def hamming(H,n):
H[0] = 1
# i = 1, x2 = 2, and so on till j5 = 0
i,x2,x3,x5,j2,j3,j5 = 1,2,3,5,0,0,0

#INV: H[0..i-1] contains the first i values of the sequence; 1<=i<= n.


# x2 = 2*H[j2] is the minimum value > H[i-1] with the form 2*x for x
# in H[0..i-1]
# x3 = 3*H[j3] is the minimum value > H[i-1] with the form 3*x for x
# in H[0..i-1]
# x5 = 5*H[j5] is the minimum value > H[i-1] with the form 5*x for x
# in H[0..i-1]
while ( ):

#assert: H[0..n-1] contains the sequence

def hamming(q,n):
q[0] = 1
CSL100-MII Name: Entry: Grp: Room: 5

i,x2,x3,x5,j2,j3,j5 = 1,2,3,5,0,0,0

#INV: q[0..i-1] contains the first i values of the sequence; 1<=i<= n.


# x2 = 2*q[j2] is the minimum value > q[i-1] with the form 2*x for x
# in q[0..i-1]
# x3 = 3*q[j3] is the minimum value > q[i-1] with the form 3*x for x
# in q[0..i-1]
# x5 = 5*q[j5] is the minimum value > q[i-1] with the form 5*x for x
# in q[0..i-1]
while (i < n):
q[i] = min(x2,x3,x5)
i = i+1
#INV: 0 <= j2 <= i-1; x2 = 2*q[j2]
while (x2 <= q[i-1]):
j2,x2 = j2+1,2*q[j2]
#assert: x2 = 2*q[j2] is minimum value greater than q[i-1] with
# the form 2*x for x in q[0..i-1]

#INV and assertion similar to above


while (x3 <= q[i-1]):
j3,x3 = j3+1,3*q[j3]

#ditto
while (x5 <= q[i-1]):
j5,x5 = j5+1,5*q[j5]
#assert: q[0..n-1] contains the sequence

You might also like