Professional Documents
Culture Documents
Minor2 Sol
Minor2 Sol
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.
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
(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
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 .
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
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
#ditto
while (x5 <= q[i-1]):
j5,x5 = j5+1,5*q[j5]
#assert: q[0..n-1] contains the sequence