Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ENCM 335 Fall 2019 Lab 8 Exercise C Part 1
- # The geometric mean of a sequence of N numbers is defined to
- # be the Nth root of the product of all of the numbers in the
- # sequence. For example, the geometric mean of the sequence
- # [10, 8, 13] is the cube root of 10 * 8 * 13, which is about 10.13.
- def geometric_mean(x):
- """Return the geometric mean of the numbers in sequence x.
- It's assumed that len(x) >= 1 and all items in x are ints or floats."""
- product = 0
- count = 0
- for number in x:
- product *= number
- count += 1
- result = product**(1/count)
- return result
- def do_a_test(a):
- print('list:', a)
- print('geometric mean:', geometric_mean(a))
- print()
- do_a_test([4, 25])
- do_a_test([0.2, 2.0, 21.0])
- do_a_test([0.5])
- do_a_test([1.0, 2.0, 3.0, 4.0, 5.0])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement