Lab: Writing Your Own Functions
Preparation
Create a file called functions_lab.py
and put all of your code for today inside this file.
Exercise 1
Earlier in the term we looked at the following expression.
bounded_val = min(max(val,lower), upper)
The idea is that after the expression is evaluated, the number val
is “bounded” between the values of lower
and upper
. This is a useful thing to do, especially when grading homework assignments since you usually want to bound grades to be between 0 and 100.
a. Write a function and name it bound_between
that takes three numeric parameters: val
, lower
, and upper
. The function should return the value of val
, but bounded between the values of lower
and upper
.
b. Save your file and open up a REPL environment in the same directory as the file.
c. Play around with your function by executing the following commands in the REPL.
>>> from functions_lab import bound_between
>>> bounded_val = bound_between(120,0,100)
>>> print(bounded_val)
After executing the above commands, the value of bounded_val
should be 100
since it is bounded to be between 0 and 100.
d. Execute your bound_between
function on a variety of other inputs to make sure it is working properly.
Exercise 2
An interesting function in mathematics is called the hailstone function. The function is simple; it takes a single positive integer input n
and it does the following:
- If
n
is even, returnn
divided by 2 - If
n
is odd, returnn
multiplied by 3
a. Write this function and add it to your functions_lab.py
file.
b. Test it in the REPL by importing it and then executing the function are a variety of n
.
>>> from functions_lab import hailstone
If you’re interested in why the hailstone function is interesting in mathematics, for fun you could read up on the Collatz conjecture! Although, you should not do this now—move on to the next exercise instead!
Exercise 3
a. Write a function called is_valid_date
that takes an integer month
and an integer day
for parameters and returns True
if it is a valid date and False
otherwise. Here are a few example executions of it in the REPL.
>>> is_valid_date(1, 30)
True
>>> is_valid_date(2, 30)
False
>>> is_valid_date(9, 30)
True
>>> is_valid_date(9, 31)
False
>>> is_valid_date(9, -1)
False
>>> is_valid_date(9, 250)
False
b. Test your function by executing it on the examples above in the REPL. Remember that you’ll need to import it first by executing.
>>> from functions_lab import is_valid_date
Exercise 4
a. Write a function called middle_element
that takes in a list or a string and returns the middle element. If the length of the list or string is even, it should return the first of the two middle elements. For example,
>>> middle_element([5,7,1])
7
>>> middle_element("quick")
'i'
>>> middle_element("browns")
'o'
>>> middle_element(["the", "quick", "brown", "fox"])
"quick"
b. Test your function in the REPL.
Exercise 5
a. Write a function called sum_to
that takes a single integer parameter n
and returns the sum of all the integers less than or equal to n
. For example, sum_to(5)
should evaluate to 15
since 1+2+3+4+5=15
. Below are some example return values.
>>> sum_to(1)
1
>>> sum_to(2)
3
>>> sum_to(3)
6
>>> sum_to(4)
10