Lab 4, Part 1: Predicates and Functions

Predicates are functions that return a boolean value (True or False). In this part of lab, you'll practice writing predicate functions.

Create a new file called lab04.py. Add your name and your partner's name at the top of the file in comments.

Predicate practice: write a predicate for each of the following tasks.

Task 1A.

Partner A

isLegal returns True if the provided age greater or equal to 21; False otherwise.

Examples:

isLegal(19) ==> False
isLegal(25) ==> True
isLegal(21) ==> True

Invent your own test cases:

# isLegal(??) ==> True
# isLegal(??) ==> False
# What happens when ?? is a string?

Task 1B.

isJustin returns True if the provided name is Justin; False otherwise.

Examples:

isJustin('Lyn') ==> False
isJustin('Justin') ==> True
isJustin('Bieber') ==> False

Invent your own test cases:

# isJustin(??) ==> True
# isJustin(??) ==> False
# Does case matter with your function? Should it?

Task 1C.

notJustinAndLegal returns True if the provided name is NOT Justin and the age is greater than or equal to 21; False otherwise. Hint: use the two predicates above in this one.

Examples:

notJustinAndLegal('Lyn', 35) ==> True
notJustinAndLegal('Justin', 21) ==> False
notJustinAndLegal('Malia', 19) ==> False

Invent your own test cases:

# notJustinAndLegal(??,??) ==> True
# notJustinAndLegal(??,??) ==> False
# Does order of inputs matter?

Task 1D.

Partner B

atLeastDouble returns True if the second number is at least twice as large as the first number; False otherwise.

Examples:

atLeastDouble(5,9) ==> False
atLeastDouble(5,10) ==> True
atLeastDouble(5,15) ==> True

Invent your own test cases:

# atLeastDouble(??,??) ==> True
# atLeastDouble(??,??) ==> False

Task 1E.

inOrder returns True if the three numbers are provided in strictly increasing order; False otherwise.

Examples:

inOrder(1, 2, 3)  ==> True
inOrder(1, 2, 2)  ==> False # 2 is not greater than 2
inOrder(2, 4, 6)  ==> True
inOrder(-3, -2, -1)  ==> True

Invent your own test cases (more than one test case for True and False):

# inOrder(??,??,??) ==> True
# inOrder(??,??,??) ==> True
# inOrder(??,??,??) ==> False
# inOrder(??,??,??) ==> False
# What if all numbers are the same?

Task 1F.

upOrDownOrder returns True if the three numbers are provided in strictly increasing order or strictly decreasing order; False otherwise.

Examples:

upOrDownOrder(1, 2, 3)  ==> True
upOrDownOrder(1, 2, 2)  ==> False # 2 is not greater than 2
upOrDownOrder(3, 2, 1)  ==> True
upOrDownOrder(-3, 0, -1)  ==> False

Invent your own test cases (more than one test case for True and False):

# upOrDownOrder(??,??,??) ==> True
# upOrDownOrder(??,??,??) ==> True
# upOrDownOrder(??,??,??) ==> False
# upOrDownOrder(??,??,??) ==> False

Task 1G.

Partner A

isQuint returns True if the provided residence hall is Beebe, Cazenove, Shafer, Munger or Pomeroy; otherwise returns False.

Examples:

isQuint('Tower') ==> False
isQuint('Munger') ==> True
isQuint('Shafer') ==> True
isQuint('Cazenove') ==> True
isQuint('Stone') ==> False

Invent your own test cases (more than one test case for True and False):

# isQuint(??) ==> True
# isQuint(??) ==> False
# What happens if ?? is a number?
# Does case matter here? Should it?

Table of Contents