Assignment 1 Python Programming-1(2)
Assignment 1 Python Programming-1(2)
AL INSTITUTE OFWrite
Instructions: TECHNOLOGY
a separate program for each problem. Avoid using in-built functions as much as
ROURKELA
possible. Write your programs primarily using if-else statements and for loops. Save each file as Assign-
ment<#No>_Question<#No>.py. Keep all files in a folder and name the folder with your Roll Number. Zip the
folder and upload it to MS Teams for evaluation.
1. Given a list [2, 8, 10, 683, 45, 1, 996, 139, 190, 58, 730, 9, 800], split it into three different arrays with different
ranges: [0, 10), [10, 100), [100, 1000] and print. [5]
2. Write a program for calculation of sum of square of all odd integers between 1 to 100. [5]
3. Implement the factorial function (do not use inbulit factorial function). [5]
4. Write a function to check whether a number is prime or not. Write a program to get the sum of first 20
prime numbers. [10]
5. Given an array of real numbers x̄ ∈ Rn , find their mean, standard deviation and variance. Take n = 10.
(Do not use inbuilt functions for the same). [10]
Recall:
1∑
n
Mean(µ) = xi
n i=1
1 ∑
n
Variance = (xi − µ)2
n − 1 i=1
√
S.D. = Variance
1
2 2
3 3 3
4 4 4 4
..
.
10 10 10 10 ··· 10
7. Generate a random matrix A(∈ R6×5 ) where all element ∈ [−1, 1]. Count the number of sign change in
A. Move row-wise. [5]
8. Differentiate the function f (x) = 3x2 − 1 at x = 5 using numerical differentiation techniques: [10]
f (x + h) − f (x)
(a) Method 1: f ′ (x) =
h
f (x + h) − f (x − h)
(b) Method 2: f ′ (x) =
2h
CS2672 Assignment 1 Page 2 of 2
∑
n
cos(x) ≈ C(x, n) = ci
i=0
where,
x2
ci = −ci−1 ; i = 0, 1, 2, . . . , n and c0 = 1
2i(2i − 1)
and x is expressed in radians. Write a program that will take the value of x from user and then calculate
its cosine. Write the program in two different ways:
(a) Sum of the first n terms, where n is a positive integer that is read into the computer along with the
numerical value for x.
(b) Continue adding successive terms in the series until the value of the next term becomes smaller than
10−5 .
1 (x−µ)2
f (x|µ, σ 2 ) = √ e− 2σ 2
2πσ 2
Where µ = mean; σ 2 variance. Take, (a) µ = 0, σ 2 = 1; (b) µ = 0, σ 2 = 0.2; (c) µ = 2, σ 2 = 0.5;
(d) µ = −2, σ 2 = 0.5. Plot all the above on same graph.
13. Plot the following functions for v ∈ [−5, 5]. [10]
(a) Saturating linear function:
0, if v < −0.5
ϕ(v) = v + 0.5, if − 0.5 ≤ v ≤ 0.5
1, if v > 0.5
eαv − e−αv
ϕ(v) = tanh(αv) =
eαv + e−αv
Take α = 0.1, 0.5, 1.0 and plot all three functions on the same figure.