Вы находитесь на странице: 1из 4

ES 102: Programming I Python

Mid-Term Examination

October 17, 2017

Duration: 1 hours 30 minutes Maximum Marks: 60

Instructions
1. Write your answers to the Theory section on the answer sheet provided.

2. You are not allowed to refer to your cheat-sheet or to use the computer during the Theory
section.

3. Once you are done with the Theory section, submit your answer sheet and take a new one.

4. You may start using your computer now. You may refer to your cheat-sheet during Practical
section.

5. Most of the work in Practical section is to be done on the computer. But some of it also
requires you to answer on a sheet.

6. You are not allowed to use the computer before you return your Theory section answer sheet.
The Theory section answer sheet, once submitted, shall not be returned to you.

7. Please download the file mt.tar.gz from the LMS to an appropriate location in your computer
and uncompress it using the following instruction:

tar xvf mt.tar.gz

A directory named mt is created. This contains all the starter files in which you shall write
your programming solutions.

8. Please note that the assessment of the Practical section will be partially automated. There-
fore, it is essential that you follow the instructions to the word (e.g. input/output format,
identifier and function names etc.)

9. Theory section must strictly be done first. You are free to divide the time between the two
sections as you find appropriate. We estimate a reasonable time division to be 45 minutes for
Theory and the remaining for Practical.
10. Instructions for submitting your solution to the Practical section are given in the README
file in the mt directory.

1 Theory
1. What will be the output of the following:
(a) map(lambda x: x > 100, range(98,102)) (1)
(b) def compare (f ,x , y ): return f ( x ) > f ( y ) (1)
map ( lambda x : compare ( lambda x : x ,x ,100) , range (98 ,102))

(c) map(lambda x: compare(len, x, "Hello, World!"), ["Hi world!", "Hello universe!", "Hello"])
(1)

2. (a) Define the following terms: (2)


1. Declaration, use of a variable
2. Binding of a variable
(b) What is scoping rule Python uses. Explain using the notion of declaration, use and binding. (2)
(c) Using Pythons scoping rules, point out which use binds to which declaration: (2)
map(lambda x: compare(lambda x: x,x,100),range(98,102))

3. What are decorators in Python? How do you apply a decorator d to a function f? Explain why (3)
decorators are syntactic sugar by presenting an alternative code which has the same effect as
the application of decorator d to a function f.

4. Provide the following for the library functionality, type signature, and example use for the
following functions:
(a) map (1)
(b) reduce (1)
(c) filter (1)

5. (a) What are modules? Why are they needed in programming? (1)
(b) Various syntaxes for importing a module in Python. What are their relative advantages (1)
and disadvantages?

6. (a) What are some of the disadvantages of recursive functions? (1)


(b) Consider the function fib implemented as follows: (1)
 
def fib ( n ):
if ( n == 0 or n == 1):
return 1
else :
return fib ( n - 1) + fib ( n - 2)
 
What is the maximum depth of the program during the computation of fib(10)? Explain.
(c) Present the recursive relation showing the total number of activation frames pushed into (1)
the program stack during this computation.

Page 2
Practical
7. (a) Implement a procedure horizLine(n,c) which takes an integer n and a character c, and (1)
prints out a horizontal line comprised of all cs. For example, calling horizLine(10,#)
will print in the following line:
# #########

(Hint: The command sys.stdout.write(c) prints a character c without printing anything


else. You must import the module sys to use this command.)
(b) Making use of horizLine, implement a function borderedMessage(msg) which takes a string (2)
msg and prints out a bordered message. For example, calling borderedMessage("Hello World!")
will result in the following getting printed:
+==============+
| Hello World ! |
+==============+

(q7.py)
8. An examiner has prepared n distinct types of questions for an exam. For each type, he has (5)
printed c copies. Each copy has inscribed on it a randomly generated number, a unique code
for each copy, mapping it back to its type. Implement a function generateCodes(n,c) which,
given the number of distinct question paper types n, and the number of copies of each question
paper type c, returns a mapping between question paper type and the list of corresponding
codes. Additional information:
1. For n, the types are [1,2,...,n].
2. For randomly generating an integer between x and y (both included), we can use the
following code:
import random
...
random . randint (x , y )

(q8.py)
9. (a) Implement the following basic functions over lists as follows: (3)
1. head(l): returns the first element of the list
2. tail(l): returns the rest of the list
3. cons(e, l) Returns a list l such that head(l) = e and tail(l) = lst.
(b) Using the above three function, provide a recursive implementation of a function (5)
split_at_n(lst, n) which, given a list lst and an integer n, return a pair (2-tuple) of two
lists (f,s) where the first list f has the first n members of lst, and s has the rest of its
elements (if any).
(c) Using split_at_n function, implement a recursive function break_list(lst,n) which, given (5)
a list lst and an integer n, return a list of lists, each of which has n element (except, may
be, the last one, if len(lst) is non divisible by n). For example:
break_list(range(10),3)=[[0,1,2],[3,4,5],[6,7,8],[9]].
(q9.py)

Page 3
10. (a) Implement a mymax(comp, a, b) function (similar to the Python library max function) as (1)
a higher order function which takes the comparison function as a parameter.
(b) Write a higher order function compare(f,x,y) which returns 1 if f(x)>f(y), 0 if f(x)=f(y), (1)
and -1 if f(x)<f(y).
(c) Write an adaptor function compare_tf(f,x,y) which returns True if f(x)>f(y), False oth- (1)
erwise. compare_tf should use compare.
(d) Write an instruction using map that takes a list lst of numbers and returns a corresponding (3)
list of Booleans. This list contains a True for every element of lst that is positive, and
False for every element of lst that is not positive. Your instruction should make use of
the compare_tf function defined above. Make use of lambda expressions as a wrapper for
compare_tf.
(q10.py)

11. (a) Write a function gen_iter(n1,n2,st) that returns a generator which lets you iterate over (3)
the sequence n1,n1+1,...,n2 in steps of st.
(b) How do we use this function? Illustrate. (2)
(q11.py)

12. Consider the family tree shown in the picture below:

B C D

E F G

J H I

Edit the code in the directory family to solve the following parts.
(a) In the file family.py, an incomplete/incorrect implementation is provided of the function (2)
makeFamily. Edit this implementation, so that it returns a family tree as above.
(b) Modify the implementation of the functions areSiblings and areCousins to match the (6)
specification given in the function header.
(Hint: A necessary (but not sufficient) evidence of the correctness of your implementation is
that it will run without throwing any AssertionError.)
(family.py)

Page 4

Вам также может понравиться