fork download
  1. from math import sqrt
  2.  
  3. def correlation_coefficient(word):
  4. alphabet = 'abcdefghijklmnopqrstuvwxyz'
  5.  
  6. x = list(range(len(word)))
  7. y = list(map(alphabet.index, word))
  8. n = len(word)
  9.  
  10. top = n * sum(a*b for a, b in zip(x, y)) - sum(x) * sum(y)
  11. bottom = (sqrt(n * sum(a ** 2 for a in x) - sum(x) ** 2)
  12. * sqrt(n * sum(a ** 2 for a in y) - sum(y) ** 2))
  13.  
  14. r = top / bottom
  15. return r
  16.  
  17. print(correlation_coefficient('forty')) # => 0.9644068036349517
  18. print(correlation_coefficient('bells')) # => 0.9717931994369533
  19. print(correlation_coefficient('correlation')) # => 0.13063589827847785
Success #stdin #stdout 0s 9992KB
stdin
Standard input is empty
stdout
0.9644068036349517
0.9717931994369533
0.13063589827847785