Exercises on Functions

Harold Nelson

9/16/2021

Ex1

Create a function, quad(a,b,c,x), to evaluate a quadratic polynomial at a given value of x. The parameters of the function are, in order:

  1. a, the coefficient of the quadratic term.

  2. b, the coefficient of the linear term.

  3. c, the constant term.

  4. x, the value at which the polynomial is to be evaluated. This polynomial in standard form is written as:

\[ax^2+bx+c\]

Your function should be fruitful. Test your function with a = 1, b = -7, c = 12.

Use the following values of x: 1,2,3,4,5 for testing.

Answer

def quad(a,b,c,x):
    result = a*x**2 + b*x + c
    return result

res = quad(1, -7, 12, 1)  
print(res)
## 6
res = quad(1, -7, 12, 2)  
print(res)
## 2
res = quad(1, -7, 12, 3)  
print(res)
## 0
res = quad(1, -7, 12, 4)  
print(res)
## 0
res = quad(1, -7, 12, 5)  
print(res)
## 2

Ex2

Repeat the exercise above, but make your function, quadv(a,b,c,x), void. Within the function print out the values of all of the variables with identifying text.

Answer

def quadv(a,b,c,x):
    result = a*x**2 + b*x + c
    print("a is",a)
    print("b is",b)
    print("c is",c)
    print("x is",x)
    print("")
    print("The result is",result)
    
quadv(1,-7,12,1)    
## a is 1
## b is -7
## c is 12
## x is 1
## 
## The result is 6

Ex2

Create a fruitful function divisible(m,n). It returns True if m is divisible by n. Otherwise, it returns False. Test it with (4,2) and (5,3).

Answer

def divisible(m,n):
  return m % n == 0

divisible(4,2)
## True
divisible(5,3)
## False

Ex3

Create a fruitful function fb(n). It returns “fizz” if n is divisible by 3, but not by 5. It returns “buzz” if n is divisible by 5, but not by 3. It returns “fizzbuzz” if n is divisible by both. If n is not divisible by either, it returns “shucks”.

Answer

def fb(n):
  if divisible(n,3) and divisible(n,5):
    return("fizzbuzz")
  elif divisible(n,3):
    return("fizz")
  elif divisible(n,5):
    return("buzz")
  else:
    return("shucks")
  

Tests

fb(13)
## 'shucks'
fb(6)
## 'fizz'
fb(10)
## 'buzz'
fb(15)
## 'fizzbuzz'
fb(37)
## 'shucks'

Ex3a

Repeat Ex3, but use no elif or else statement, just if.

Answer

def fba(n):
  if divisible(n,3) and not divisible(n,5):
    return("fizz")
  
  if divisible(n,5) and not divisible(n,3):
    return("buzz")
  
  if divisible(n,3) and divisible(n,5):
    return("fizzbuzz")
  
  if not divisible(n,3) and not divisible(n,5):
    return("shucks")

Tests

fba(6)
## 'fizz'
fba(10)
## 'buzz'
fba(15)
## 'fizzbuzz'
fba(37)
## 'shucks'

Ex4

The parameter weekday is True if it is a weekday, and the parameter vacation is True if we are on vacation. We sleep in if it is not a weekday or we’re on vacation. Return True if we sleep in.

def sleep_in(weekday,vacation):

sleep_in(False, False) → True
sleep_in(True, False) → False
sleep_in(False, True) → True

Answer

def sleep_in(weekday, vacation):
  if not weekday or vacation:
    return True
  else:
    return False
# This can be shortened to: return(not weekday or vacation)

Tests

sleep_in(False, False)
## True
sleep_in(True, False)
## False
sleep_in(False, True)
## True
sleep_in(True, True)
## True

Ex5

We have two monkeys, a and b, and the parameters a_smile and b_smile indicate if each is smiling. We are in trouble if they are both smiling or if neither of them is smiling. Return True if we are in trouble.

def monkey_trouble(a_smile,b_smile):

monkey_trouble(True, True) → True
monkey_trouble(False, False) → True
monkey_trouble(True, False) → False

Answer

def monkey_trouble(a_smile, b_smile):
  if a_smile and b_smile:
    return True
  if not a_smile and not b_smile:
    return True
  return False
  ## The above can be shortened to:
  ##   return ((a_smile and b_smile) or (not a_smile and not b_smile))
  ## Or this very short version (think about how this is the same as the above)
  ##   return (a_smile == b_smile)

Tests

monkey_trouble(True, True)
## True
monkey_trouble(False, False)
## True
monkey_trouble(True, False)
## False

Ex6

Given two int values, return their sum. Unless the two values are the same, then return double their sum.

def sum_double(a,b):

sum_double(1, 2) → 3
sum_double(3, 2) → 5
sum_double(2, 2) → 8

Answer

def sum_double(a, b):
  # Store the sum in a local variable
  sum = a + b
  
  # Double it if a and b are the same
  if a == b:
    sum = sum * 2
  return sum

Tests

sum_double(1, 2)
## 3
sum_double(3, 2)
## 5
sum_double(2, 2)
## 8

Ex7

Given an int n, return the absolute difference between n and 21, except return double the absolute difference if n is over 21.

def diff21(n):

diff21(19) → 2
diff21(10) → 11
diff21(21) → 0
diff21(24) → 6

Answer

def diff21(n):
  if n <= 21:
    return 21 - n
  else:
    return (n - 21) * 2

Tests

diff21(19)
## 2
diff21(10)
## 11
diff21(21)
## 0
diff21(24)
## 6

Ex8

We have a loud talking parrot. The “hour” parameter is the current hour time in the range 0..23. We are in trouble if the parrot is talking and the hour is before 7 or after 20. Return True if we are in trouble.

def parrot_trouble(talking, hour):

parrot_trouble(True, 6) → True
parrot_trouble(True, 7) → False
parrot_trouble(False, 6) → False
parrot_trouble(True, 22) → True

Answer

def parrot_trouble(talking, hour):
  return (talking and (hour < 7 or hour > 20))
  # Need extra parenthesis around the or clause
  # since and binds more tightly than or.
  # and is like arithmetic *, or is like arithmetic +

Tests

parrot_trouble(True, 6)
## True
parrot_trouble(True, 7)
## False
parrot_trouble(False, 6)
## False
parrot_trouble(True, 22)
## True