IP Assignment 1 2022
IP Assignment 1 2022
Assignment 1
January 2022
GENERAL INSTRUCTIONS
1. In this assignment, you need to write the code for every sub-problem in different files.
2. You are only allowed to use the basic modules available in Python. You cannot use any
external modules, libraries or APIs.
3. Please create a document citing all the online and offline resources that you used in doing
this assignment.
4. Your code will be checked for plagiarism against the code of your classmates as well as some
sample codes available online.
5. You can solve any 6 problems out of 8. Out of these 6 problems, one of either Q6 or Q7 is
mandatory to attempt. Rest all the other problems are bonus and practice questions.
6. Bonus problems will be graded only if you score more than 80% in the regular 6 problems.
7. As it is your first intensive assignment, hence start early. Resolve all your doubts with the
TAs, 2 days before the deadline.
SUBMISSION INSTRUCTIONS
Q1.
Print following patterns (exactly as depicted): You must write a generic code so that you can change
n to get different size patterns, but the pattern should not change. A menu-driven program should
be made for each of these patterns. The program should take the type of pattern as input (the
strings in bold below) first, followed by the value of n.
Input ‘n’ as number of students. For every student, take Geometry Dimension as input: 2D or 3D. For
either of these choices, print a Menu List showcasing all geometric figures of that dimension and
take shape code as user input (i.e. from 1 to 12, based on the serial number above, For eg., 1 ->
square, 2-> rectangle. etc.)
Note: Degree of polynomial function will be at max 3 and it will contain only one variable i.e. x.
Q4. Satisfiability Problem
For a given boolean formula Fn consisting of only three boolean variables b1, b2, b3 (i.e. the formula
will be over these boolean variables and use bool operations), determine if the formula is satisfiable
or not.
A formula Fn is called satisfiable if the variables of the formula Fn (b1, b2, b3) can be replaced by the
values TRUE/FALSE in such a way that the formula Fn becomes TRUE.
If it is satisfiable, give any set of values for the variables that satisfies Fn.
Note: Please use the variables names as: Fn, b1, b2, b3.
The boolean expression Fn will be hard-coded. Thus, you’ll use:
Fn = (b1 and not b1)
Fn = (b1 or b2) and (b2 or not b3)
to check if the expression evaluates to True or False.
Examples:
b1 not b1 Fn
T F F
F T F
Thus, this expression is unsatisfiable. (Fn never becomes TRUE)
Output:
> Unsatisfiable
b1 b2 b3 not b3 b1 or b2 b2 or not b3 Fn
T T T F T T T
T T F T T T T
T F T F T F F
T F F T T T T
F T T F T T T
F T F T T T T
F F T F F F F
F F F T F T F
Q5.
Take input a non negative number ‘n’ and create functions for each of the following :
i) a function getReverse(n) which will reverse the digits of n and return it.
ii) a function checkPalindrome(n) which will return True if n is a palindrome, else False.
iii) a function checkNarcissistic(n) which will return True if n is a Narcissistic number
otherwise, False. (Reference : https://mathworld.wolfram.com/NarcissisticNumber.html)
iv) a function findDigitSum(n) which will find the sum of digits of n. If the given number, S,
is a one digit number return S. [ n >=0]
v) a function findSquareDigitSum(n) which will find the sum of squares of digits of n. Let say
S. If S is a one digit number then, return S^2 . [n >=0]
Create a menu driven program to execute these operations. The program should take the operation
as input first, followed by the value of the integer n.
Menu-driven means you have to print the menu and then ask from user to select an operation out of
the given menu. The menu for this question is like this :
--------------------------------------------------------------------------------------------------------------------------------
Hello User, Welcome to the Application. Please select one of the following operations.
1. Find reverse of a number
2. Check whether a number is a palindrome or not.
3. Check whether a number is a Narcissistic number or not.
4. Find the sum of digits of a number
5. Find the sum of squares of digits of a number.
6. Select 6 to exit the application.
--------------------------------------------------------------------------------------------------------------------------------
General flow. You can deviate slightly not much from it. ->
1) First display the menu and ask the user to select an operation.
2) then ask the user to input n, call the required function and then print the result you got from the
function call.
3) Then again go to step 1 and repeat the process until the user chooses option 6.
Q6.
𝑎 𝑏 𝑐
Consider a polynomial function 𝑓(𝑥) = 𝑥 + 𝑥 + 𝑥 + ...
The coefficients of all the variables is 1, but their exponents vary.
You need to create a function find_roots(l) that takes as parameter a list consisting of the exponents
l: [a, b, c, ...]. This will be a finite list. The function find_roots(l) will return a root of the function f(x).
To find the roots, you have to use the Newton-Raphson method. You can have a look at it here -
https://en.wikipedia.org/wiki/Newton%27s_method
You will notice that the Newton-Raphson method requires 𝑓'(𝑥). But, we don’t have it as an input so
you can work step-by-step and first try to compute 𝑓'(𝑥) and then try to apply Newton-Raphson
method. You can also check the differentiability of a function through this and not jump to the
Newton-Raphson method as the Newton-Raphson can only be applied to differentiable functions. If
a non-differentiable function is entered, you can simply exit the code.
Example 1:
2 −1
c = [2, -1] # so this means 𝑓(𝑥) = 𝑥 + 𝑥
Using Newton-Raphson method starting with x0 = 1 and having a threshold of 0.001
We get the root as -1.0
Example 2:
2 0.8
c = [2, 1, 1, 1, 0.8] # so this means 𝑓(𝑥) = 𝑥 + 𝑥 + 𝑥 + 𝑥 + 𝑥
Using Newton-Raphson method starting with x0 = 1 and having a threshold of 0.001
We get the root as 0
Q7.
𝑝 𝑞 𝑟
Consider a polynomial function 𝑓(𝑥) = 𝑥 + 𝑥 + 𝑥 + ...
The coefficients of all the variables is 1, but their exponents vary.
Create a function “calculate_area” that takes as parameter a list consisting of the exponents “l”: [p, q,
r, ...] (a finite list) as well as 2 inputs “a” and “b” to define a range of values, and “d” as a common
difference for sum terms. The function calculate_area(l,a,b,d) will return the area under the curve
from r1 to r2 limits as described by the polynomial definition of f(x) using a list of exponents l.
Use Simpson's 1/3 Algorithm to find the area under a curve, using the exact mathematical expression as
shown below:
Note: To have integral summation terms on the RHS, (b-a) needs to be divisible by d. Hence, exit the code
if d is not divisible by (b-a), else continue with the procedure.
Example 1:
2
c = [2, 1] # so this means 𝑓(𝑥) = 𝑥 + 𝑥
a=0
b=6
d=2
Using Simpson’s ⅓ Algo, RHS= 90
Explanation
6 0+2 0+2*2 0+2*3 2 4 6
∫ 𝑓(𝑥) 𝑑𝑥 = ∫ 𝑓(𝑥) 𝑑𝑥 + ∫ 𝑓(𝑥) 𝑑𝑥 + ∫ 𝑓(𝑥) 𝑑𝑥 = ∫ 𝑓(𝑥) 𝑑𝑥 + ∫ 𝑓(𝑥) 𝑑𝑥 + ∫ 𝑓(𝑥) 𝑑𝑥
0 0 0+2 0+2*2 0 2 4
2
((0+2)−0) (0+(0+2)) 1
∫ 𝑓(𝑥) 𝑑𝑥 = 6
[𝑓(0) + 4𝑓( 2
) + 𝑓(0 + 2)] = 3
[𝑓(0) + 4𝑓(1) + 𝑓(2)]
0
Putting the values, of x = 0, 1, 2 in f(x) we get,
𝑓(0) = 0, 𝑓(1) = 2, 𝑓(2) = 6
2
14
So, ∫ 𝑓(𝑥) 𝑑𝑥 = 3
0
Similarly, do the same step for the other 2 terms,
4 6
74 182
∫ 𝑓(𝑥) 𝑑𝑥 = 3
, ∫ 𝑓(𝑥) 𝑑𝑥 = 3
2 4
Finally,
6 2 4 6
14 74 182 270
∫ 𝑓(𝑥) 𝑑𝑥 = ∫ 𝑓(𝑥) 𝑑𝑥 + ∫ 𝑓(𝑥) 𝑑𝑥 + ∫ 𝑓(𝑥) 𝑑𝑥 = 3
+ 3
+ 3
= 3
= 90
0 0 2 4
Example 2:
0.5 −1
c=[0.5, -1] # so this means 𝑓(𝑥) = 𝑥 + 𝑥
a=1
b=3
d=1
Using Simpson’s ⅓ Algo, RHS= 3.897335456369697
You purchase a car. Every year the car price depreciates, and its maintenance cost increases.
However, you derive value/utility from using it. Let’s say the limit of using the car is 15 years.
The goal is to find the best time to sell your car within the 15 year limit. i.e. the time when the cost
of owning becomes less than the value you derive.
If throughout the 15 year period, the value of your car is the same or more than the cost of owning,
then you sell it after 15 years.
Variables:
Initial cost: c
Depreciation rate: r% every year
Maintenance cost: 1% of c per year for the first 5 years, then it increases by 50% every year
Value/Utility per km is Rs 50 per km (cost of taxi plus comfort and convenience) and this increases
by 10% every year.
We assume you drive a fixed distance every year - say 6000 km
cost of owning = cost of car after applying depreciation rate in that particular year
-
maintenance cost
Output:
Print the year which is the best time to sell your car.
Explanation:
1st year:
maintenance: 1% of initial_cost = 10000
depreciation: 5% of initial_cost = 50000
cost = 1000000 - 50000 - 10000 = 940000
2nd year:
maintenance: old_maintenance + 1% of initial_cost = 20000
depreciation: 5% of initial_cost = 50000
cost = 940000 - 50000 - 20000 = 870000
3rd year:
maintenance: old_maintenance + 1% of initial_cost = 30000
depreciation: 5% of initial_cost = 50000
cost = 870000 - 50000 - 30000 = 790000
4th year:
maintenance: old_maintenance + 1% of initial_cost = 40000
depreciation: 5% of initial_cost = 50000
cost = 790000 - 50000 - 40000 = 700000
5th year:
maintenance: old_maintenance + 1% of initial_cost = 50000
depreciation: 5% of initial_cost = 50000
cost = 700000 - 50000 - 50000 = 600000
6th year:
maintenance: 50% increase of itself = 1.5*50000 = 75000
depreciation: 5% of initial_cost = 50000
cost = 600000 - 50000 - 75000 = 475000
Now,
Cost of owning became less than the utility I derive. (475000 < 531468.3)
Thus, I will sell my car in the 6th year.
BONUS PROBLEMS
B1.
Abhay has recently opened a gift store. The store, since it is new, consists of only 3 items for now.
Every item must have a price per pack(taken as input from the user). To popularize his store, which
he has named as “Delhi Days”, he offers saver combo packs of two items. Each of these combo packs
has some associated discount with them(taken as input from the user). Ultimately, there is one
‘SuperSaver’ pack consisting of all the three items with a 28% discount on the combined price of
those 3 items.
● You have to take the required inputs based upon the output shown below.
● The 1st Saver Combo called ComboPack1 consists of Item1 and Item2.
● 2+nd Saver Combo called ComboPack2 consists of Item1 and Item3
● 3rd Saver Combo called ComboPack3 consists of item2 and Item3
● SuperSaver pack consists of all the items
● All the discounts apply on the sum of total prices for each of the items present in that pack.
Create a catalog of items and the prices as shown in the image below. You can use your own ideas.
You are not required to strictly follow the pattern. You can add your ideas and intricacies within the
code.
User Inputs required:
You have to take prices of each item, discounts for each combo pack and contact number as input
from the user. You can see the prices of respective combo packs have been calculated automatically.
Desired Output:
In this question, we will try to find at how many points does a given ray of light intersect a given
sphere.
The origin of the sphere is described as (x0 , y0 , z0) and the radius as R
2 2 2 2
So, the equation of sphere would be (𝑥 − 𝑥0) + (𝑦 − 𝑦0) + (𝑧 − 𝑧0) − 𝑅 = 0
Given this information you need to find out if the ray of light intersects the sphere or not. And if it
does, print the point(s) of intersection.
Constraint: t ≤ 1000
PRACTICE PROBLEMS
Q1:
For these sub-problems you can either write an individual program for each one of them or, you can
use a menu driven approach so as to select a problem number and then solve it. You are free to
apply your ideas. Multiple solutions possible.
Q3:
Write a function playStr(), which takes two strings X and Y as parameters. Also you have to take
these two strings as an input and call the playStr() function for them and then print the value which
is returned by the function.
Note: X and Y consist of English alphabets only.
Do the following task in the function playStr(X, Y):
1. Print whether X has all the vowels (a, e, i, o, u) or not # True or False
(if string X has ‘a’ or ‘A’ or both then we say ‘a’ is in X)
2. Print whether Y is a substring of X. # True or False
3. Return the count of consonants in Y.
(if Y = ‘Apple’ then you have to return 2 i.e. p, l)
Sample 1:
X = ‘Apple’, Y = ‘App’
Output:
False
True
1
Explanation:
False, as Apple contains only 1 vowel
True, as App is a substring of Apple
1 , as App contains only one consonant i.e. p
Q4:
Mohan and Suresh are two close friends. One day they decided to explore the truth behind the
special theory of relativity. With Mohan being the stationary observer on earth, Suresh, with his
state of the art and scientifically approved spacecraft, selects a point beyond the solar system which
he has to reach. The speed of his spacecraft is around 98% of the speed of light. After Suresh has
returned back to the earth, exactly 2 days (24 hours) have passed since. For the clarification he asks
Mohan, how much time has elapsed, according to his clock. To Suresh’s surprise the times being
shown by the clock are different.
So how much time (in hours) has elapsed according to Mohan’s clock? [Hint: Lorentz factor,
2 2)
γ = 1 − [(𝑣 /𝑐 ], where 𝑐 is the speed of light in m/s, 𝑣 is the speed of Suresh’s spacecraft in
m/s.
The time from the perspective of an stationary observer, 𝑡'is given as
𝑡' = (𝑡/γ) where, 𝑡 is the time from the perspective of the observer in
motion]
Constraints:
𝑣 < 𝑐 [speed of spacecraft cannot reach or exceed the speed of light]
Hints:
● Compute Mohan’s version of time = (time by suresh’s clock)/ γ
● Return the value in hours
● Handle the constraint as well, so that the universal law is not violated.
For this problem, You are free to use any number of inputs and create different types of command
line interaction with the user. It’s all about your own ingenuity and creativity.
You can try implementing the “BABYLONIAN METHOD” to compute the square root of a number and
use it for this problem.