Academic Year 2022 Sample

Python example test – session 2

General note: The following code is executed as the first cell of the notebook in which all other problems’ codes are run.

import numpy as np from scipy.integrate 
import solve_ivp 
import scipy.stats as st from scipy 
import signal 
import matplotlib.pyplot as plt 

1. What is the output of this code?

A = np.array([ [1, 2], [3, 4] ]) 
b = np.array([5, 6]) 
c = np.array([0, 1]) 
print(np.dot(A.T + c, b))
| A       | B       | C       | D       | E       | F       |
|---------|---------|---------|---------|---------|---------|
| [23 45] | [17 50] | [34 34] | [28 39] | [29 40] | [22 44] |

2. What is the output of this code?

A = np.arange(9).reshape(3, -1) 
B = np.eye(3) 
print(np.trace(A - 2 * B))
| A  | B  | C  | D | E | F | G  |
|----|----|----|---|---|---|----|
| 10 | -6 | -1 | 0 | 1 | 6 | 10 |

3. What is the output of this code?

I = np.array([  
 [1, 1, 1, 1, 0, 0, 0, 0], 
 [1, 1, 1, 1, 0, 0, 0, 0], 
 [1, 1, 1, 1, 0, 0, 0, 0], 
 [1, 1, 1, 1, 0, 0, 0, 0], 
 [0, 0, 0, 0, 1, 1, 1, 1], 
 [0, 0, 0, 0, 1, 1, 1, 1], 
 [0, 0, 0, 0, 1, 1, 1, 1],
 [0, 0, 0, 0, 1, 1, 1, 1]
 ])   
 
 S_x = np.outer( np.array([1, 2, 1]).T, np.array([-1, 0, 1]) )  
 print(signal.convolve2d(I, S_x, boundary='symm'))

A.

[[0 0 0 0  2  2 0 0 0 0] 
[ 0 0 0 0  2  2 0 0 0 0] 
[ 0 0 0 0  2  2 0 0 0 0] 
[ 0 0 0 0  2  2 0 0 0 0] 
[ 0 0 0 0  1  1 0 0 0 0] 
[ 0 0 0 0 -1 -1 0 0 0 0] 
[ 0 0 0 0 -2 -2 0 0 0 0] 
[ 0 0 0 0 -2 -2 0 0 0 0] 
[ 0 0 0 0 -2 -2 0 0 0 0] 
[ 0 0 0 0 -2 -2 0 0 0 0]]

B.

[[0 0 0 0  4  4 0 0 0 0] 
[ 0 0 0 0  4  4 0 0 0 0] 
[ 0 0 0 0  4  4 0 0 0 0] 
[ 0 0 0 0  4  4 0 0 0 0] 
[ 0 0 0 0  2  2 0 0 0 0] 
[ 0 0 0 0 -2 -2 0 0 0 0] 
[ 0 0 0 0 -4 -4 0 0 0 0] 
[ 0 0 0 0 -4 -4 0 0 0 0] 
[ 0 0 0 0 -4 -4 0 0 0 0] 
[ 0 0 0 0 -4 -4 0 0 0 0]]

C.

[[0 0 0 0 0  0  0  0  0  0] 
[ 0 0 0 0 0  0  0  0  0  0] 
[ 0 0 0 0 0  0  0  0  0  0] 
[ 0 0 0 0 0  0  0  0  0  0] 
[ 2 2 2 2 1 -1 -2 -2 -2 -2] 
[ 2 2 2 2 1 -1 -2 -2 -2 -2] 
[ 0 0 0 0 0  0  0  0  0  0] 
[ 0 0 0 0 0  0  0  0  0  0] 
[ 0 0 0 0 0  0  0  0  0  0] 
[ 0 0 0 0 0  0  0  0  0  0]]

D.

[[0 0 0 0 0  0  0  0  0  0] 
[ 0 0 0 0 0  0  0  0  0  0] 
[ 0 0 0 0 0  0  0  0  0  0] 
[ 0 0 0 0 0  0  0  0  0  0] 
[ 4 4 4 4 2 -2 -4 -4 -4 -4] 
[ 4 4 4 4 2 -2 -4 -4 -4 -4] 
[ 0 0 0 0 0  0  0  0  0  0] 
[ 0 0 0 0 0  0  0  0  0  0] 
[ 0 0 0 0 0  0  0  0  0  0] 
[ 0 0 0 0 0  0  0  0  0  0]]

4. What is the output of this code?

from sympy import Symbol, Limit, sin 
x = Symbol('x') 
ans = Limit(np.power((1 + (1 / x)), x), x, np.inf).doit() 
print(round(ans, 2))
| A     | B     | C     | D    | E | F   | G    | H    | I    |
|-------|-------|-------|------|---|-----|------|------|------|
| -7.39 | -3.14 | -2.72 | -1.0 | 0 | 1.0 | 2.72 | 3.14 | 7.39 |

5. What is the output of this code?

def is_prime(num):  
    for n in range(2, int(num ** 1/2) + 1): 
        if num % n == 0: 
        return False 
    return True  

def sum_digits(num):  
    return (num % 10) + (num // 10)  

a, b = [2**i for i in range(4, 6)] 
c, d = [10*i for i in range(2, 4)]  

a1 = np.arange(a, b) 
a2 = np.arange(c, d)  

s1 = set(filter(is_prime, a1)) 
s2 = set([i for i in a2 if sum_digits(i) < 2**3])

print(len(s1.union(s2)))
| A | B | C | D  | E  | F  | G  | H  |
|---|---|---|----|----|----|----|----|
| 0 | 7 | 9 | 10 | 11 | 12 | 13 | 42 |

6. What is the output of this code?

import pandas as pd 
from sklearn.neighbors import KernelDensity 
from scipy.integrate import quad   

def pdf2cdf(X): 
    ''' 
    Calculates cumulative distribution function (CDF) using 
    integration 
    '''  

kde = KernelDensity(kernel='gaussian', bandwidth=0.75) .fit(X[:, np.newaxis])   
gmm_pdf = lambda x: np.exp(kde.score(np.array([x]).reshape(-1, 1)))   

x_cdf = np.arange(-5, 20, 0.1) 
y_cdf = np.array([tup[0] 
    for tup in [quad(gmm_pdf, a, b) 
        for a, b in [(a, b) 
            for a, b in zip(x_cdf, x_cdf[1:len(x_cdf)])]]] + [0] ).cumsum()   
    
    return x_cdf, y_cdf   
    
N = 10000 

# a guassian-mixture PDF 
X = np.concatenate(( 
    np.random.normal(10, 2, 10*N), 
    np.random.normal(0,  1, 2 *N) 
))  
    
s = pd.Series(X)  

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(20, 6))  

s.plot(kind='kde', bw_method='scott', ax=ax1) 
ax1.set_title('Mixed gaussian PDF')  

x, y_cdf = pdf2cdf(X)  

ax2.plot(x, y_cdf, color='b') 
ax2.set_title('CDF from integration over gaussian-mixture PDF')

A.

B.

C.

D.

E.

F.

Last updated