def filter_list(values, min_value=None, max_value=None):
    """Filters a list of numbers to keep only the ones between one or two thresholds.
    You need to set at least one of min_value or max_value!
    Parameters
    ----------
    values : list
        a list of numbers (floats or ints)
    min_value : float or int, optional
        the minimum threshold to filter the data (inclusive)
    max_value : float or int, optional
        the maximum threshold to filter the data (inclusive)
    Examples
    --------
    >>> a = [1, 3, 4, 2.2]
    >>> filter_list(a, min_value=2)
    [3, 4, 2.2]
    >>> a  # a is unchanged
    [1, 3, 4, 2.2]
    >>> filter_list(a, max_value=3)
    [1, 3, 2.2]
    >>> filter_list(a, min_value=2, max_value=3)
    [3, 2.2]
    >>> filter_list([1, 2, 3, 4])
    Traceback (most recent call last):
     ...
    ValueError: Need to set at least one of min_value or max_value!
    """
    if min_value is None and max_value is None:
        raise ValueError('Need to set at least one of min_value or max_value!')
    output = []
    
    # <your own code here>
    
    return outputAssignment #04
Unit 4
Until the next session, study the provided lecture material and solve the following exercises.
Go back to the program you wrote for the previous exercise #03-04. Make your program code as organised and easy-to-read as possible. To do so, pack the language classification that selects the welcome message into a function welcome_message(name, language) that you use in the main part of your script.
years2days()
Today we coded a function years2days().
- So far, the error message (“traceback”) that we get when providing the function with a different data type other than intorfloatis very cryptic and a user might not learn from the message what they did wrong or why the function fails. Modify the function, so that it prints a meaningful message when the user tries to executeyears2days("ten").
- So far, n_leapyearsis simply added to the conversion result. The function would happily execute the statementyears2days(1, n_leapyears=2), which is obviously meaningless. Implement a check that prints a message and does not execute the conversion ifn_leapyearsis more thanyears.
Below, I pasted the skeleton of a function I want you to code. Most importantly, I provide you with the documentation of the function. We read function documentation in the past (using help()), and we learned how to write the docstrings for our own functions, too. Here, you see a more comprehensive example of a function documentation. Carefully read the documentation and try to understand what the function is supposed to do. The examples help illustrate the information provided by the docstring and the parameter definitions.
- None: We have not yet talked about the Nonekeyword, but if you consult the Internet you will quickly see that it is easy to understand.
- So far, we have used the print()function to craft (“error”) messages to the user. In the function below, I demonstrate you how a “proper” error can be raised.
- You will have to iterate through the provided list of values …
- … and use a well-crafted conditional expression to decide which values …
- … need to be ‘added’ to the output list (find the right function yourself in our classroom material!)
Notebook
For the remaining two exercises, use the opportunity of solving the exercises in a notebook to familiarize yourself with the workflow of creating and deleting cells, writing markdown, and running code in a notebook. Download the file 04_some_tasks.ipynb to find a notebook that I prepared for you.
Code a function that accepts two arguments, first an arbitrary word or sentence (type str, e.g., "energy efficiency") then a character (also str, e.g., "e"). The function should return an integer of how many times the character appeared in the sentence. Write a comprehensive docstring for the function and test whether it performs as expected with a few test cases.
Consider this:
Code a function that accepts an arbitrary sentence as input and returns a list of strings containing the individual words, just like the example above. However, let’s imagine the .split() method does not exist and you have to find another way of achieving this. Does your function return the same result as the split method?
