0% found this document useful (0 votes)
8 views4 pages

Programming I (Python) Assignment 3: Instructions

Uploaded by

Aryan Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Programming I (Python) Assignment 3: Instructions

Uploaded by

Aryan Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Programming I (Python)

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

3. Upload answers.tar.gz as the submission to the assignment.

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)

3. (a) Write a function diamond_a() that prints a diamond of height 5.


 
__ *
_ ***
*****
_ ***
__ *
 
(b) Write a function diamond_b(n) that prints a diamond of height n, where n is an odd
number. Your function is not expected to behave deterministically if n is not an odd
number. For example, diamond_b(3) will give:
 
_*
***
_*
 
and diamond_b(5) will give a diamond as printed in part(a).
(c) Write a function diamond_c(n, c) that prints a diamond of height n made of character c,
where n is an odd number. Your function is not expected to behave deterministically if n
is not an odd number. For example, diamond(3, ’1’) will give:
 
_1
111
_1
 
(file: diamond.py)

4. (a) Write a function ndiamond_a() that prints a numerical diamond of height 5.


1
Note that ’_’ denotes a space character in the questions 2, Q3(a), Q3(b).

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

passed to it as input parameter.


Example:
 
print ( mattrans ([[1 , 2] , [3 , 4]]))
 
 
2 1 2
A matrix is represented as a list of lists: [[1, 2], [3, 4]]. This holds for all instances of matrices
3 4
in this assignment.

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

You might also like