In: Computer Science
Write a function average_steps(step_records) that takes a list of records, ie, tuples in the form (date_str, step_count), and returns the average number of steps made across all the records as an unrounded floating point number.
If step_records is an empty list then None should be returned.
Note: You should include and use your total_steps function.
Test | Result |
---|---|
step_records = [('2010-01-01',1), ('2010-01-02',2), ('2010-01-03',3)] avg = average_steps(step_records) print(avg) |
2.0 |
step_records = [] avg = average_steps(step_records) print(avg) |
None |
step_records = [('2010-01-01',3), ('2010-01-02',3), ('2010-01-03',3), ('2010-01-04',3), ('2010-01-05',3)] avg = average_steps(step_records) print(avg) |
3.0 |
#source code:
def total_steps(p):
return p
def average_steps(step_records):
c=0
sum=0
total=0
for i in range(len(step_records)):
total=total_steps(i+1)
sum=sum+step_records[i][1]
if(total==0):
return None
else:
return sum/total
step_records=[('2010-01-01',1),('2010-01-02',2),('2010-01-03',3)]
avg=average_steps(step_records)
print(avg)
step_records=[]
avg=average_steps(step_records)
print(avg)
step_records=[('2010-01-01',3),('2010-01-02',3),('2010-01-03',3),('2010-01-04',3),('2010-01-05',3)]
avg=average_steps(step_records)
print(avg)
#output:
#if you have any doubt comment below...