Question

In: Computer Science

Write a function average_steps(step_records) that takes a list of records, ie, tuples in the form (date_str,...

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

Solutions

Expert Solution

#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...


Related Solutions

Write a function that accepts a dictionary and produces a sorted list of tuples The dictionary...
Write a function that accepts a dictionary and produces a sorted list of tuples The dictionary looks like this: {‘US’: [{'Chicago, IL': ('2/1/2020 19:43', 2, 0, 0)}, {'San Benito, CA': ('2/3/2020 3:53', 2, 0, 0)}, {'Santa Clara, CA': ('2/3/2020 0:43', 2, 0, 0)}, {'Boston, MA': ('2/1/2020 19:43', 1, 0, 0)}, {'Los Angeles, CA': ('2/1/2020 19:53', 1, 0, 0)}, {'Orange, CA': ('2/1/2020 19:53', 1, 0, 0)}, {'Seattle, WA': ('2/1/2020 19:43', 1, 0, 0)}, {'Tempe, AZ': ('2/1/2020 19:43', 1, 0, 0)}], 'Australia'...
Write a function that takes a list of integers as input and returns a list with...
Write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2] Do not use any special or built in functions like append, reverse etc.
#Write a function called find_max_sales. find_max_sales will #have one parameter: a list of tuples. Each tuple...
#Write a function called find_max_sales. find_max_sales will #have one parameter: a list of tuples. Each tuple in the #list will have two items: a string and an integer. The #string will represent the name of a movie, and the integer #will represent that movie's total ticket sales (in millions #of dollars). # #The function should return the movie from the list that #had the most sales. Return only the movie name, not the #full tuple. #Below are some lines of...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should assume that the list is already in ascending order. The function should insert the number into the correct position of the list so that the list stays in ascending order. It should modify the list, not build a new list. It does not need to return the list, because it is modifying it.   Hint: Use a whlie loop and list methods lst = [1,3,5,7]...
Write a Python function that takes a list of string as arguments. When the function is...
Write a Python function that takes a list of string as arguments. When the function is called it should ask the user to make a selection from the options listed in the given list. The it should get input from the user. Place " >" in front of user input. if the user doesn't input one of the given choices, then the program should repeatedly ask the user to pick from the list. Finally, the function should return the word...
Write a Matlab function for a matrix that takes in a matrix in echelon form and...
Write a Matlab function for a matrix that takes in a matrix in echelon form and will return the row canonical form. The function cannot use rref, or any other matlab built in functions.
We were asked this: Write a function that takes a list, and returns a list representing...
We were asked this: Write a function that takes a list, and returns a list representing each word whose reverse is also in the list. def find_reversals(lst: List[str]) -> List[str]: Each pair, such as 'abut', 'tuba', should be represented by the first element encountered. Don't report the same pairs twice. Don't list palindromes. I wrote this, which might not be optimal but passes the unit test! def find_reversals(lst): #Place to save to found_reversals=[]    #Make each string lowercase for i...
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Python Question Using lists, write the function non_unique(list) that takes a list list as argument. It...
Python Question Using lists, write the function non_unique(list) that takes a list list as argument. It returns a list which duplicated elements remains and each duplicated element is followed by a number which shows how many times it appears. All elements in return list should be in the same order as their appearance in the original list. For example, given the input [‘a’, ‘b’, ‘c’, ‘a’, ‘b’, ‘d’, ‘a’,‘e’], the function would return [‘a’, 3, ‘b’, 2]. Another example, ['abc',...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT