In: Computer Science
Write a class called Person that has two private data members - the person's name and age. It should have an init method that takes two values and uses them to initialize the data members. It should have a get_age method.
Write a separate function (not part of the Person class) called basic_stats that takes as a parameter a list of Person objects and returns a tuple containing the mean, median, and mode of all the ages. To do this, use the mean, median and mode functions in the statistics module. Your basic_stats function should return those three values as a tuple, in the order given above.
For example, it could be used as follows:
p1 = Person("Kyoungmin", 73)
p2 = Person("Mercedes", 24)
p3 = Person("Avanika", 48)
p4 = Person("Marta", 24)
person_list = [p1, p2, p3, p4]
print(basic_stats(person_list)) # should print a tuple of three
values
The files must be named: basic_stats.py
class Person:
name=""
age=0
def __init__(self,nam,age):
self.name = nam
self.age=age
def get_age(self):
return self.age
def basic_stats(person_list):
i=0
ages=[]
n=len(person_list)
while i<n:
ages.append(person_list[i].get_age())
i=i+1
print(ages)
sum = 0
for i in range( 0, n):
sum += ages[i]
mean=sum/n
sorted(ages)
if n % 2 != 0:
median=float(ages[n/2])
else:
median = float((ages[int((n-1)/2)] + ages[int(n/2)])/2.0)
maxelement = max(ages)
temp = maxelement + 1
count = [0] * temp
for i in range(temp) :
count[i] = 0
for i in range(n) :
count[ages[i]] += 1
mode = 0
m = count[0]
for i in range(1, temp) :
if (count[i] > m) :
m = count[i]
mode = i
return
("Mean="+str(mean),"Median="+str(median),"Mode="+str(mode))
p1 = Person("Kyoungmin", 73)
p2 = Person("Mercedes", 24)
p3 = Person("Avanika", 48)
p4 = Person("Marta", 24)
person_list = [p1, p2, p3, p4]
print(basic_stats(person_list))