Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.85 KB | None | 0 0
  1. # ENCM 335 Fall 2019 Lab 8 Exercise C Part 1
  2.  
  3. # The geometric mean of a sequence of N numbers is defined to
  4. # be the Nth root of the product of all of the numbers in the
  5. # sequence.  For example, the geometric mean of the sequence
  6. # [10, 8, 13] is the cube root of 10 * 8 * 13, which is about 10.13.
  7.  
  8.  
  9. def geometric_mean(x):
  10.    
  11.     """Return the geometric mean of  the numbers in sequence x.
  12.  
  13.    It's assumed that len(x) >= 1 and all items in x are ints or floats."""
  14.    
  15.     product = 0;
  16.     count = 0;
  17.  
  18.     for number in x:
  19.         product *= number
  20.         count += 1
  21.     result = product**(1/count)
  22.    
  23.     return result
  24.  
  25.  
  26. def do_a_test(a):
  27.     print('list:', a)
  28.     print('geometric mean:', geometric_mean(a))
  29.     print()
  30.  
  31.  
  32. do_a_test([4, 25])
  33. do_a_test([0.2, 2.0, 21.0])
  34. do_a_test([0.5])
  35. do_a_test([1.0, 2.0, 3.0, 4.0, 5.0])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement