### Thread: Average Def Function (Newb)

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2011
Posts
14
Rep Power
0

#### Average Def Function (Newb)

Code:
```def average(a):

pass

def mode(a):

pass

def histogram(a,b):

pass```
average(sample)
- average( [a1, ..., an] ) returns the average value of the given list of numbers.
- average: on empty sample list, return None.
Examples:
- average([3,8]) = 5.5
- average([3,8,10]) = 7.0

mode(sample)
mode( [a1, ..., an] ) returns the sorted list, in ascending order, of modes of the given list of numbers.
Examples:
- mode([3, 8, 5, 3]) = [3]
- mode([.3333, .3333, .3333, -1, -1, 1, 1, 1]) = [0.33, 1]

histogram(sample, binBoundaries)
Histogram( [a1, ..., an], [b1, b2, ..., bk] ) returns a new list [c1, c2, ..., ck+1 ], where c1 is the number (count) of values in sample that are smaller than or equal to b1, ck+1 is the number of values in sample that are greater than bk, while ci (for 1 < i < k+1 ) is the number of values in sample that are greater than bi and smaller than or equal to bi+1. That is, the list of values binBoundaries defines a set of "bins" and the function Histogram should count (and return) how many samples fall into each bin.
You can assume that the list binBoundaries is sorted in ascending order.
Examples:
- histogram([3,8], [1]) = [0, 2]
- histogram([3,8], [1,3,6]) = [0, 1, 0, 1]
2. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2011
Posts
14
Rep Power
0

#### Updated coding

Feel free to correct me if I am wrong in this coding.
Code:
```def average(a):
for i in range(len(a)):
tmp = a[i]
sum = sum + tmp
counter = counter + 1
avg = sum/counter
print(avg)
pass

from collections import defaultdict
def mode(a):
counts = defaultdict(int)
for item in a:
counts[item] += 1
return max(counts, key = counts.get)
pass

def histogram(a,b):

pass```
Would I use recursive or stay with this coding?
*Still need to add if the list is empty it returns none

I still don't have a clue how to do the mode though, would I use something like this? Shown above in the code?
3. #### Fix average

Let's fix average. Your version returns neither the average nor None appropriately.

Code:
```def average(a):
'''
average(sample)
- average( [a1, ..., an] )
returns the average value of the given list of numbers.
- average: on empty sample list,
return None.
'''
if not a:                           # handle the empty sample list
return None

sum = counter = 0            # initialize local counter and sum variables
for i in range(len(a)):
tmp = a[i]
sum = sum + tmp
counter = counter + 1
avg = sum/counter

return avg                          # returns the average```
Next you might realize that avg needs to be computed only once. You could dispense with the avg=sum/counter line and return sum/counter . Then you might realize that you'd be better off using the sum function rather than hiding it as a local variable. You'd finally observe that by the end of the loop iterations your counter variable will have the same value as the list length. With all this thought, and just a little more to generalize you'd come up with this version of average:

Code:
```#! python3
'''
run doctests:
\$ python3 p.py
'''

def average(a):
'''
average(sample)
- average( [a1, ..., an] )
returns the average value of the given list of numbers.
- average: on empty sample list,
return None.

>>> average([3,8]) == 5.5
True
>>> average([3,8,10]) == 7
True
>>> average('invalid data') == None
True
'''
try:
return sum(a)/len(a)
except:
pass

if __name__ == "__main__":
import doctest
doctest.testmod()```
Last edited by b49P23TIvg; November 15th, 2011 at 04:39 PM.
4. #### mode---a better effort than was average

You've returned the counts of the mode rather than the mode. defaultdict is a good idea for this project. I inserted try except else logic to trap invalid or invalid data. Then I take the additional step to return the required list---the modes are contained in a list of all the keys which have the maximum occurrences. I stuffed what could be a big loop into a list comprehension.
Code:
```#! python3
'''
run doctests:
\$ python3 p.py
'''

from collections import defaultdict

def mode(a):
'''
>>> mode([3, 8, 5, 3]) == [3]
True
>>> mode(['a', 'a', 'a', -1, -1, 1, 1, 1]) == ['a', 1]
True
>>> mode([]) == None
True
'''
counts = defaultdict(int)
try:
for item in a:
counts[item] += 1
mx = max(counts.values())
except:
return None
else:
# prepare a list of all keys having value mx
return [key for (key,value,) in counts.items() if value == mx]

if __name__ == "__main__":
import doctest
doctest.testmod()```
5. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2011
Posts
14
Rep Power
0

#### Thanks

Thanks so far! I'll be updating my code as the week goes on.
6. No Profile Picture
Contributing User
Devshed Novice (500 - 999 posts)

Join Date
May 2009
Posts
552
Rep Power
34
You only need to compute the average once, and "sum" is a reserved word so it should not be used as a variable name. Also if you are using Python 2.x the divide will return an integer unless you convert to a float.
Code:
```    total = counter = 0
for tmp in a:
total += tmp
counter = counter + 1
avg = float(total)/counter```
7. #### sum is not reserved

Code:
```>>> for=3                   # for is a reserved word.
File "<stdin>", line 1
for=3
^
SyntaxError: invalid syntax
>>>
>>>
>>> sum=8                 # sum is not a reserved word.
>>> del sum
>>> 'sum' in dir(__builtins__)
True
>>> __builtins__.sum = 98
>>> sum
98
>>>```
8. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2011
Posts
14
Rep Power
0

#### Moment Def

Code:
```def moment(a,p):
if not a:
return None

sum = counter = 0
for i in range(len(a)):
tmp = a[i]
sum = sum + tmp
counter = counter + 1
avg = sum/counter
mome = 0
for c in range(len(p)):
for x in range(len(a)):
mom = (a[c] - avg)**p[c]
mome = mome + mom
momem = (1/counter)(mome)
return momem

pass```
This is my current code for function moment. How would I output it as a list?

moment( [a1, ..., an], [p1, p2, ..., pk] ) returns a new list [c1, c2, ..., ck], where ci (for 1 <= i <= k) is the pi -th moment of [a1, ..., an].
The p-th moment is the average of the differences between the values and the mean of the list of values, each difference raised to the p-th power before averaging. That is, the p-th moment of [a1, ..., an] is 1/n [(a1-m)p+ ..+ (an-m)p], where m is the mean of the list of values. You can assume that the pi s are positive integers.
The values returned should be rounded to two(2) decimals.
Examples:
- moment([3,8],[1]) = [0.0]
- moment([3,8],[1,2]) = [0.0, 6.25]
- moment([3,8],[2,1]) = [6.25, 0.0]

moment:
- if the first argument is empty, return None.
- otherwise, if the second argument is empty, moment should return an empty list;
9. #### Use your average function

I'd use a list comprehension to return the moments as a list. I'd also use my average function rather than rewriting average twice more. I'd include tests to verify my code. And I'd make a good effort to post code without gross errors.
Code:
```#! python3
'''
run doctests:
\$ python3 p.py
'''

def average(a):
'''
>>> average([3,8]) == 5.5
True
>>> average([3,8,10]) == 7
True
>>> average('invalid data') == None
True
'''
try:
return sum(a)/len(a)
except:
''

def moment(a,p):
'''
>>> moment([],[])
>>> moment([8,23],[]) == list()
True
>>> moment([3,8],[1]) == [0.0]
True
>>> moment([3,8],[1,2]) == [0.0, 6.25]
True
>>> moment([3,8],[2,1]) == [6.25, 0.0]
True
'''
avg = average(a)  # use your verified average function.
if avg == None:
return None
moments = [average([(A-avg)**P for A in a]) for P in p]
return moments

if __name__ == "__main__":
import doctest
doctest.testmod()```
10. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2011
Posts
14
Rep Power
0

#### Incomplete code.

Code:
```def histogram(a,b):
d = []
for x in a:
if x in a:
d[x]+= 1
else:
d[x] = 1
return ...```
This is the incomplete function for histogram. How do I make it so it does:

histogram(sample, binBoundaries)
Histogram( [a1, ..., an], [b1, b2, ..., bk] ) returns a new list [c1, c2, ..., ck+1 ], where c1 is the number (count) of values in sample that are smaller than or equal to b1, ck+1 is the number of values in sample that are greater than bk, while ci (for 1 < i < k+1 ) is the number of values in sample that are greater than bi and smaller than or equal to bi+1. That is, the list of values binBoundaries defines a set of "bins" and the function Histogram should count (and return) how many samples fall into each bin.
You can assume that the list binBoundaries is sorted in ascending order.
Examples:
- histogram([3,8], [1]) = [0, 2]
- histogram([3,8], [1,3,6]) = [0, 1, 0, 1]
11. #### Too much

Code:
```#! python3

'''
run doctests:
\$ python3 p.py
'''

import bisect
BS = bisect.bisect_left

def histogram(samples,binBoundaries):
'''
>>> histogram([3,8], [1]) == [0, 2]
True
>>> histogram([3,8], [1,3,6]) == [0, 1, 0, 1]
True
'''
h = [0]*(len(binBoundaries)+1)
for sample in samples:
h[BS(binBoundaries, sample)] += 1
return h

if __name__ == "__main__":
import doctest
doctest.testmod()```
For the heck of it, try to implement your own binary search algorithm. Your effort for this histogram problem appears insubstantial.