# ----------------------------------------------
#  Question
#  Write a function pearson_corr, which takes as parameters
#  - x, a list of n elements
#  - y, a list of n elements
#  The function should compute the Pearson correlation coefficient and return the result.

#  The formula for computing Pearson correlation coefficient is as follows:
#  ...
# ----------------------------------------------


import math

def pearson_corr(x, y):
    stdv_x, stdv_y, cov_xy = 0, 0, 0
    sum_x, sum_y = 0, 0

    n = len(x)

    for i in range(n):
        sum_x += x[i]
        sum_y += y[i]

    mean_x = sum_x / len(x)
    mean_y = sum_y / len(y)

    for i in range(n):
        stdv_x += (x[i] - mean_x)**2
        stdv_y += (y[i] - mean_y)**2
        cov_xy += (x[i] - mean_x) * (y[i] - mean_y)

    return cov_xy / (math.sqrt(stdv_x) * math.sqrt(stdv_y))

x = [9.49, 5.47,	8.41,	7.43,	7.16,	5.87,	4.26,	7.58,	1.95,	3.87,	5.34,	7.40,	9.26,	3.72,	9.97,	1.96,	3.63,	6.66,	1.40,	2.17]
y = [8.62, 4.85,	2.60,	8.25,	5.65,	3.11,	2.66,	7.82,	4.37,	4.07,	6.46,	6.49,	9.48,	6.64,	5.10,	9.48,	2.73,	6.45,	4.82,	2.82]
print(f"Pearson correlation = {pearson_corr(x, y)}") # 0.3396543266203493

