Last active
August 29, 2015 13:56
-
-
Save lindslev/8873105 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import math | |
| lsst = [] | |
| lsst.append({'id':'40','name':'Joe Bloggs','posts':'4'}) | |
| lsst.append({'id':'567','name':'Jenny Smith','posts':'3'}) | |
| lsst.append({'id':'3','name':'Frank Jones','posts':'54'}) | |
| lsst.append({'id':'46','name':'Samantha Wills','posts':'0'}) | |
| lsst.append({'id':'6789','name':'Ahmed Joseph Naran','posts':'15'}) | |
| def orderList(lst): | |
| orderedList = [] | |
| for elem in lst: | |
| split_name = elem['name'].split(' ') | |
| last_name = split_name.pop() | |
| first_name = split_name.pop() | |
| if elem['posts'] != '0': | |
| orderedList.append({'first_name':first_name, 'last_name':last_name,'posts':elem['posts']}) | |
| orderedList = sorted(orderedList, key=lambda k: int(k['posts'])) | |
| orderedList.reverse() | |
| return orderedList | |
| def generateMathFxns(lst): | |
| total_posts = 0 | |
| average_divisor = 0 | |
| #when dividing to find average, will total users include those who didnt post? | |
| #decision : no | |
| lst = orderList(lst) | |
| list_length = len(lst) | |
| maximum = int(lst[0]['posts']) | |
| minimum = int(lst[list_length-1]['posts']) | |
| if list_length % 2 == 0: | |
| median_index_1 = (list_length / 2) - 1 | |
| median_index_2 = median_index_1 + 1 | |
| median = float((float(lst[median_index_1]['posts']) + float(lst[median_index_2]['posts'])) / 2) | |
| else: | |
| median_index = (list_length + 1) / 2 | |
| median = int(lst[median_index]['posts']) | |
| for elem in lst: | |
| total_posts = total_posts + int(elem['posts']) | |
| average_divisor = average_divisor + 1 | |
| average = float(float(total_posts) / float(average_divisor)) | |
| total_variance = 0 | |
| for elem in lst: | |
| variance_val = float(elem['posts']) - average | |
| variance_val = math.pow(variance_val, 2) | |
| total_variance = total_variance + variance_val | |
| standard_dev = float(math.sqrt(total_variance)) | |
| print "Average: %f" % average | |
| print "Median: %f" % median | |
| print "Minimum: %d" % minimum | |
| print "Maximum: %d" % maximum | |
| print "Standard deviation: %f" % standard_dev | |
| generateMathFxns(lsst) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment