Harold Nelson
9/16/2021
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:
a, the coefficient of the quadratic term.
b, the coefficient of the linear term.
c, the constant term.
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.
## 6
## 2
## 0
## 0
## 2
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.
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
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).
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”.
Repeat Ex3, but use no elif or else statement, just if.
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
## True
## False
## True
## True
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
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)
## True
## True
## False
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
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
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