Question: Hello, This topic is on list mutation and I am a little rusty on it. Thank you in advance. Part 1: A better Average Fix

Hello, This topic is on list mutation and I am a little rusty on it.

Thank you in advance.

Part 1: A better Average

  • Fix the function find_average so that its implementation (the code it uses) does not change or modify the input. And I can't use any python library to solve this, at least now, so I do so by just rewriting the function.
  • The I have to Update find_average to handle case where the empty list is passed in or if parameter is None. By returning the value None for an average.

Part 2: To Celsius

The following function fetch_temp_from_internet returns a random temperature (in Fahrenheit):

def fetch_temp_from_internet(): import random return random.randint(0,100) 

We will discuss the import statement in another lesson -- it's a way to use other Python libraries. Copy this code exactly into the editor as it is -- you will not modify this code.

Create a function batch_convert which takes in a sequence of temperatures (in Fahrenheit) and returns a new list whose values are the celsius equivalent. In order to convert Fahrenheit to Celsius, you need to subtract 32 from the temperature and multiply by 5/9.

def batch_convert(items): return [] # should be 0 and 100 print(batch_convert([32, 212])) 

You can test a batch of temperatures this way:

f = [] for n in range(0, 10): f.append(fetch_temp_from_internet()) print(batch_convert(f)) 

Make sure you understand the above code.

Part 3:

The following code is part of a project that keeps a history of temperatures. Its current implementation (get_temperatures) calls the function (aka a service) fetch_temp_from_internet() to get the current temperature and then it adds the temperature to a list that it is maintaining:

history = [] def get_temperatures(): temp = fetch_temp_from_internet() history.append(temp) return history 

The problem is that a dubious client (a client is code the calls a library, API, or function) could destroy your service:

def dubious_client(): get_temperatures() print(get_temperatures()) get_temperatures().clear() print(get_temperatures()) 

  • Sure to call dubious_client so you understand the problem.
# dubious_client() 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!