Professional Documents
Culture Documents
Col100 Minor PDF
Col100 Minor PDF
5 Points
Write a Python program that given two vectors a and b returns their dot-product = ∑ ai ⋅
bi , by filling in the numbered blanks appropriately in the subparts of this question.
n = len(a)
# INVARIANT ___________________(2)
return ans
Q1.1 Initialisation
0.5 Points
To what value should variable ans be initialised? (use a decimal point, e.g., -1.0)
0.0
The statements in the loop body are: (you may use multiple lines)
when i == n+1
Q2 Making change
7 Points
If you have an unlimited supply of ₹500, ₹200, and ₹100 notes, how many different ways are
there to make change for a given amount of money? For example, there are three ways of
making change for ₹400:
Complete the following algorithm for this problem. Assume that the amount is always a
multiple of ₹100.
[Hint: To make change for e.g. ₹2100, I can either (a) use at least one ₹500 note and then
make change for ₹1600, or (b) make change for ₹2100 using only ₹200 and ₹100 notes.]
change521 : N → N
change521(n) = {
__(1)__ if n < 500,
__(2)__ otherwise
Given a natural number n divisible by 100, change521(n) gives the number of different
ways to make ₹n using ₹500, ₹200, and ₹100 notes.
Your algorithm may use the following helper functions, which you should also complete:
change21(n) = __(3)__
change21(n) gives the number of different ways to make ₹n using ₹200 and ₹100 notes.
change1(n) = __(4)__
change1(n) gives the number of different ways to make ₹n using ₹100 notes.
What should be returned by change521(n) if n < 500? (Write only a single mathematical
expression; no explanation needed)
return change21
x == (n//500)
y == n%500
if x ==0:
elif x == 1:
else:
Complete the definition of the mathematical function change21(n). You may use multiple
lines.
def change21(n):
# returns tuples which is (((number of 200 notes), (number of 100 notes for that case)),
further cases)
x = n//200
y = n%200
if x ==0:
elif x == 1:
else:
Complete the definition of the mathematical function change1(n). You may use multiple
lines.
def change1(n):
x = n//100
return x
What is the smallest modification to the algorithm that will make all three functions
change521(n), change21(n), change1(n) give the correct results even if n is not a
multiple of 100? (This need not be the most efficient way to handle that case.)
State which function you will change and to what you will change it.
and so in all functions above one extra term of change in last will be added
Q3 Detecting List Element
6 Points
Here is the Python implementation of an algorithm detect to find whether a given integer
k is present in a list L of size n , assuming that the elements in L are stored in increasing
order ( L[i]<L[i+1] for 0 ≤ i<n-1 ).
return False
else:
if k == L[mid]:
return True
else:
return False
first and last correspond to the index of the first and last elements of the range in which
to search. So, we would call this function as: detect (LL, 0, n-1, k) to find k in a list
LL of size n .
State the recurrence relation for the time complexity of the detect algorithm for an input
list of size n . Justify your answer.
Use the above recurrence to show that the time complexity of detect (LL, 0, n-1, k)
is O(log n).
th
Assume that we can obtain L[i] (the i element of list L ) in constant time, and we can
pass a list to a function in constant time.
The Recurrence relation for detect algorithm is the following. Justify (explain) your answer.
we will keep bisecting our range untill we exhaust our range, this is the basic idea
if k = L[mid]
if not then we check to what side of mid is the answer and discard the useless section
of range, this will work provided the list is either increasing or dewcreasing and our list
is increasing
that is if L[mid} is more then our required number, we decrease the last to mid
else:
that is if L[mid} is less then our required number, we increase the start to mid
when the function is recursively called, this bisection will keep on decreasing the range
untill we get answer or last becomes < start
Show that the time complexity of detect is O(log n), where n is the number of elements
in the search range.
let there be 2^k elements, and for worst case we start from first element and go to last
element of list(ie full list)
now T(n) = T(n-1) + 2 (checking that if mid = required and checking first inequality
now 2^k = n
log_2(n) = k
T(n) = 2log_2(n) +c
T(n) = O(log(n))
Q4 Permutations
7 Points
Consider the following algorithm (written in English, not Python) to compute all
permutations of a given list. For example, permutations[1,2,3] = [[1,2,3], [2,1,3], [2,3,1], [1,3,2],
[3,1,2], [3,2,1]].
permutations(lst):
If lst is empty:
Return [ [ ] ]
Otherwise:
Let rest = [ lst[1], lst[2], ..., lst[n-1] ] be the rest of the elements
Return out
Assume that insertions(x, lst) is a function that takes an element x and a list lst and returns
the list of all possible ways to insert x into lst. For example, insertions(4, [1,2,3]) = [[4,1,2,3],
[1,4,2,3], [1,2,4,3], [1,2,3,4]].
Prove the correctness of this algorithm, i.e. show that the output of permutations(lst) is a list
containing every possible permutation of lst.
Justify the correctness of the algorithm when the input is the empty list. (Keep your answer
short)
Assuming the induction hypothesis, explain why every possible permutation of lst will appear
in the output list. (Keep your answer short)
Q4.6 A modification
1 Point
If we changed the base case to return the empty list [] if the input list is empty, what would
happen when we evaluate permutations([1]) ?
Q5 Polynomial addition
8 Points
as lists [c0 , c1 , … cn ] with the elements being the co-efficients given in lowest-to-highest
exponent order (“Little-Endian”).
p2 (x) == b0 + b1 x + … + bn xn
[Hint: you may use ideas from programs addvec and app discussed in class]
# where k == _____(1)
# OUTPUT p3 =~= p1 + p2
m = len(p1)
n = len(p2)
max(m, n) + 1
Q5.2 Initialisation
0.5 Points
Complete the body of the function addpoly (in Python). You may use multiple lines
Q5.4 Properties
2 Points
addpoly is commutative
addpoly is associative
Q6 Polynomial evaluation
7 Points
p(x) = cn xn + … + c1 x + c0
cn xn + … + c2 x2 + a1 x + a0
= (… (cn ⋅ x + cn−1 ) ⋅ x + … + c1 ) ⋅ x + c0
# over j = 0...n
# EXIT i == _________________(4)
return ans
Q6.1 Initialisation
1 Point
i−1
ans == ∑j=0 cn−j xj
p(0) or c0
STUDENT
Gurharinder Singh
TOTAL POINTS
5.5 / 40 pts
QUESTION 1
QUESTION 2
QUESTION 3
QUESTION 4
Permutations 0 / 7 pts
4.1 Induction variable 0 / 1 pt
4.6 A modification 0 / 1 pt
QUESTION 5
QUESTION 6