Academic Year 2022 Actual

Python actual 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, 0],[0, -1] ])
b = np.array([-42, 42])

print(sum(np.dot(A, b)))
| A   | B   | C   | D | E  | F  | G  |
|-----|-----|-----|---|----|----|----|
| -84 | -42 | -21 | 0 | 21 | 45 | 84 |

2. What is the output of this code?

A = np.arange(9).reshape(3, -1) 
print((A - 3)[:, 1])

A. [-3 0 3] B. [0 1 2] C. [-3 -2 -1] D. [-2 1 4] E. [-1 2 5] F. [1 4 7] G. [3 4 5]

| A        | B       | C          | D        | E        | F       | G       |
|----------|---------|------------|----------|----------|---------|---------|
| [-3 0 3] | [0 1 2] | [-3 -2 -1] | [-2 1 4] | [-1 2 5] | [1 4 7] | [3 4 5] |

3. What is the output of this code?

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

A.

[[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] 
[ 8 8 8 8 -8 -8 8 8 8 8] 
[ 8 8 8 8 -8 -8 8 8 8 8] 
[ 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]]

B.

[[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 -4 -4 -4 -4 -4 -4]
[ 4 4 4 4 -4 -4 -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]]

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]
[ 8 8 8 8  4 -4 -8 -8 -8 -8]
[ 8 8 8 8  4 -4 -8 -8 -8 -8]
[ 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]
[ 1 1 1 1  1 -1 -1 -1 -1 -1]
[ 1 1 1 1  1 -1 -1 -1 -1 -1]
[ 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]]

E.

[[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] 
[ 1 1 1 1  1  1 1 1 1 1] 
[ 1 1 1 1  1  1 1 1 1 1]
[ 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((3 * x**2) / (x**2), x, np.inf).doit() 
print(round(ans, 2))
| A | B | C  | D      |
|---|---|----|--------|
| 0 | 3 | oo | np.inf |

5. What is the output of this code?

def count_cell_neighbors(M, row, col):

neighbors = M[max(0, row-1):min(row+2, M.shape[0]), 
                max(0, col-1):min(col+2, M.shape[1])]

return neighbors.sum() - M[row, col] M = np.array([
                                [0, 0, 0, 0], 
                                [0, 0, 1, 0], 
                                [0, 0, 1, 1],
                                [0, 0, 0, 0], ])

A = np.zeros_like(M)

for row in range(M.shape[0]):
    for col in range(M.shape[1]):
         A[row, col] = count_cell_neighbors(M, row, col)

plt.matshow(A, cmap='gray') 
plt.colorbar()

A.

B.

C.

D.

6. What is the output of this code?

D = np.zeros((100))

for i in range(int(1e6)):
    rand = int(random.gauss(mu=50, sigma=10)) 
        if 0 <= rand < 100:
            D[rand] += 1

for i in range(int(1e6)):
    rand = int(random.gauss(mu=40, sigma=3)) 
        if 0 <= rand < 100:
            if D[rand] > 0:
                D[rand] -= 1

D /= D.max()

C = np.cumsum(D)

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

ax1.plot(list(range(len(D))), D) 
ax1.set_title('Gaussian mixture')

ax2.plot(list(range(len(C))), C) 
ax2.set_title('Cumulative sum');

A.

B.

C.

D.

E.

Last updated