Programming I (Python) Assignment 3: Instructions
Programming I (Python) Assignment 3: Instructions
Assignment 3
Instructions
• Answers to each question should be provided in a file whose name is mentioned against the
respective question.
• This assignment is about functions. Please ensure that your code does not have any
extraneous input/output code.
• In several questions, underscores (’_’) have been used to highlight spaces (’ ’) in the output
code. Your output should contain the space character (’ ’) in all those spaces.
• How to submit.
1. The stub/starter files for all questions (except Q. d) are provided in the directory named
answers. Please write your answers in the files with appropriate names as given in the
questions.
2. Once you are satisfied with your solutions/answers, exit the answers directory and
compress the answers directory preferably using the following command:
tar cvzf answers . tar . gz answers
Part B
Theory Questions
1. (a) Write a short note on side-effects in imperative style programming.
(b) (2 points) Distinguish between named procedures and mathematical functions. Give an
example of each.
(c) Justify the following statements: “Named procedures can not exist without side-effects.”
(d) In the following pieces of code, please identify if they have side-effect or not. If yes, please
mention/mark the precise point where we see side-effect.
i.
def add (x , y ): return x + y
ii.
x = int ( input ( " Enter the number : " ))
iii.
print ( " Hello world ! " )
iv.
sum = 0
for i in range (10):
sum += i
print ( sum )
(file: theory.pdf)
2. Write a function banner(m) that prints prints the message m decorated with borders. For
example, banner("Good Morning!") with give:
**** ***** ****** **
* _Good Morning ! _ *
**** ***** ****** **
1
(file: banner.py)
Page 2
__1
_121
12321
_121
__1
(b) Write a function ndiamond_b(n) that prints a numerical diamond of height n. For example,
ndiamond_b(3) will give:
_1
121
_1
and ndiamond_b(5) will give an output similar to the one in part (a).
(file: ndiamond.py)
5. Implement a function hello(name) that returns a string with ”Hello ” as prefix to name. (name
is a string input.)
Example:
msg = Hello ( " IIITB " )
print ( msg )
will print
hello IIITB
(file: hello.py)
6. Implement a function even_elements(l) that takes an input list l and returns a list only even
elements from l. Use a loop to achieve this.
Example:
lst = even_elements ([0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9])
print ( lst )
will print
[0 , 2 , 4 , 6 , 8]
(file: even.py)
7. Write a function mattrans(m) that calculates (and returns) the transpose of the matrix m 2
Page 3
will print
[[1 , 3] , [2 , 4]]
(file: mattrans.py)
8. Write a function matmul(m1 , m2 ) that calculates (and returns) the product of two matrices
m1 and m2 passed to it as input parameters. Before beginning its main computation, matmul
should check if m1 and m2 are multipliable using the function are_multipliable(m1 , m2 ). If
they are not, matmul should return (not print) an appropriate message as a string.
(file: matmul.py) Example:
print ( matmul ([[1 , 2 , 3] , [4 , 5 , 6]] , [[7 , 10] , [8 , 11] , [9 , 12]]))
will print
[[50 , 68] , [122 , 67]]
(file: matmul.py)
9. (a) Consider a library that maintains its database of titles as a Python dictionary that maps
the titles mapped to the authors and their language.
For example: (see books.csv)
Write a function authors() that returns the dictionary of authors whose books are available
in the library mapped to the list of all the books they have authored.
(b) Consider the library in Q9(a). Write a function languages() that returns a dictionary
mapping the languages in which books are available in the library to the number of titles
available in each.
In both cases, the name of the CSV file in which the library database is given as an input from
the standard input. For your convenience, we have provided provided the code for reading the
CSV file.
(file: library.py)
Page 4