1
+ import math
2
+ import json
3
+
4
+
5
+ class DataStats :
6
+ # original code sourced from https://github.com/lgiordani/datastats
7
+
8
+ def stats (self , data , iage , isalary ):
9
+ # iage and isalary are the starting age and salary used to
10
+ # compute the average yearly increase of salary.
11
+
12
+ # Compute average yearly increase
13
+ average_age_increase = math .floor (
14
+ sum ([e ['age' ] for e in data ])/ len (data )) - iage
15
+ average_salary_increase = math .floor (
16
+ sum ([int (e ['salary' ][1 :]) for e in data ])/ len (data )) - isalary
17
+
18
+ yearly_avg_increase = math .floor (
19
+ average_salary_increase / average_age_increase )
20
+
21
+ # Compute max salary
22
+ salaries = [int (e ['salary' ][1 :]) for e in data ]
23
+ threshold = '$' + str (max (salaries ))
24
+
25
+ max_salary = [e for e in data if e ['salary' ] == threshold ]
26
+
27
+ # Compute min salary
28
+ salaries = [int (d ['salary' ][1 :]) for d in data ]
29
+ min_salary = [e for e in data if e ['salary' ] ==
30
+ '${}' .format (str (min (salaries )))]
31
+
32
+ return json .dumps ({
33
+ 'avg_age' : math .floor (sum ([e ['age' ] for e in data ])/ len (data )),
34
+ 'avg_salary' : math .floor (sum (
35
+ [int (e ['salary' ][1 :]) for e in data ])/ len (data )),
36
+ 'avg_yearly_increase' : yearly_avg_increase ,
37
+ 'max_salary' : max_salary ,
38
+ 'min_salary' : min_salary
39
+ })
0 commit comments