Preparation

Create a file called functions_lab.py and put all of your code for today inside this file. If you’d like to work from your StuWork directory, remember to mount the drive and then changing directories to it with:

$ cd /Volumes/COURSES/cs111-03-s19/StuWork/USERNAME/
$ mkdir lab-04-15
$ cd lab-04-15

Exercise 1

Earlier in the term we looked at the following expression.

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(120,0,100)
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, return n divided by 2
  • If n is odd, return n times 3, plus 1

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 *

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 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 4

It is possible to have a function that does not return anything. For example, add the following function to your functions_lab.py file:

def greet(name):
    print("Hello " + name + ". It is nice to meet you!")

a. Refresh the REPL and reload the functions from functions_lab, then predict what the following statement will do:

>>> val = greet("John Cena")

b. Verify your prediction by executing it in the REPL and checking what the value in val is.

Exercise 5

Create a file called scoping_test.py and paste the following code into it.

def foo(n):
    n = 10

def bar(lst):
    lst = [100, 200, 300]

def baz(lst):
    lst[0] = 100
    lst[1] = 200
    lst[2] = 300

def main():
    n = 5
    lst = ["a", "b", "c"]

    print(n, lst)   # A

    foo(n)
    print(n, lst)   # B

    bar(lst)
    print(n, lst)   # C

    baz(lst)
    print(n,lst)    # D

if __name__ == "__main__":
    main()

a. Predict what values will be printed at the lines marked A, B, C, D above. Discuss with your partner why this is the case.

b. Test your predictions by executing python3 scoping_test.py. Was it what you expected?

Exercise 6

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 *